24 package org.lightjason.agentspeak.common;
26 import com.google.common.base.Charsets;
27 import com.google.common.hash.Hasher;
28 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29 import org.apache.commons.lang3.StringUtils;
32 import javax.annotation.Nonnull;
33 import javax.annotation.Nullable;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Objects;
39 import java.util.concurrent.CopyOnWriteArrayList;
40 import java.util.function.BiConsumer;
41 import java.util.function.BinaryOperator;
42 import java.util.function.Function;
43 import java.util.function.Supplier;
44 import java.util.stream.Collector;
45 import java.util.stream.Collectors;
46 import java.util.stream.IntStream;
47 import java.util.stream.Stream;
74 public CPath(
final IPath p_path,
final String... p_varargs )
77 m_path.addAll( Arrays.asList( p_varargs ) );
89 m_separator = p_path.separator();
97 public CPath( @Nullable
final String... p_varargs )
99 if ( ( Objects.isNull( p_varargs ) ) || ( p_varargs.length == 0 ) )
103 m_path = Arrays.stream( StringUtils.join( p_varargs, m_separator ).split( m_separator ) )
105 .filter( i -> !i.isEmpty() )
107 if ( m_path.size() == 0 )
118 public CPath( @Nonnull
final Stream<String> p_stream )
129 m_path = Collections.emptyList();
141 return new CPath( p_varargs );
154 Arrays.asList( p_varargs ).subList( 1, p_varargs.length ).stream()
155 .flatMap( i -> Arrays.stream( StringUtils.split( i, p_varargs[0] ) ) )
166 public static IPath from( @Nonnull
final String p_string )
175 return new CPath(
this ).pushback( p_path );
182 return new CPath(
this ).pushback( p_path );
187 public final IPath remove(
final int p_index )
189 if ( !m_path.isEmpty() )
196 public final IPath remove(
final int p_start,
final int p_end )
198 m_path.subList( p_start, p_end ).clear();
205 return p_path.size() <= this.
size()
206 && IntStream.range( 0, p_path.size() ).boxed().parallel().allMatch( i -> this.
get( i - p_path.size() ).
equals( p_path.get( i ) ) );
212 return p_path.size() <= this.
size()
213 && IntStream.range( 0, p_path.size() ).boxed().parallel().allMatch( i -> this.
get( i ).
equals( p_path.get( i ) ) );
219 public final String
get(
final int p_index )
221 return p_index < 0 ? m_path.get( m_path.size() + p_index ) : m_path.get( p_index );
226 public final String
path( @Nonnull
final String p_separator )
228 return StringUtils.join( m_path, p_separator );
235 return StringUtils.join( m_path, m_separator );
249 if ( p_separator.isEmpty() )
252 m_separator = p_separator;
260 IntStream.range( 0, m_path.size() ).boxed().parallel().forEach( i -> m_path.set( i, m_path.get( i ).toLowerCase() ) );
268 IntStream.range( 0, m_path.size() ).boxed().parallel().forEach( i -> m_path.set( i, m_path.get( i ).toUpperCase() ) );
281 public final IPath subpath(
final int p_fromindex,
final int p_toindex )
288 p_toindex > 0 ? p_toindex : this.
size() + p_toindex
290 .mapToObj( m_path::get )
296 public final synchronized String
suffix()
298 return m_path.isEmpty()
300 : m_path.get( m_path.size() - 1 );
307 m_path.forEach( i -> l_hasher.putString( i, Charsets.UTF_8 ) );
308 return l_hasher.hash().hashCode();
312 @SuppressFBWarnings(
"EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS" )
313 public final
boolean equals( final Object p_object )
315 return ( ( p_object instanceof
IPath ) && ( this.
hashCode() == p_object.hashCode() ) )
316 || ( ( p_object instanceof String ) && ( this.
path().hashCode() == p_object.hashCode() ) );
339 p_path.
stream().forEach( m_path::add );
363 final List<String> l_path = Stream.concat( p_path.stream(),
m_path.stream() ).
collect( Collectors.toList() );
376 final String l_suffix = this.
suffix();
412 return Integer.compare( this.
hashCode(), p_path.hashCode() );
424 final List<String> l_dotremove =
m_path.stream()
425 .filter( i -> Objects.nonNull( i ) && ( !i.isEmpty() ) && ( !
".".
equals( i ) ) )
426 .collect( Collectors.toList() );
427 if ( l_dotremove.isEmpty() )
430 final String l_last = l_dotremove.get( l_dotremove.size() - 1 );
431 final List<String> l_backremove = IntStream.range( 0, l_dotremove.size() - 1 )
433 .filter( i -> !l_dotremove.get( i + 1 ).equals(
".." ) )
434 .map( l_dotremove::get )
435 .collect( Collectors.toList() );
436 if ( !
"..".
equals( l_last ) )
437 l_backremove.add( l_last );
441 m_path.addAll( l_backremove );
452 return new Collector<String, List<String>, List<String>>()
455 public final Supplier<List<String>> supplier()
457 return CopyOnWriteArrayList<String>::new;
461 public final BiConsumer<List<String>, String> accumulator()
467 public final BinaryOperator<List<String>> combiner()
477 public final Function<List<String>, List<String>> finisher()
483 public final Set<Characteristics> characteristics()
485 return Collections.emptySet();
498 return new CopyOnWriteArrayList<>();
506 public static Collector<String, IPath, IPath>
collect()
514 private static final class CPathCollector implements Collector<String, IPath, IPath>
520 return () ->
new CPath( Stream.empty() );
538 return Function.identity();
544 return Collections.emptySet();
String DEFAULTSEPERATOR
default seperator
static IPath createPath( @Nonnull final String... p_varargs)
creates a path object from different items
static Collector< String, List< String >, List< String > > collectorfactory()
collector factory
final List< String > m_path
list with path parts *
final BinaryOperator< IPath > combiner()
final int size()
returns the number of path elements
IPath reverse()
reverse path
final IPath pushback( @Nonnull final IPath p_path)
adds a path at the end
final synchronized IPath pushfront( @Nonnull final IPath p_path)
adds a path to the front of the path
CPath()
private ctor for empty path
final Supplier< IPath > supplier()
final IPath subpath(final int p_fromindex)
creates a path of the start index until the end
final Stream< String > stream()
stream over elements
static final long serialVersionUID
serial id
illegal argument exception
final boolean startswith( @Nonnull final IPath p_path)
final int compareTo( @Nonnull final IPath p_path)
common structure for execution definition
final boolean equals(final Object p_object)
final IPath separator( @Nonnull final String p_separator)
sets the separator
static IPath from( @Nonnull final String p_string)
factor method to build path
final synchronized boolean endswith( @Nonnull final IPath p_path)
check of a path ends with another path
CPath(final IPath p_path, final String... p_varargs)
copy-ctor with arguments
static IPath createPathWithSeperator( @Nonnull final String... p_varargs)
creates a path object by splitting a string
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
final boolean empty()
check if the path is empty
class to create a path structure
final synchronized IPath upper()
changes all elements to uppercase
final String path()
returns the full path as string
final IPath subpath(final int p_fromindex, final int p_toindex)
creates a path of the indices
Stream< String > stream()
stream over elements
final Set< Characteristics > characteristics()
final boolean startswith(final String p_path)
check of a path starts with another path
final Function< IPath, IPath > finisher()
static Hasher getTermHashing()
returns the hasing function for term data
static List< String > listfactory()
list factory
final String path( @Nonnull final String p_separator)
final IPath append( @Nonnull final String p_path)
appends a string at the current path and returns the new object
CPath( @Nonnull final Stream< String > p_stream)
ctor
final IPath append( @Nonnull final IPath p_path)
appends a path at the current and returns a new object
final String separator()
returns the separator
synchronized void normalize()
normalize the internal path
final BiConsumer< IPath, String > accumulator()
final synchronized String suffix()
returns the last part of the path
final synchronized IPath lower()
changes all elements to lower-case
class for any helper calls
String m_separator
separator of the path elements *
IPath pushback( @Nonnull final IPath p_path)
adds a path at the end
final IPath pushfront( @Nonnull final String p_path)
adds a path at the front
CPath( @Nonnull final IPath p_path)
copy-ctor
IPath remove(final int p_index)
removes an element
final IPath pushback( @Nonnull final String p_path)
adds a path at the end
final IPath reverse()
reverse path
CPath( @Nullable final String... p_varargs)
ctor
static Collector< String, IPath, IPath > collect()
returns a collector to build a path from strings
final String removesuffix()
remove the suffix from the path