public abstract class TraversableBase<T> extends FeaturedBase implements C.Traversable<T>
Provide default implementation to some C.Traversable interface
C.Featured.Factory| Constructor and Description |
|---|
TraversableBase() |
| Modifier and Type | Method and Description |
|---|---|
C.Traversable<T> |
accept(Lang.Visitor<? super T> visitor)
Iterate this
Traversable with a visitor function. |
boolean |
allMatch(Lang.Function<? super T,Boolean> predicate)
Check if all elements match the predicate specified
|
boolean |
anyMatch(Lang.Function<? super T,Boolean> predicate)
Check if any elements matches the predicate specified
|
<R> C.Traversable<R> |
collect(String path) |
C.Traversable<T> |
each(Lang.Visitor<? super T> visitor)
Alias of
C.Traversable.accept(Lang.Visitor) |
C.Traversable<T> |
eager()
Returns this traversable and turn off
C.Feature.LAZY |
C.Traversable<T> |
filter(Lang.Function<? super T,Boolean> predicate)
Returns an new traversable that contains all elements in the current traversable except that does not pass the test of the filter function specified.
|
Lang.Option<T> |
findOne(Lang.Function<? super T,Boolean> predicate)
Iterate the traversal to check if any element applied to the predicate the iteration process stop when the element is found and return an option describing the element.
|
<R> C.Traversable<R> |
flatMap(Lang.Function<? super T,? extends Iterable<? extends R>> mapper)
Returns a traversable consisting of the results of replacing each element of this stream with the contents of the iterable produced by applying the provided mapping function to each element.
|
TraversableBase<T> |
forEach(Lang.Visitor<? super T> visitor)
Iterate through this traversal and apply the visitor function specified to each element iterated
|
protected int |
generateHashCode()
Sub class can override this method to provide more efficient algorithm to generate hash code.
|
int |
hashCode()
Iterate through the traversal to aggregate hash code of all element.
|
protected EnumSet<C.Feature> |
initFeatures()
Sub class should override this method to provide initial feature set for the feature based instance
|
boolean |
isEmpty()
Is this traversal empty?
|
C.Traversable<T> |
lazy()
Returns this traversable and try to turn on
C.Feature.LAZY. |
<R> C.Traversable<R> |
map(Lang.Function<? super T,? extends R> mapper)
Returns an new traversable with a mapper function specified.
|
boolean |
noneMatch(Lang.Function<? super T,Boolean> predicate)
Check if no elements matches the predicate specified.
|
C.Traversable<T> |
parallel()
Returns this traversable and try to turn on
C.Feature.PARALLEL. |
Lang.Option<T> |
reduce(Lang.Func2<T,T,T> accumulator)
Iterate through the traversal to apply the accumulator to the result of previous application and the element being iterated.
|
<R> R |
reduce(R identity,
Lang.Func2<R,T,R> accumulator)
Iterate through the traversal to apply the accumulator to the result of previous application and the element being iterated.
|
C.Traversable<T> |
sequential()
Returns this traversable and turn off
C.Feature.PARALLEL |
features_, features, is, setFeature, unsetFeatureclone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, waitsizeforEach, iterator, spliteratorfeatures, isprotected EnumSet<C.Feature> initFeatures()
FeaturedBaseSub class should override this method to provide initial feature set for the feature based instance
initFeatures in class FeaturedBasepublic TraversableBase<T> forEach(Lang.Visitor<? super T> visitor)
Iterate through this traversal and apply the visitor function specified to each element iterated
forEach in interface C.Traversable<T>visitor - the visitorTraversable instanceprotected int generateHashCode()
Sub class can override this method to provide more efficient algorithm to generate hash code. The default implementation use Lang.iterableHashCode(Iterable) to generate the hash code
public int hashCode()
Iterate through the traversal to aggregate hash code of all element. If the traversal is C.Feature.IMMUTABLE a cached hashcode will be generated at first time calling this method and returned directly for the following calls
public C.Traversable<T> lazy()
C.TraversableReturns this traversable and try to turn on C.Feature.LAZY. If lazy is not supported then return this traversable directly without any state change
lazy in interface C.Traversable<T>public C.Traversable<T> eager()
C.TraversableReturns this traversable and turn off C.Feature.LAZY
eager in interface C.Traversable<T>public C.Traversable<T> parallel()
C.TraversableReturns this traversable and try to turn on C.Feature.PARALLEL. If this traversable does not support C.Feature.PARALLEL then return this traversable directly without any state change
parallel in interface C.Traversable<T>public C.Traversable<T> sequential()
C.TraversableReturns this traversable and turn off C.Feature.PARALLEL
sequential in interface C.Traversable<T>public boolean isEmpty()
C.TraversableIs this traversal empty?
isEmpty in interface C.Traversable<T>true if the traversal is empty or false otherwisepublic <R> R reduce(R identity,
Lang.Func2<R,T,R> accumulator)
Iterate through the traversal to apply the accumulator to the result of previous application and the element being iterated. If the traversal is empty then return the identity specified
reduce in interface C.Traversable<T>R - the type of the identity and the return valueidentity - the identity value for the accumulating functionaccumulator - the function the combine two valuespublic Lang.Option<T> reduce(Lang.Func2<T,T,T> accumulator)
Iterate through the traversal to apply the accumulator to the result of previous application and the element being iterated. If the traversal is empty then return Lang.NONE, otherwise an Lang.Option wrapping the accumulated result is returned
reduce in interface C.Traversable<T>accumulator - the function the combine two values_.NONE if the traversal is empty or an option describing the final accumulated valuepublic Lang.Option<T> findOne(Lang.Function<? super T,Boolean> predicate)
Iterate the traversal to check if any element applied to the predicate the iteration process stop when the element is found and return an option describing the element. If no element applied to the predicate then Lang.NONE is returned
findOne in interface C.Traversable<T>predicate - the function map element to Booleanpublic boolean anyMatch(Lang.Function<? super T,Boolean> predicate)
C.TraversableCheck if any elements matches the predicate specified
anyMatch in interface C.Traversable<T>predicate - the function to test the elementtrue if any element matches the predicatepublic boolean noneMatch(Lang.Function<? super T,Boolean> predicate)
C.TraversableCheck if no elements matches the predicate specified. This should be equivalent to:
this.allMatch(_.F.negate(predicate));
noneMatch in interface C.Traversable<T>predicate - the function to test the elementtrue if none element matches the predicatepublic boolean allMatch(Lang.Function<? super T,Boolean> predicate)
C.TraversableCheck if all elements match the predicate specified
allMatch in interface C.Traversable<T>predicate - the function to test the elementtrue if all elements match the predicatepublic C.Traversable<T> accept(Lang.Visitor<? super T> visitor)
C.TraversableIterate this Traversable with a visitor function. This method does not specify the approach to iterate through this structure. The implementation might choose iterate from left to right, or vice versa. It might even choose to split the structure into multiple parts, and iterate through them in parallel
accept in interface C.Traversable<T>visitor - a function that apply to element in this Traversable. The return value of the function is ignoredTraversable instance for chained callpublic C.Traversable<T> each(Lang.Visitor<? super T> visitor)
C.TraversableAlias of C.Traversable.accept(Lang.Visitor)
each in interface C.Traversable<T>visitor - the visitor to tranverse the elementsTraversable instancepublic <R> C.Traversable<R> map(Lang.Function<? super T,? extends R> mapper)
C.TraversableReturns an new traversable with a mapper function specified. The element in the new traversal is the result of the mapper function applied to this traversal element.
Traversable traversable = C.list(23, .NONE, null); assertEquals(C.list(true, false, false), traversal.map(.F.NOT_NULL)); assertEquals(C.list(“23”, "“, ”"), traversal.map(_.F.AS_STRING));
For Lazy Traversable, it must use lazy evaluation for this method. Otherwise it is up to implementation to decide whether use lazy evaluation or not
map in interface C.Traversable<T>R - the element type of the new traversalmapper - the function that applied to element in this traversal and returns element in the result traversalpublic <R> C.Traversable<R> flatMap(Lang.Function<? super T,? extends Iterable<? extends R>> mapper)
C.TraversableReturns a traversable consisting of the results of replacing each element of this stream with the contents of the iterable produced by applying the provided mapping function to each element. If the result of the mapping function is null, this is treated as if the result is an empty traversable.
flatMap in interface C.Traversable<T>R - the element type of the the new traversablemapper - the function produce an iterable when applied to an elementpublic <R> C.Traversable<R> collect(String path)
collect in interface C.Traversable<T>public C.Traversable<T> filter(Lang.Function<? super T,Boolean> predicate)
C.TraversableReturns an new traversable that contains all elements in the current traversable except that does not pass the test of the filter function specified.
Traversable traversable = C.list(-1, 0, 1, -3, 7); Traversable filtered = traversable.filter(_.F.gt(0)); assertTrue(filtered.contains(1)); assertFalse(filtered.contains(-3));
filter in interface C.Traversable<T>predicate - the function that test if the element in the traversable should be kept in the resulting traversable. When applying the filter function to the element, if the result is true then the element will be kept in the resulting traversable.Copyright © 2014–2021 OSGL (Open Source General Library). All rights reserved.