Package net.sf.saxon.s9api.streams
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:
AutoCloseable
,BaseStream<T,
,Stream<T>> Stream<T>
XdmStream extends the capabilities of the standard JDK
Stream
.
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T>
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
asAtomic()
Return the result of the stream as anXdmAtomicValue
.asList()
Return the result of the stream as aList<XdmItem>
.Return the result of the stream as aList<XdmAtomicValue>
.Return the result of the stream as aList<XdmNode>
.asNode()
Return the result of the stream as anXdmNode
.Return the result of the stream as anOptional<XdmAtomicValue>
.Return the result of the stream as anOptional<XdmNode>
.Return the result of the stream as anOptional<String>
.asString()
Return the result of the stream as anString
.Return the result of the stream as an XdmValue.at
(int position) Return the item at a given position in the stream.void
close()
<R> R
collect
(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) <R,
A> R long
count()
distinct()
boolean
exists()
Return true if the stream is non-empty.Filter a stream of items, to create a new stream containing only those items that satisfy a supplied conditionfindAny()
first()
Return the first item of this stream, if there is one, discarding the remainder.<R> Stream
<R> flatMapToDouble
(Function<? super T, ? extends DoubleStream> mapper) flatMapToInt
(Function<? super T, ? extends IntStream> mapper) flatMapToLong
(Function<? super T, ? extends LongStream> mapper) flatMapToXdm
(Step<U> mapper) void
void
forEachOrdered
(Consumer<? super T> action) boolean
iterator()
last()
Return the last item of this stream, if there is one, discarding the remainder.limit
(long maxSize) <R> Stream
<R> mapToDouble
(ToDoubleFunction<? super T> mapper) mapToInt
(ToIntFunction<? super T> mapper) mapToLong
(ToLongFunction<? super T> mapper) max
(Comparator<? super T> comparator) min
(Comparator<? super T> comparator) boolean
parallel()
reduce
(BinaryOperator<T> accumulator) reduce
(T identity, BinaryOperator<T> accumulator) <U> U
reduce
(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) skip
(long n) sorted()
sorted
(Comparator<? super T> comparator) subStream
(int start, int end) Return the items at a given range of positions in the stream.Object[]
toArray()
<A> A[]
toArray
(IntFunction<A[]> generator) untilFirstExclusive
(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 itemuntilFirstInclusive
(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 itemMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.stream.Stream
dropWhile, mapMulti, mapMultiToDouble, mapMultiToInt, mapMultiToLong, takeWhile, toList
-
Field Details
-
base
-
-
Constructor Details
-
XdmStream
- Parameters:
base
- the stream of items
-
XdmStream
Create anXdmStream
consisting of zero or one items, supplied in the form of anOptional<XdmItem>
- Parameters:
input
- the optional item
-
-
Method Details
-
filter
Filter a stream of items, to create a new stream containing only those items that satisfy a supplied condition -
map
-
mapToInt
-
mapToLong
-
mapToDouble
- Specified by:
mapToDouble
in interfaceStream<T extends XdmItem>
-
flatMap
-
flatMapToXdm
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.- Type Parameters:
U
- the type of items returned by the mapping function- Parameters:
mapper
- the mapping function- Returns:
- a new stream of items
-
flatMapToInt
- Specified by:
flatMapToInt
in interfaceStream<T extends XdmItem>
-
flatMapToLong
- Specified by:
flatMapToLong
in interfaceStream<T extends XdmItem>
-
flatMapToDouble
- Specified by:
flatMapToDouble
in interfaceStream<T extends XdmItem>
-
distinct
-
sorted
-
sorted
-
peek
-
limit
-
skip
-
forEach
-
forEachOrdered
- Specified by:
forEachOrdered
in interfaceStream<T extends XdmItem>
-
toArray
-
toArray
-
reduce
-
reduce
-
reduce
-
collect
public <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) -
collect
-
min
-
max
-
count
public long count() -
anyMatch
-
allMatch
-
noneMatch
-
findFirst
-
findAny
-
iterator
-
spliterator
- Specified by:
spliterator
in interfaceBaseStream<T extends XdmItem,
Stream<T extends XdmItem>>
-
isParallel
public boolean isParallel()- Specified by:
isParallel
in interfaceBaseStream<T extends XdmItem,
Stream<T extends XdmItem>>
-
sequential
- Specified by:
sequential
in interfaceBaseStream<T extends XdmItem,
Stream<T extends XdmItem>>
-
parallel
-
unordered
-
onClose
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBaseStream<T extends XdmItem,
Stream<T extends XdmItem>>
-
asXdmValue
Return the result of the stream as an XdmValue. This is a terminal operation.- Returns:
- the contents of the stream, as an XdmValue.
-
asList
Return the result of the stream as aList<XdmItem>
. This is a terminal operation.- Returns:
- the contents of the stream, as a
List<XdmItem>
.
-
asListOfNodes
Return the result of the stream as aList<XdmNode>
. This is a terminal operation.- Returns:
- the list of nodes delivered by the stream
- Throws:
ClassCastException
- if the stream contains an item that is not a node
-
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 nodeClassCastException
- if the stream contains an item that is not a node
-
asNode
Return the result of the stream as anXdmNode
. This is a terminal operation.- Returns:
- the single node delivered by the stream
- Throws:
ClassCastException
- if the stream contains an item that is not a nodeXdmCollectors.MultipleItemException
- if the stream contains more than one itemNoSuchElementException
- if the stream is empty
-
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:
ClassCastException
- if the stream contains an item that is not an atomic value
-
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 itemClassCastException
- if the stream contains an item that is not an atomic value
-
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:
ClassCastException
- if the stream contains an item that is not atomicXdmCollectors.MultipleItemException
- if the stream contains more than one itemNoSuchElementException
- if the stream is empty
-
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 itemUnsupportedOperationException
- if the stream contains an item that has no string value, for example a function item
-
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:
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 itemNoSuchElementException
- if the stream is empty
-
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()
-
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
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.
-
at
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
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
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
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.
-