static interface
NodeStream.DescendantNodeStream<T extends Node>
A specialization of
NodeStream
that allows configuring tree traversal behaviour when traversing the descendants of a node.
boolean
all(Predicate<? super @NonNull T> predicate)
Returns whether all elements of this stream match the provided predicate.
default NodeStream<Node>
ancestors()
Returns a node stream containing all the ancestors of the nodes contained in this stream.
default <R extends Node>
NodeStream<R>
ancestors(Class<? extends R> rClass)
Returns the
ancestor streamof each node in this stream, filtered by the given node type.
default NodeStream<Node>
ancestorsOrSelf()
Returns a node stream containing the nodes contained in this stream and their ancestors.
boolean
any(Predicate<? super @NonNull T> predicate)
Returns whether any elements of this stream match the provided predicate.
NodeStream<T>
append(NodeStream<? extends @NonNull T> right)
Returns a new node stream that contains all the elements of this stream, then all the elements of the given stream.
static <O> Function<@Nullable Object,@Nullable O>
asInstanceOf(Class<? extends O> c1, Class<? extends O>... rest)
Returns a map function, that checks whether the parameter is an instance of any of the given classes.
NodeStream<T>
cached()
Returns a node stream containing all the elements of this node stream, but which will evaluate the upstream pipeline only once.
default NodeStream<Node>
children()
Returns a node stream containing all the children of the nodes contained in this stream.
default <R extends Node>
NodeStream<R>
children(Class<? extends R> rClass)
Returns the
children streamof each node in this stream, filtered by the given node type.
<R,A>
R
collect(Collector<? super @NonNull T,A,R> collector)
Collects the elements of this node stream using the specified
Collector
.
int
count()
Returns the number of nodes in this stream.
NodeStream.DescendantNodeStream<Node>
descendants()
Returns a node stream containing all the strict descendants of the nodes contained in this stream.
<R extends Node>
NodeStream.DescendantNodeStream<R>
descendants(Class<? extends R> rClass)
Returns the
descendant streamof each node in this stream, filtered by the given node type.
NodeStream.DescendantNodeStream<Node>
descendantsOrSelf()
Returns a node stream containing the nodes contained in this stream and their descendants.
NodeStream<T>
distinct()
Returns a stream consisting of the distinct elements (w.r.t
Object.equals(Object)
) of this stream.
NodeStream<T>
drop(int n)
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
NodeStream<T>
dropLast(int n)
Returns a stream consisting of the elements of this stream except the n tail elements.
static <T extends Node>
NodeStream<T>
empty()
Returns an empty node stream.
NodeStream<T>
filter(Predicate<? super @NonNull T> predicate)
Returns a node stream consisting of the nodes of this stream that match the given predicate.
default <R extends Node>
NodeStream<R>
filterIs(Class<? extends R> rClass)
Filters the nodes of this stream that are a subtype of the given class.
default <U> NodeStream<T>
filterMatching(Function<? super @NonNull T,? extends @Nullable U> extractor, U comparand)
Filters the nodes of this stream by comparing a value extracted from the nodes with the given constant.
default NodeStream<T>
filterNot(Predicate<? super @NonNull T> predicate)
Filters the node of this stream using the negation of the given predicate.
default <U> NodeStream<T>
filterNotMatching(Function<? super @NonNull T,? extends @Nullable U> extractor, U comparand)
Inverse of
filterMatching(Function, Object)
.
@Nullable T
first()
Returns the first element of this stream, or null
if the stream is empty.
default <R extends Node>
@Nullable R
first(Class<? extends R> rClass)
Returns the first element of this stream of the given type, or null
if there is none.
default @Nullable T
first(Predicate<? super @NonNull T> predicate)
Returns the first element of this stream that matches the given predicate, or null
if there is none.
default <R extends Node>
NodeStream<R>
firstChild(Class<? extends R> rClass)
Returns a stream containing the first child of each of the nodes in this stream that has the given type.
default <R extends Node>
@Nullable R
firstNonNull(Function<? super @NonNull T,? extends @Nullable R> nullableFun)
Returns the first element of this stream for which the mapping function returns a non-null result.
default Optional<T>
firstOpt()
Returns an optional containing the first element of this stream, or an empty optional if the stream is empty.
default @NonNull T
firstOrThrow()
Returns the first element of this stream, or throws a
NoSuchElementException
if the stream is empty.
<R extends Node>
NodeStream<R>
flatMap(Function<? super @NonNull T,? extends @Nullable NodeStream<? extends R>> mapper)
Returns a node stream consisting of the results of replacing each node of this stream with the contents of a stream produced by the given mapping function.
default NodeStream<Node>
followingSiblings()
Returns a node stream containing all the following siblings of the nodes contained in this stream.
void
forEach(Consumer<? super @NonNull T> action)
static <T extends Node,R extends Node>
NodeStream<R>
forkJoin(NodeStream<? extends T> upstream, Function<? super @NonNull T,? extends NodeStream<? extends R>> fst, Function<? super @NonNull T,? extends NodeStream<? extends R>> snd, Function<? super @NonNull T,? extends NodeStream<? extends R>>... rest)
Applies the given mapping functions to the given upstream in order and merges the results into a new node stream.
static <T extends Node>
NodeStream<T>
fromIterable(Iterable<? extends @Nullable T> iterable)
Returns a new node stream that contains the same elements as the given iterable.
default @Nullable T
get(int n)
Returns the element at index n in this stream.
default boolean
isEmpty()
Returns 'true' if the stream has no elements.
@Nullable T
last()
Returns the last element of this stream, or null
if the stream is empty.
default <R extends Node>
@Nullable R
last(Class<? extends R> rClass)
Returns the last element of this stream of the given type, or null
if there is none.
<R extends Node>
NodeStream<R>
map(Function<? super @NonNull T,? extends @Nullable R> mapper)
Returns a node stream consisting of the results of applying the given mapping function to the node of this stream.
boolean
none(Predicate<? super @NonNull T> predicate)
Returns whether no elements of this stream match the provided predicate.
boolean
nonEmpty()
Returns 'true' if the stream has at least one element.
static <T extends Node>
NodeStream<T>
of(@Nullable T node)
Returns a node stream containing zero or one node, depending on whether the argument is null or not.
static <T extends Node>
NodeStream<T>
of(T... nodes)
Returns a node stream whose elements are the given nodes in order.
static <T extends Node>
NodeStream<T>
ofOptional(Optional<? extends T> optNode)
Returns a node stream containing zero or one node, depending on whether the optional is empty or not.
default NodeStream<Node>
parents()
Returns a node stream containing all the (first-degree) parents of the nodes contained in this stream.
NodeStream<T>
peek(Consumer<? super @NonNull T> action)
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
default NodeStream<Node>
precedingSiblings()
Returns a node stream containing all the preceding siblings of the nodes contained in this stream.
NodeStream<T>
prepend(NodeStream<? extends @NonNull T> right)
Returns a new node stream that contains all the elements of the given stream, then all the elements of this stream.
default <R> R
reduce(R identity, BiFunction<? super R,? super @NonNull T,? extends R> accumulate)
Reduce the elements of this stream sequentially.
default int
sumBy(ToIntFunction<? super @NonNull T> toInt)
Sum the elements of this stream by associating them to an integer.
default int
sumByInt(ToIntFunction<? super @NonNull T> intMapper)
Returns the sum of the value of the function applied to all elements of this stream.
NodeStream<T>
take(int maxSize)
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
NodeStream<T>
takeWhile(Predicate<? super @NonNull T> predicate)
Returns the longest prefix of elements that satisfy the given predicate.
default List<T>
toList()
Collects the elements of this node stream into a list.
default <R> List<R>
toList(Function<? super @NonNull T,? extends R> mapper)
Maps the elements of this node stream using the given mapping and collects the results into a list.
Stream<@NonNull T>
toStream()
Returns a new stream of Ts having the pipeline of operations defined by this node stream.
static <T extends Node>
NodeStream<T>
union(Iterable<? extends NodeStream<? extends T>> streams)
Returns a node stream containing all the elements of the given streams, one after the other.
static <T extends Node>
NodeStream<T>
union(NodeStream<? extends T>... streams)
Returns a node stream containing all the elements of the given streams, one after the other.
iterator, spliterator
<R extends Node> NodeStream<R> flatMap(Function<? super @NonNull T,? extends @Nullable NodeStream<? extends R>> mapper)
If you want to flatMap this node stream to a Stream
with arbitrary elements (ie not nodes), use toStream()
then Stream.flatMap(Function)
.
R
- Type of nodes contained in the returned stream
mapper
- A function mapping the elements of this stream to another stream
Stream.flatMap(Function)
<R extends Node> NodeStream<R> map(Function<? super @NonNull T,? extends @Nullable R> mapper)
If you want to map this node stream to a Stream
with arbitrary elements (ie not nodes), use toStream()
then Stream.map(Function)
.
R
- The node type of the new stream
mapper
- A function mapping the elements of this stream to another node type
Stream.map(Function)
NodeStream<T> filter(Predicate<? super @NonNull T> predicate)
Returns a node stream consisting of the nodes of this stream that match the given predicate.
predicate
- A predicate to apply to each node to determine if it should be included
Stream.filter(Predicate)
, filterNot(Predicate)
, filterIs(Class)
, filterMatching(Function, Object)
NodeStream<T> peek(Consumer<? super @NonNull T> action)
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. Note that terminal operations such as
count()
don't necessarily execute the action.
action
- an action to perform on the elements as they are consumed from the stream
NodeStream<T> append(NodeStream<? extends @NonNull T> right)
Returns a new node stream that contains all the elements of this stream, then all the elements of the given stream.
right
- Other stream
union(NodeStream[])
NodeStream<T> prepend(NodeStream<? extends @NonNull T> right)
Returns a new node stream that contains all the elements of the given stream, then all the elements of this stream.
right
- Other stream
union(NodeStream[])
NodeStream<T> cached()
Returns a node stream containing all the elements of this node stream, but which will evaluate the upstream pipeline only once. The returned stream is not necessarily lazy, which means it may evaluate the upstream pipeline as soon as the call to this method is made.
This is useful e.g. if you want to call several terminal operations without executing the pipeline several times. For example,
NodeStream<T> stream = NodeStream.of(...) // long pipeline // ... .cached() // downstream // ... ; stream.forEach(this::addViolation); // both up- and downstream will be evaluated curViolations += stream.count(); // only downstream is evaluated
NodeStream<T> take(int maxSize)
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
maxSize
- Maximum size of the returned stream
IllegalArgumentException
- if n is negative
Stream.limit(long)
, drop(int)
NodeStream<T> drop(int n)
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
n
- the number of leading elements to skip
IllegalArgumentException
- if n is negative
Stream.skip(long)
, take(int)
, dropLast(int)
NodeStream<T> dropLast(int n)
Returns a stream consisting of the elements of this stream except the n tail elements. If n is greater than the number of elements of this stream, returns an empty stream. This requires a lookahead buffer in general.
n
- the number of trailing elements to skip
IllegalArgumentException
- if n is negative
drop(int)
NodeStream<T> takeWhile(Predicate<? super @NonNull T> predicate)
Returns the longest prefix of elements that satisfy the given predicate.
predicate
- The predicate used to test elements.
NodeStream<T> distinct()
default NodeStream<Node> ancestors()
This is equivalent to flatMap(Node::ancestors)
.
Node.ancestors()
, ancestorsOrSelf()
, ancestors(Class)
default NodeStream<Node> ancestorsOrSelf()
This is equivalent to flatMap(Node::ancestorsOrSelf)
.
ancestors()
default NodeStream<Node> parents()
This is equivalent to map(Node::getParent)
.
ancestors()
, ancestorsOrSelf()
default NodeStream<Node> children()
This is equivalent to flatMap(Node::children)
.
Node.children()
, children(Class)
NodeStream.DescendantNodeStream<Node> descendants()
Node.descendants()
, descendants(Class)
, descendantsOrSelf()
NodeStream.DescendantNodeStream<Node> descendantsOrSelf()
Node.descendantsOrSelf()
, descendants()
default NodeStream<Node> followingSiblings()
Returns a node stream containing all the following siblings of the nodes contained in this stream.
default NodeStream<Node> precedingSiblings()
Returns a node stream containing all the preceding siblings of the nodes contained in this stream. The nodes are yielded from left to right, i.e. in document order.
default <R extends Node> NodeStream<R> children(Class<? extends R> rClass)
Returns the
children streamof each node in this stream, filtered by the given node type.
This is equivalent to children().filterIs(rClass)
.
R
- Type of node the returned stream should contain
rClass
- Type of node the returned stream should contain
filterIs(Class)
, Node.children(Class)
default <R extends Node> NodeStream<R> firstChild(Class<? extends R> rClass)
This is equivalent to flatMap(it -> it.children(rClass).take(1))
.
R
- Type of node the returned stream should contain
rClass
- Type of node the returned stream should contain
Node.children(Class)
<R extends Node> NodeStream.DescendantNodeStream<R> descendants(Class<? extends R> rClass)
R
- Type of node the returned stream should contain
rClass
- Type of node the returned stream should contain
filterIs(Class)
, Node.descendants(Class)
default <R extends Node> NodeStream<R> ancestors(Class<? extends R> rClass)
Returns the
ancestor streamof each node in this stream, filtered by the given node type.
This is equivalent to ancestors().filterIs(rClass)
.
R
- Type of node the returned stream should contain
rClass
- Type of node the returned stream should contain
filterIs(Class)
, Node.ancestors(Class)
default NodeStream<T> filterNot(Predicate<? super @NonNull T> predicate)
This is equivalent to filter(predicate.negate())
predicate
- A predicate to apply to each node to determine if it should be included
filter(Predicate)
default <U> NodeStream<T> filterMatching(Function<? super @NonNull T,? extends @Nullable U> extractor, U comparand)
Filters the nodes of this stream by comparing a value extracted from the nodes with the given constant. This takes care of null value by calling
Objects.equals(Object, Object)
. E.g. to filter nodes that have the
image"a"
, use
filterMatching(Node::getImage, "a")
.
This is equivalent to filter(t -> Objects.equals(extractor.apply(t), comparand))
.
U
- Type of value to compare
extractor
- Function extracting a value from the nodes of this stream
comparand
- Value to which the extracted value will be compared
filter(Predicate)
, filterNotMatching(Function, Object)
default <R extends Node> NodeStream<R> filterIs(Class<? extends R> rClass)
This is equivalent to filter(rClass::isInstance).map(rClass::cast)
.
R
- The type of the nodes of the returned stream
rClass
- The type of the nodes of the returned stream
filter(Predicate)
, asInstanceOf(Class, Class[])
default <U> NodeStream<T> filterNotMatching(Function<? super @NonNull T,? extends @Nullable U> extractor, U comparand)
U
- Type of value to compare
extractor
- Function extracting a value from the nodes of this stream
comparand
- Value to which the extracted value will be compared
filter(Predicate)
, filterMatching(Function, Object)
default <R> R reduce(R identity, BiFunction<? super R,? super @NonNull T,? extends R> accumulate)
Reduce the elements of this stream sequentially.
R
- Result type
identity
- Identity element
accumulate
- Combine an intermediate result with a new node from this stream, returns the next intermediate result
default int sumBy(ToIntFunction<? super @NonNull T> toInt)
Sum the elements of this stream by associating them to an integer.
toInt
- Map an element to an integer, which will be added to the running sum returns the next intermediate result
int count()
Returns the number of nodes in this stream.
default int sumByInt(ToIntFunction<? super @NonNull T> intMapper)
Returns the sum of the value of the function applied to all elements of this stream.
intMapper
- Mapping function
boolean nonEmpty()
Returns 'true' if the stream has at least one element.
isEmpty()
default boolean isEmpty()
Returns 'true' if the stream has no elements.
nonEmpty()
boolean any(Predicate<? super @NonNull T> predicate)
Returns whether any elements of this stream match the provided predicate. If the stream is empty then false is returned and the predicate is not evaluated.
predicate
- The predicate that one element should match for this method to return true
all(Predicate)
, none(Predicate)
boolean none(Predicate<? super @NonNull T> predicate)
Returns whether no elements of this stream match the provided predicate. If the stream is empty then true is returned and the predicate is not evaluated.
predicate
- The predicate that no element should match for this method to return true
any(Predicate)
, all(Predicate)
boolean all(Predicate<? super @NonNull T> predicate)
Returns whether all elements of this stream match the provided predicate. If the stream is empty then true is returned and the predicate is not evaluated.
predicate
- The predicate that all elements should match for this method to return true
any(Predicate)
, none(Predicate)
default @Nullable T get(int n)
Returns the element at index n in this stream. If no such element exists,
null
is returned.
This is equivalent to
.drop(n)
.first()
If you'd rather continue processing the nth element as a node stream, you can use
.drop(n)
.take(1)
n
- Index of the element to find
null
if it doesn't exist
IllegalArgumentException
- if n is negative
@Nullable T first()
Returns the first element of this stream, or
null
if the stream is empty.
If you'd rather continue processing the first element as a node stream, you can use take(1)
.
This is equivalent to get(0)
.
null
if it doesn't exist
first(Predicate)
, first(Class)
, firstOpt()
default @NonNull T firstOrThrow()
first(Predicate)
, first(Class)
, firstOpt()
default Optional<T> firstOpt()
This is equivalent to Optional.ofNullable(first())
.
first(Predicate)
, first(Class)
, first()
default @Nullable T first(Predicate<? super @NonNull T> predicate)
Returns the first element of this stream that matches the given predicate, or null
if there is none.
predicate
- The predicate that one element should match for this method to return it
null
if it doesn't exist
first()
, first(Class)
default <R extends Node> @Nullable R first(Class<? extends R> rClass)
Returns the first element of this stream of the given type, or null
if there is none.
R
- The type of node to find
rClass
- The type of node to find
null
if it doesn't exist
first()
, first(Predicate)
default <R extends Node> @Nullable R firstNonNull(Function<? super @NonNull T,? extends @Nullable R> nullableFun)
Returns the first element of this stream for which the mapping function returns a non-null result. Returns null if there is no such element. This is a convenience method to use with
asInstanceOf(Class, Class[])
, because using just
map
followed by
first()
will lose the type information and mentioning explicit type arguments would be needed.
R
- Result type
nullableFun
- Mapper function
asInstanceOf(Class, Class[])
@Nullable T last()
Returns the last element of this stream, or null
if the stream is empty. This may or may not require traversing all the elements of the stream.
null
if it doesn't exist
default <R extends Node> @Nullable R last(Class<? extends R> rClass)
Returns the last element of this stream of the given type, or null
if there is none.
R
- The type of node to find
rClass
- The type of node to find
null
if it doesn't exist
last()
<R,A> R collect(Collector<? super @NonNull T,A,R> collector)
R
- the type of the result
A
- the intermediate accumulation type of the Collector
collector
- the Collector
describing the reduction
Stream.collect(Collector)
, Collectors
, toList()
, toList(Function)
Stream<@NonNull T> toStream()
Returns a new stream of Ts having the pipeline of operations defined by this node stream. This can be called multiple times.
default List<T> toList()
Collects the elements of this node stream into a list. Just like for
Collectors.toList()
, there are no guarantees on the type, mutability, serializability, or thread-safety of the returned list.
This is equivalent to collect(Collectors.toList())
.
Collectors.toList()
, collect(Collector)
default <R> List<R> toList(Function<? super @NonNull T,? extends R> mapper)
This is equivalent to collect(Collectors.mapping(mapper, Collectors.toList()))
.
R
- Return type of the mapper, and element type of the returned list
mapper
- Mapping function
Collectors.mapping(Function, Collector)
, collect(Collector)
static <T extends Node> NodeStream<T> of(@Nullable T node)
If you know the node is not null, you can also call node.
.asStream()
T
- Element type of the returned stream
node
- The node to contain
Node.asStream()
static <T extends Node> NodeStream<T> fromIterable(Iterable<? extends @Nullable T> iterable)
It's possible to map an iterator to a node stream by calling fromIterable(() -> iterator)
, but then the returned node stream would only be iterable once.
T
- Type of nodes in the returned node stream
iterable
- Source of nodes
static <T extends Node> NodeStream<T> ofOptional(Optional<? extends T> optNode)
Returns a node stream containing zero or one node, depending on whether the optional is empty or not.
T
- Element type of the returned stream
optNode
- The node to contain
of(Node)
@SafeVarargs static <T extends Node> NodeStream<T> of(T... nodes)
Returns a node stream whose elements are the given nodes in order. Null elements are not part of the resulting node stream.
T
- Element type of the returned stream
nodes
- The elements of the new stream
@SafeVarargs static <T extends Node> NodeStream<T> union(NodeStream<? extends T>... streams)
Returns a node stream containing all the elements of the given streams, one after the other.
T
- The type of stream elements
streams
- the streams to flatten
static <T extends Node> NodeStream<T> union(Iterable<? extends NodeStream<? extends T>> streams)
Returns a node stream containing all the elements of the given streams, one after the other.
T
- The type of stream elements
streams
- the streams to flatten
static <T extends Node> NodeStream<T> empty()
Returns an empty node stream.
T
- Expected type of nodes.
@SafeVarargs static <T extends Node,R extends Node> NodeStream<R> forkJoin(NodeStream<? extends T> upstream, Function<? super @NonNull T,? extends NodeStream<? extends R>> fst, Function<? super @NonNull T,? extends NodeStream<? extends R>> snd, Function<? super @NonNull T,? extends NodeStream<? extends R>>... rest)
Applies the given mapping functions to the given upstream in order and merges the results into a new node stream. This allows exploring several paths at once on the same stream. The method is lazy and won't evaluate the upstream pipeline several times.
R
- Common supertype for the element type of the streams returned by the mapping functions
upstream
- Source of the stream
fst
- First mapper
snd
- Second mapper
rest
- Rest of the mappers
@SafeVarargs static <O> Function<@Nullable Object,@Nullable O> asInstanceOf(Class<? extends O> c1, Class<? extends O>... rest)
Returns a map function, that checks whether the parameter is an instance of any of the given classes. If so, it returns the parameter, otherwise it returns null.
This may be used to filter a node stream to those specific classes, for example:
NodeStream<ASTExpression> exprs = someStream.map(asInstanceOf(ASTInfixExpression.class, ASTCastExpression.class));
Using this in the middle of a call chain might require passing explicit type arguments:
ASTTypeDeclaration ts =
node.ancestors()
.<ASTTypeDeclaration>map(asInstanceOf(ASTClassDeclaration.class, ASTEnumDeclaration.class))
.first(); // would not compile without the explicit type arguments
For this use case the firstNonNull(Function)
method may be used, which reduces the above to
ASTTypeDeclaration ts =
node.ancestors().firstNonNull(asInstanceOf(ASTClassDeclaration.class, ASTEnumDeclaration.class));
O
- Output type
c1
- First type to test
rest
- Other types to test
firstNonNull(Function)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4