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>
public class XdmStream<T extends XdmItem> extends Object implements Stream<T>
XdmStream extends the capabilities of the standard JDKStream.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-purposeCollectorinterface. - 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
XdmItems.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallMatch(Predicate<? super T> predicate)booleananyMatch(Predicate<? super T> predicate)XdmAtomicValueasAtomic()Return the result of the stream as anXdmAtomicValue.List<T>asList()Return the result of the stream as aList<XdmItem>.List<XdmAtomicValue>asListOfAtomic()Return the result of the stream as aList<XdmAtomicValue>.List<XdmNode>asListOfNodes()Return the result of the stream as aList<XdmNode>.XdmNodeasNode()Return the result of the stream as anXdmNode.Optional<XdmAtomicValue>asOptionalAtomic()Return the result of the stream as anOptional<XdmAtomicValue>.Optional<XdmNode>asOptionalNode()Return the result of the stream as anOptional<XdmNode>.Optional<String>asOptionalString()Return the result of the stream as anOptional<String>.StringasString()Return the result of the stream as anString.XdmValueasXdmValue()Return the result of the stream as an XdmValue.Optional<T>at(int position)Return the item at a given position in the stream.voidclose()<R> Rcollect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)<R,A>
Rcollect(Collector<? super T,A,R> collector)longcount()XdmStream<T>distinct()booleanexists()Return true if the stream is non-empty.XdmStream<T>filter(Predicate<? super T> predicate)Filter a stream of items, to create a new stream containing only those items that satisfy a supplied conditionOptional<T>findAny()Optional<T>findFirst()XdmStream<T>first()Return the first item of this stream, if there is one, discarding the remainder.<R> Stream<R>flatMap(Function<? super T,? extends Stream<? extends R>> mapper)DoubleStreamflatMapToDouble(Function<? super T,? extends DoubleStream> mapper)IntStreamflatMapToInt(Function<? super T,? extends IntStream> mapper)LongStreamflatMapToLong(Function<? super T,? extends LongStream> mapper)<U extends XdmItem>
XdmStream<U>flatMapToXdm(Step<U> mapper)voidforEach(Consumer<? super T> action)voidforEachOrdered(Consumer<? super T> action)booleanisParallel()Iterator<T>iterator()XdmStream<T>last()Return the last item of this stream, if there is one, discarding the remainder.XdmStream<T>limit(long maxSize)<R> Stream<R>map(Function<? super T,? extends R> mapper)DoubleStreammapToDouble(ToDoubleFunction<? super T> mapper)IntStreammapToInt(ToIntFunction<? super T> mapper)LongStreammapToLong(ToLongFunction<? super T> mapper)Optional<T>max(Comparator<? super T> comparator)Optional<T>min(Comparator<? super T> comparator)booleannoneMatch(Predicate<? super T> predicate)Stream<T>onClose(Runnable closeHandler)Stream<T>parallel()XdmStream<T>peek(Consumer<? super T> action)Optional<T>reduce(BinaryOperator<T> accumulator)Treduce(T identity, BinaryOperator<T> accumulator)<U> Ureduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)Stream<T>sequential()XdmStream<T>skip(long n)XdmStream<T>sorted()XdmStream<T>sorted(Comparator<? super T> comparator)Spliterator<T>spliterator()XdmStream<T>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)Stream<T>unordered()XdmStream<T>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 itemXdmStream<T>untilFirstInclusive(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(Predicate<? super T> predicate)
Filter a stream of items, to create a new stream containing only those items that satisfy a supplied condition
-
mapToInt
public IntStream mapToInt(ToIntFunction<? super T> mapper)
-
mapToLong
public LongStream mapToLong(ToLongFunction<? super T> mapper)
-
mapToDouble
public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
- Specified by:
mapToDoublein interfaceStream<T extends XdmItem>
-
flatMapToXdm
public <U extends XdmItem> XdmStream<U> flatMapToXdm(Step<U> mapper)
Create a newXdmStreamby applying a mapping function (specifically, aStep) to each item in the stream. TheStepreturns 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
public IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
- Specified by:
flatMapToIntin interfaceStream<T extends XdmItem>
-
flatMapToLong
public LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
- Specified by:
flatMapToLongin interfaceStream<T extends XdmItem>
-
flatMapToDouble
public DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
- Specified by:
flatMapToDoublein interfaceStream<T extends XdmItem>
-
sorted
public XdmStream<T> sorted(Comparator<? super T> comparator)
-
forEachOrdered
public void forEachOrdered(Consumer<? super T> action)
- Specified by:
forEachOrderedin interfaceStream<T extends XdmItem>
-
toArray
public <A> A[] toArray(IntFunction<A[]> generator)
-
reduce
public T reduce(T identity, BinaryOperator<T> accumulator)
-
reduce
public Optional<T> reduce(BinaryOperator<T> accumulator)
-
reduce
public <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
-
collect
public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
-
min
public Optional<T> min(Comparator<? super T> comparator)
-
max
public Optional<T> max(Comparator<? super T> comparator)
-
spliterator
public Spliterator<T> spliterator()
- Specified by:
spliteratorin interfaceBaseStream<T extends XdmItem,Stream<T extends XdmItem>>
-
isParallel
public boolean isParallel()
- Specified by:
isParallelin interfaceBaseStream<T extends XdmItem,Stream<T extends XdmItem>>
-
sequential
public Stream<T> sequential()
- Specified by:
sequentialin interfaceBaseStream<T extends XdmItem,Stream<T extends XdmItem>>
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBaseStream<T extends XdmItem,Stream<T extends XdmItem>>
-
asXdmValue
public XdmValue 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
public List<T> 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
public List<XdmNode> 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
public 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 nodeClassCastException- 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:
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
public 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:
ClassCastException- if the stream contains an item that is not an atomic value
-
asOptionalAtomic
public 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 itemClassCastException- 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:
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
public Optional<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 itemUnsupportedOperationException- if the stream contains an item that has no string value, for example a function item
-
asString
public 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:
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
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()
-
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.
-
at
public 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(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(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.
-
-