Class XdmStream<T extends XdmItem>
- java.lang.Object
-
- net.sf.saxon.s9api.streams.XdmStream<T>
-
- Type Parameters:
T
- The type of items delivered by the stream.
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.util.stream.BaseStream<T,java.util.stream.Stream<T>>
,java.util.stream.Stream<T>
public class XdmStream<T extends XdmItem> extends java.lang.Object implements java.util.stream.Stream<T>
XdmStream
extends the capabilities of the standard JDKStream
class.The extensions are:
- Additional terminal operations are provided, allowing the results of the
stream to be delivered for example as a
List<XdmNode>
or anOptional<XdmNode>
more conveniently than using the general-purposeCollector
interface. - Many of these terminal operations are short-circuiting, that is, they stop processing input when no further input is required.
- The additional terminal operations throw a checked exception if a dynamic error occurs while generating the content of the stream.
The implementation is customized to streams of
XdmItem
s.Note: This class is implemented by wrapping a base stream. Generally, the methods on this class delegate to the base stream; those methods that return a stream wrap the stream returned by the base class. The context object can be used by a terminal method on the XdmStream to signal to the originator of the stream that no further input is required.
-
-
Constructor Summary
Constructors Constructor Description XdmStream(java.util.Optional<T> input)
Create anXdmStream
consisting of zero or one items, supplied in the form of anOptional<XdmItem>
XdmStream(java.util.stream.Stream<T> base)
Create anXdmStream
from a generalStream
that returns XDM items.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allMatch(java.util.function.Predicate<? super T> predicate)
Returns true if every item in the stream matches a supplied predicateboolean
anyMatch(java.util.function.Predicate<? super T> predicate)
Returns true if any item in the stream matches a supplied predicateXdmAtomicValue
asAtomic()
Return the result of the stream as anXdmAtomicValue
.java.util.List<T>
asList()
Return the contents of the stream as aList<XdmItem>
.java.util.List<XdmAtomicValue>
asListOfAtomic()
Return the result of the stream as aList<XdmAtomicValue>
.java.util.List<XdmNode>
asListOfNodes()
Return the result of the stream as aList<XdmNode>
.XdmNode
asNode()
Return the result of the stream as anXdmNode
.java.util.Optional<XdmAtomicValue>
asOptionalAtomic()
Return the result of the stream as anOptional<XdmAtomicValue>
.java.util.Optional<XdmNode>
asOptionalNode()
Return the result of the stream as anOptional<XdmNode>
.java.util.Optional<java.lang.String>
asOptionalString()
Return the result of the stream as anOptional<String>
.java.lang.String
asString()
Return the result of the stream as anString
.XdmValue
asXdmValue()
Return the contents of the stream as an XdmValue.java.util.Optional<T>
at(int position)
Return the item at a given position in the stream.void
close()
Close the stream<R> R
collect(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator, java.util.function.BiConsumer<R,R> combiner)
<R,A>
Rcollect(java.util.stream.Collector<? super T,A,R> collector)
long
count()
Returns the number of items in the streamXdmStream<T>
distinct()
Returns a stream consisting of the distinct items present in this stream.boolean
exists()
Return true if the stream is non-empty.XdmStream<T>
filter(java.util.function.Predicate<? super T> predicate)
Filter a stream of items, to create a new stream containing only those items that satisfy a supplied condition.java.util.Optional<T>
findAny()
Returns an item in the stream, chosen arbitrarily, orOptional.empty()
if the stream is emptyjava.util.Optional<T>
findFirst()
Returns the first item in the stream, orOptional.empty()
if the stream is emptyXdmStream<T>
first()
Return the first item of this stream, if there is one, discarding the remainder.T
firstItem()
Return the first item of this stream, if there is one, discarding the remainder; return null if the stream is empty.<R> java.util.stream.Stream<R>
flatMap(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.java.util.stream.DoubleStream
flatMapToDouble(java.util.function.Function<? super T,? extends java.util.stream.DoubleStream> mapper)
Returns anDoubleStream
consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.java.util.stream.IntStream
flatMapToInt(java.util.function.Function<? super T,? extends java.util.stream.IntStream> mapper)
Returns anIntStream
consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.java.util.stream.LongStream
flatMapToLong(java.util.function.Function<? super T,? extends java.util.stream.LongStream> mapper)
Returns anLongStream
consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.<U extends XdmItem>
XdmStream<U>flatMapToXdm(Step<U> mapper)
void
forEach(java.util.function.Consumer<? super T> action)
Performs a given action once for each item of the stream, in non-deterministic ordervoid
forEachOrdered(java.util.function.Consumer<? super T> action)
Performs a given action once for each item of the stream, in the order in which the items appearboolean
isParallel()
Ask whether this stream will be evaluated in paralleljava.util.Iterator<T>
iterator()
Get an iterator over the items in the streamXdmStream<T>
last()
Return the last item of this stream, if there is one, discarding the remainder.T
lastItem()
Return the last item of this stream, if there is one, discarding the remainder; return null if the stream is empty.XdmStream<T>
limit(long maxSize)
Returns a stream containing the initial items of this stream, up to a maximum size<R> java.util.stream.Stream<R>
map(java.util.function.Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.java.util.stream.DoubleStream
mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
Returns aDoubleStream
consisting of the results of applying the given function to the elements of this stream.java.util.stream.IntStream
mapToInt(java.util.function.ToIntFunction<? super T> mapper)
Returns anIntStream
consisting of the results of applying the given function to the elements of this stream.java.util.stream.LongStream
mapToLong(java.util.function.ToLongFunction<? super T> mapper)
Returns aLongStream
consisting of the results of applying the given function to the elements of this stream.java.util.Optional<T>
max(java.util.Comparator<? super T> comparator)
Returns the maximum item in the stream of items, comparing them using the suppliedComparator
.java.util.Optional<T>
min(java.util.Comparator<? super T> comparator)
Returns the minimum item in the stream of items, comparing them using the suppliedComparator
.boolean
noneMatch(java.util.function.Predicate<? super T> predicate)
Returns true if no item in the stream matches a supplied predicatejava.util.stream.Stream<T>
onClose(java.lang.Runnable closeHandler)
Returns an equivalent stream with a specified handler to be called when the stream is exhaustedjava.util.stream.Stream<T>
parallel()
Returns an equivalent stream that will (where possible and appropriate) be evaluated in parallelXdmStream<T>
peek(java.util.function.Consumer<? super T> action)
Returns the supplied stream, while invoking a supplied action on each element of the stream as it is processed.java.util.Optional<T>
reduce(java.util.function.BinaryOperator<T> accumulator)
Callson the underlying stream
T
reduce(T identity, java.util.function.BinaryOperator<T> accumulator)
Performs a reduction or fold operation on the items in the stream.<U> U
reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator, java.util.function.BinaryOperator<U> combiner)
java.util.stream.Stream<T>
sequential()
Returns an equivalent stream that will be evaluated sequentiallyXdmStream<T>
skip(long n)
Returns a stream containing the items of this stream after skipping a specified number of items.XdmStream<T>
sorted()
Returns a stream consisting of the elements of this stream, in sorted order.XdmStream<T>
sorted(java.util.Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, in sorted order using a suppliedComparator
.java.util.Spliterator<T>
spliterator()
Get a Spliterator over the items in the streamXdmStream<T>
subStream(int start, int end)
Return the items at a given range of positions in the stream.java.lang.Object[]
toArray()
Returns an array containing the items in this stream<A> A[]
toArray(java.util.function.IntFunction<A[]> generator)
Returns an array containing the items in this stream, using a supplied function to generate the array; this allows the type of the returned array to be controlled.java.util.stream.Stream<T>
unordered()
Returns an equivalent stream that offers no guarantee of retaining the order of itemsXdmStream<T>
untilFirstExclusive(java.util.function.Predicate<? super XdmItem> predicate)
Experimental method to return the content of a stream up to the first item that satisfies a given predicate, excluding that itemXdmStream<T>
untilFirstInclusive(java.util.function.Predicate<? super XdmItem> predicate)
Experimental method to return the content of a stream up to the first item that satisfies a given predicate, including that item
-
-
-
Method Detail
-
filter
public XdmStream<T> filter(java.util.function.Predicate<? super T> predicate)
Filter a stream of items, to create a new stream containing only those items that satisfy a supplied condition.For example,
body.select(child("*")).filter(n -> n.getNodeName().getLocalName().startsWith("h"))
returns a stream of all the child elements ofbody
whose local name starts with "h".Note: an alternative to filtering a stream is to use a
Step
that incorporates aPredicate
, for examplebody.select(child("*").where(n -> n.getNodeName().getLocalName().startsWith("h")))
- Specified by:
filter
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
predicate
- the supplied condition. AnyPredicate
can be supplied, but some particularly useful predicates are available by calling static methods on thePredicates
class, for examplePredicates.empty(Steps.child("author"))
, which is true for a node that has no child elements with local name "author".- Returns:
- the filtered stream
-
map
public <R> java.util.stream.Stream<R> map(java.util.function.Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.This is an intermediate operation.
For example,
n.select(child(*)).map(c -> c.getNodeName().getLocalName())
returns a stream delivering the local names of the element children ofn
, as instances ofjava.lang.String
. Note the result is aStream
, not anXdmStream
.- Specified by:
map
in interfacejava.util.stream.Stream<T extends XdmItem>
- Type Parameters:
R
- The element type of the new stream- Parameters:
mapper
- a non-interfering, stateless function to apply to each element- Returns:
- the new stream
-
mapToInt
public java.util.stream.IntStream mapToInt(java.util.function.ToIntFunction<? super T> mapper)
Returns anIntStream
consisting of the results of applying the given function to the elements of this stream.For example,
n.select(child(*)).map(c -> c.getStringValue().length())
returns a stream delivering the lengths of the string-values of the element children ofn
. Note the result is aStream
, not anXdmStream
.This is an intermediate operation.
- Specified by:
mapToInt
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
mapper
- a non-interfering, stateless function to apply to each element- Returns:
- the new stream
-
mapToLong
public java.util.stream.LongStream mapToLong(java.util.function.ToLongFunction<? super T> mapper)
Returns aLongStream
consisting of the results of applying the given function to the elements of this stream.This is an intermediate operation.
- Specified by:
mapToLong
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
mapper
- a non-interfering, stateless function to apply to each element- Returns:
- the new stream
-
mapToDouble
public java.util.stream.DoubleStream mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
Returns aDoubleStream
consisting of the results of applying the given function to the elements of this stream.This is an intermediate operation.
- Specified by:
mapToDouble
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
mapper
- a non-interfering, stateless function to apply to each element- Returns:
- the new stream
-
flatMap
public <R> java.util.stream.Stream<R> flatMap(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream isclosed
after its contents have been placed into this stream. (If a mapped stream isnull
an empty stream is used, instead.)This is an intermediate operation.
Note: The
flatMap()
operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream. This corresponds to the action of the "!" operator in XPath.- Specified by:
flatMap
in interfacejava.util.stream.Stream<T extends XdmItem>
- Type Parameters:
R
- The element type of the new stream- Parameters:
mapper
- a non-interfering, stateless function to apply to each element which produces a stream of new values- Returns:
- the new stream
-
flatMapToXdm
public <U extends XdmItem> XdmStream<U> flatMapToXdm(Step<U> mapper)
Create a newXdmStream
by applying a mapping function (specifically, aStep
) to each item in the stream. TheStep
returns a sequence of items, which are inserted into the result sequence in place of the original item.This method is similar to
flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>)
, but differs in that it returns anXdmStream
, making additional methods available.Note:
XdmValue.select(net.sf.saxon.s9api.streams.Step<T>)
is implemented using this method, and in practice it is usually clearer to use that method directly. For examplenode.select(child("*")).flatMapToXdm(child(*))
can be written asnode.select(child("*").then(child("*"))
. Both expressions return a stream containing all the grandchildren elements ofnode
. The same result can be achieved more concisely by writingnode.select(path("*", "*"))
- Type Parameters:
U
- the type of items returned by the mapping function- Parameters:
mapper
- the mapping function- Returns:
- a new stream of items
-
flatMapToInt
public java.util.stream.IntStream flatMapToInt(java.util.function.Function<? super T,? extends java.util.stream.IntStream> mapper)
Returns anIntStream
consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream isclosed
after its contents have been placed into this stream. (If a mapped stream isnull
an empty stream is used, instead.)This is an intermediate operation.
- Specified by:
flatMapToInt
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
mapper
- a non-interfering, stateless function to apply to each element which produces a stream of new values- Returns:
- the new stream
- See Also:
flatMap(Function)
-
flatMapToLong
public java.util.stream.LongStream flatMapToLong(java.util.function.Function<? super T,? extends java.util.stream.LongStream> mapper)
Returns anLongStream
consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream isclosed
after its contents have been placed into this stream. (If a mapped stream isnull
an empty stream is used, instead.)This is an intermediate operation.
- Specified by:
flatMapToLong
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
mapper
- a non-interfering, stateless function to apply to each element which produces a stream of new values- Returns:
- the new stream
- See Also:
flatMap(Function)
-
flatMapToDouble
public java.util.stream.DoubleStream flatMapToDouble(java.util.function.Function<? super T,? extends java.util.stream.DoubleStream> mapper)
Returns anDoubleStream
consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream isclosed
after its contents have placed been into this stream. (If a mapped stream isnull
an empty stream is used, instead.)This is an intermediate operation.
- Specified by:
flatMapToDouble
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
mapper
- a non-interfering, stateless function to apply to each element which produces a stream of new values- Returns:
- the new stream
- See Also:
flatMap(Function)
-
distinct
public XdmStream<T> distinct()
Returns a stream consisting of the distinct items present in this stream. Items are compared usingObject.equals(java.lang.Object)
. This means that twoXdmNode
objects are compared by node identity (so two separate nodes are distinct even if they have the same name and the same content).
-
sorted
public XdmStream<T> sorted()
Returns a stream consisting of the elements of this stream, in sorted order.Note, this method is unlikely to be useful, because most
XdmItem
values do not implementComparable
.
-
sorted
public XdmStream<T> sorted(java.util.Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, in sorted order using a suppliedComparator
.
-
peek
public XdmStream<T> peek(java.util.function.Consumer<? super T> action)
Returns the supplied stream, while invoking a supplied action on each element of the stream as it is processed.This method is designed primarily for debugging, to allow the contents of a stream to be monitored.
-
limit
public XdmStream<T> limit(long maxSize)
Returns a stream containing the initial items of this stream, up to a maximum size
-
skip
public XdmStream<T> skip(long n)
Returns a stream containing the items of this stream after skipping a specified number of items.
-
forEach
public void forEach(java.util.function.Consumer<? super T> action)
Performs a given action once for each item of the stream, in non-deterministic order
-
forEachOrdered
public void forEachOrdered(java.util.function.Consumer<? super T> action)
Performs a given action once for each item of the stream, in the order in which the items appear
-
toArray
public java.lang.Object[] toArray()
Returns an array containing the items in this stream
-
toArray
public <A> A[] toArray(java.util.function.IntFunction<A[]> generator)
Returns an array containing the items in this stream, using a supplied function to generate the array; this allows the type of the returned array to be controlled.- Specified by:
toArray
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
generator
- a function that takes an integer as argument and returns an array of the given length- Returns:
- an array containing all the items in the stream; the type of the array is determined by the generator function
-
reduce
public T reduce(T identity, java.util.function.BinaryOperator<T> accumulator)
Performs a reduction or fold operation on the items in the stream.For example, given a sequence of elements of the form
<change year="2020" rise="1.03"/>
the accumulated rise over a number of years may be computed aschanges.stream().select(attribute("rise")) .map(a->a.getTypedValue().getDoubleValue()) .reduce(1, (x, y) -> x*y)
- Specified by:
reduce
in interfacejava.util.stream.Stream<T extends XdmItem>
- Parameters:
identity
- an initial value of an accumulator variable. This must be an identity value for the supplied accumulator function (so if F is the accumulator function,F(identity, V)
returnsV
for any value ofV
).accumulator
- the accumulator function: this takes the old value of the accumulator variable and the current item from the stream as arguments, and returns a new value for the accumulator variable. This function must be associative, that is,F(A, F(B, C))
must always give the same result asF(F(A, B), C))
- Returns:
- the final value of the accumulator variable after all items have been processed.
-
reduce
public java.util.Optional<T> reduce(java.util.function.BinaryOperator<T> accumulator)
Callson the underlying stream
-
reduce
public <U> U reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator, java.util.function.BinaryOperator<U> combiner)
-
collect
public <R> R collect(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator, java.util.function.BiConsumer<R,R> combiner)
-
collect
public <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
-
min
public java.util.Optional<T> min(java.util.Comparator<? super T> comparator)
Returns the minimum item in the stream of items, comparing them using the suppliedComparator
.
-
max
public java.util.Optional<T> max(java.util.Comparator<? super T> comparator)
Returns the maximum item in the stream of items, comparing them using the suppliedComparator
.
-
count
public long count()
Returns the number of items in the stream
-
anyMatch
public boolean anyMatch(java.util.function.Predicate<? super T> predicate)
Returns true if any item in the stream matches a supplied predicate
-
allMatch
public boolean allMatch(java.util.function.Predicate<? super T> predicate)
Returns true if every item in the stream matches a supplied predicate
-
noneMatch
public boolean noneMatch(java.util.function.Predicate<? super T> predicate)
Returns true if no item in the stream matches a supplied predicate
-
findFirst
public java.util.Optional<T> findFirst()
Returns the first item in the stream, orOptional.empty()
if the stream is empty
-
findAny
public java.util.Optional<T> findAny()
Returns an item in the stream, chosen arbitrarily, orOptional.empty()
if the stream is empty
-
iterator
public java.util.Iterator<T> iterator()
Get an iterator over the items in the stream
-
spliterator
public java.util.Spliterator<T> spliterator()
Get a Spliterator over the items in the stream
-
isParallel
public boolean isParallel()
Ask whether this stream will be evaluated in parallel
-
sequential
public java.util.stream.Stream<T> sequential()
Returns an equivalent stream that will be evaluated sequentially
-
parallel
public java.util.stream.Stream<T> parallel()
Returns an equivalent stream that will (where possible and appropriate) be evaluated in parallel
-
unordered
public java.util.stream.Stream<T> unordered()
Returns an equivalent stream that offers no guarantee of retaining the order of items
-
onClose
public java.util.stream.Stream<T> onClose(java.lang.Runnable closeHandler)
Returns an equivalent stream with a specified handler to be called when the stream is exhausted
-
close
public void close()
Close the stream
-
asXdmValue
public XdmValue asXdmValue()
Return the contents of the stream as an XdmValue. This is a terminal operation.- Returns:
- the contents of the stream, as an XdmValue.
-
asList
public java.util.List<T> asList()
Return the contents of the stream as aList<XdmItem>
. This is a terminal operation.- Returns:
- the contents of the stream, as a
List<XdmItem>
.
-
asListOfNodes
public java.util.List<XdmNode> asListOfNodes()
Return the result of the stream as aList<XdmNode>
. This is a terminal operation.Node: the method makes it convenient to process the contents of a stream using a for-each loop, for example:
for (XdmNode n : start.select(child().where(isText())).asList()) { process(n) }
A more idiomatic style, however, is to use the
forEach(java.util.function.Consumer<? super T>)
method:start.select(child().where(isText())).forEach(n -> process(n))
- Returns:
- the list of nodes delivered by the stream
- Throws:
java.lang.ClassCastException
- if the stream contains an item that is not a node
-
asOptionalNode
public java.util.Optional<XdmNode> asOptionalNode()
Return the result of the stream as anOptional<XdmNode>
. This is a terminal operation.- Returns:
- the single node delivered by the stream, or absent if the stream is empty
- Throws:
XdmCollectors.MultipleItemException
- if the stream contains more than one nodejava.lang.ClassCastException
- if the stream contains an item that is not a node
-
asNode
public XdmNode asNode()
Return the result of the stream as anXdmNode
. This is a terminal operation.- Returns:
- the single node delivered by the stream
- Throws:
java.lang.ClassCastException
- if the stream contains an item that is not a nodeXdmCollectors.MultipleItemException
- if the stream contains more than one itemjava.util.NoSuchElementException
- if the stream is empty
-
asListOfAtomic
public java.util.List<XdmAtomicValue> asListOfAtomic()
Return the result of the stream as aList<XdmAtomicValue>
. This is a terminal operation.- Returns:
- the list of atomic values delivered by the stream
- Throws:
java.lang.ClassCastException
- if the stream contains an item that is not an atomic value
-
asOptionalAtomic
public java.util.Optional<XdmAtomicValue> asOptionalAtomic()
Return the result of the stream as anOptional<XdmAtomicValue>
. This is a terminal operation.- Returns:
- the string value of the single item delivered by the stream, or absent if the stream is empty
- Throws:
XdmCollectors.MultipleItemException
- if the stream contains more than one itemjava.lang.ClassCastException
- if the stream contains an item that is not an atomic value
-
asAtomic
public XdmAtomicValue asAtomic()
Return the result of the stream as anXdmAtomicValue
. This is a terminal operation.- Returns:
- the string value of the single item delivered by the stream, or a zero-length string if the stream is empty
- Throws:
java.lang.ClassCastException
- if the stream contains an item that is not atomicXdmCollectors.MultipleItemException
- if the stream contains more than one itemjava.util.NoSuchElementException
- if the stream is empty
-
asOptionalString
public java.util.Optional<java.lang.String> asOptionalString()
Return the result of the stream as anOptional<String>
. This is a terminal operation.- Returns:
- the string value of the single item delivered by the stream, or absent if the stream is empty
- Throws:
XdmCollectors.MultipleItemException
- if the stream contains more than one itemjava.lang.UnsupportedOperationException
- if the stream contains an item that has no string value, for example a function item
-
asString
public java.lang.String asString()
Return the result of the stream as anString
. This is a terminal operation.- Returns:
- the string value of the single item delivered by the stream
- Throws:
java.lang.UnsupportedOperationException
- if the stream contains an item that has no string value, for example a function itemXdmCollectors.MultipleItemException
- if the stream contains more than one itemjava.util.NoSuchElementException
- if the stream is empty
-
first
public XdmStream<T> first()
Return the first item of this stream, if there is one, discarding the remainder. This is a short-circuiting operation similar tofindFirst()
, but it returnsXdmStream<T>
rather thanOptional<T>
so that further operations such asatomize()
can be applied, and so that a typed result can be returned using a method such asasOptionalNode()
orasOptionalString()
- Returns:
- a stream containing the first item in this stream
-
firstItem
public T firstItem()
Return the first item of this stream, if there is one, discarding the remainder; return null if the stream is empty. This is a short-circuiting operation similar tofindFirst()
- Returns:
- the first item in this stream, or null if the stream is empty.
-
exists
public boolean exists()
Return true if the stream is non-empty. This is a short-circuiting terminal operation.- Returns:
- true if at least one item is present in the stream.
-
last
public XdmStream<T> last()
Return the last item of this stream, if there is one, discarding the remainder. This is a short-circuiting operation similar tofirst()
; it returnsXdmStream<T>
rather thanOptional<T>
so that further operations suchatomize()
can be applied, and so that a typed result can be returned using a method such asasOptionalNode()
orasOptionalString()
- Returns:
- a stream containing only the last item in the stream, or an empty stream if the input is empty.
-
lastItem
public T lastItem()
Return the last item of this stream, if there is one, discarding the remainder; return null if the stream is empty. This is a short-circuiting operation similar tolastItem()
.- Returns:
- the last item in the stream, or null if the input is empty.
-
at
public java.util.Optional<T> at(int position)
Return the item at a given position in the stream. This is a short-circuiting terminal operation.- Parameters:
position
- the required position; items in the stream are numbered from zero.- Returns:
- the item at the given position if there is one; otherwise,
Optional.empty()
-
subStream
public XdmStream<T> subStream(int start, int end)
Return the items at a given range of positions in the stream. For example, subStream(0, 3) returns the first three items in the stream. This is a short-circuiting terminal operation.- Parameters:
start
- the position of the first required item; items in the stream are numbered from zero.end
- the position immediately after the last required item.- Returns:
- a stream containing those items whose zero-based position is greater-than-or-equal-to start, and less-than end. No error occurs if either start or end is out of range, or if end is less than start.
-
untilFirstInclusive
public XdmStream<T> untilFirstInclusive(java.util.function.Predicate<? super XdmItem> predicate)
Experimental method to return the content of a stream up to the first item that satisfies a given predicate, including that item- Parameters:
predicate
- a condition that determines when the stream should stop- Returns:
- a stream containing all items in the base stream up to and including the first item that satisfies a given predicate.
-
untilFirstExclusive
public XdmStream<T> untilFirstExclusive(java.util.function.Predicate<? super XdmItem> predicate)
Experimental method to return the content of a stream up to the first item that satisfies a given predicate, excluding that item- Parameters:
predicate
- a condition that determines when the stream should stop- Returns:
- a stream containing all items in the base stream up to the item immediately before the first item that satisfies a given predicate.
-
-