- See Also:
-
ExtendedIterator
ClosableIterator
Iter
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <X,
C extends Collection<X>>
CPuts all the remaining items of the given iterator into thecollection
, and returns this collection itself.static <X> boolean
Returns whether all elements of the given iterator match the provided predicate.static <X> boolean
Returns whether any elements of the given iterator match the provided predicate.static <X> Spliterator<X>
asSpliterator
(Iterator<? extends X> iterator, long size, int characteristics) Creates aSpliterator
using a givenIterator
as the source of elements.static <X> Stream<X>
Creates a new sequentialStream
from the givenIterator
, which is expected to deliver nonnull items: it is required that the operationIterator.next()
must not returnnull
if the methodIterator.hasNext()
answerstrue
.static <X> Stream<X>
Constructs a new sequentialStream
from the givenIterator
, with the specifiedcharacteristics
.static <X> Stream<X>
Constructs a new sequentialStream
from the givenIterator
, with the specifiedcharacteristics
and estimatedsize
.static <X> Stream<X>
asStream
(org.apache.jena.util.iterator.ExtendedIterator<? extends X> iterator) Creates a new sequentialStream
from the givenExtendedIterator
.static void
Closes iterator if it isCloseableIterator
.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
concat
(org.apache.jena.util.iterator.ExtendedIterator<? extends X>... iterators) Creates a lazily concatenatedExtended Iterator
whose elements are all the elements of the given iterators.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
concat
(org.apache.jena.util.iterator.ExtendedIterator<? extends X> a, org.apache.jena.util.iterator.ExtendedIterator<? extends X> b) Creates a lazily concatenatedExtended Iterator
whose elements are all the elements of the first iterator followed by all the elements of the second iterator.static long
Returns the count of elements in the given iterator.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
create
(Collection<? extends X> members) Creates a newExtended Iterator
} over all elements of the specified collection.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
Creates a newExtended Iterator
over all elements of an iterator which will be created by theprovider
on first iteration.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
Answers anExtendedIterator
returning the elements of the specifiediterator
.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
distinct
(org.apache.jena.util.iterator.ExtendedIterator<X> base) Returns anExtended Iterator
consisting of the distinct elements (according toObject.equals(Object)
) of the given iterator.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
Returns an extended iterator consisting of the elements of the specified extended iterator that match the given predicate.static <X> Optional<X>
Returns anOptional
describing the first element of the iterator, or an emptyOptional
if the iterator is empty.static <T,
F> org.apache.jena.util.iterator.ExtendedIterator<T> flatMap
(org.apache.jena.util.iterator.ExtendedIterator<F> base, Function<? super F, ? extends Iterator<? extends T>> mapper) Returns anExtended Iterator
consisting of the results of replacing each element of the givenbase
iterator with the contents of a mapped iterator produced by applying the provided mapping function (map
) to each element.static <X> void
PerformsforEach
.static <X> Stream<X>
Creates aStream
for a futureSet
, which is produced by the factory-parametergetAsSet
.static boolean
hasAtLeast
(Iterator<?> iterator, int n) Answerstrue
iff the given iterator has more thann
or equal ton
elements.static boolean
hasExactly
(Iterator<?> iterator, int n) Answerstrue
iff the given iterator has exactlyn
elements.static <X> boolean
Returns whether no elements of the given iterator match the provided predicate.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
of()
Creates a newExtended Iterator
} containing nothing.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
of
(X item) Creates a newExtended Iterator
} containing a single specified element.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
of
(X... members) Creates a newExtended Iterator
} containing the specified elements.static <X> org.apache.jena.util.iterator.ExtendedIterator<X>
Returns anExtended Iterator
consisting of the elements of the givenbase
iterator, additionally performing the providedaction
on each element as elements are consumed from the resulting iterator.static <X> Set<X>
Takes the specified number of items from the source iterator.static <X,
K, V, M extends Map<K, V>>
MtoMap
(Iterator<X> iterator, Function<? super X, ? extends K> keyMapper, Function<? super X, ? extends V> valueMapper, BinaryOperator<V> mergeFunction, Supplier<M> mapSupplier) Returns aMap
(of the type ofM
) whose keys and values are the result of applying the provided mapping functions to the input elements.
-
Constructor Details
-
Iterators
public Iterators()
-
-
Method Details
-
asStream
public static <X> Stream<X> asStream(org.apache.jena.util.iterator.ExtendedIterator<? extends X> iterator) Creates a new sequentialStream
from the givenExtendedIterator
. Takes care about degenerate cases empty and single-element iterator.- Type Parameters:
X
- anything- Parameters:
iterator
-ExtendedIterator
ofX
-elements- Returns:
- a
Stream
ofX
- See Also:
-
asStream
Creates a new sequentialStream
from the givenIterator
, which is expected to deliver nonnull items: it is required that the operationIterator.next()
must not returnnull
if the methodIterator.hasNext()
answerstrue
. The methodasStream(Iterator, int)
with the second parameter equals0
can be used to create aStream
for an iterator that may delivernull
s.If the given parameter is
ClosableIterator
, remember to callBaseStream.close()
explicitly if the iterator is not exhausted (i.e. in caseIterator.hasNext()
is stilltrue
). It should be done for all short-circuiting terminal operations such asStream.findFirst()
,Stream.findAny()
,Stream.anyMatch(Predicate)
etc.- Type Parameters:
X
- the type of iterator-items- Parameters:
iterator
-Iterator
that delivers nonnull elements, cannot benull
- Returns:
Stream
-
asStream
Constructs a new sequentialStream
from the givenIterator
, with the specifiedcharacteristics
. If the given parameter isClosableIterator
, an explicit call to theBaseStream.close()
method is required for all short-circuiting terminal operations.- Type Parameters:
X
- the type of iterator-items- Parameters:
iterator
-Iterator
, theSpliterator
's source, notnull
characteristics
-int
, characteristics of theSpliterator
's source- Returns:
- a non-parallel
Stream
, that wraps theiterator
with the given characteristics
-
asStream
public static <X> Stream<X> asStream(Iterator<? extends X> iterator, long size, int characteristics) Constructs a new sequentialStream
from the givenIterator
, with the specifiedcharacteristics
and estimatedsize
. If the given parameter isClosableIterator
, an explicit call to theBaseStream.close()
method is required for all short-circuiting terminal operations.- Type Parameters:
X
- the type of iterator-items- Parameters:
iterator
-Iterator
, theSpliterator
's source, notnull
size
-long
, aSpliterator
's estimates size, positive number or-1
characteristics
-int
, characteristics of theSpliterator
's source- Returns:
- a non-parallel
Stream
, that wraps theiterator
with the given parameters
-
asSpliterator
public static <X> Spliterator<X> asSpliterator(Iterator<? extends X> iterator, long size, int characteristics) Creates aSpliterator
using a givenIterator
as the source of elements. If thesize
is not-1
, the returnedSpliterator
will report this number as the initialestimated size
.- Type Parameters:
X
- the type of iterator-items- Parameters:
iterator
-Iterator
, notnull
size
-long
, a positive number or-1
characteristics
-int
, characteristics of the spliterator's source- Returns:
Spliterator
- Throws:
NullPointerException
- if the given iterator isnull
-
fromSet
Creates aStream
for a futureSet
, which is produced by the factory-parametergetAsSet
. The producedSet
must not change and must not containnull
.- Type Parameters:
X
- the type of items- Parameters:
getAsSet
-Supplier
that produces aSet
ofX
- Returns:
- distinct sequential
Stream
- See Also:
-
flatMap
public static <T,F> org.apache.jena.util.iterator.ExtendedIterator<T> flatMap(org.apache.jena.util.iterator.ExtendedIterator<F> base, Function<? super F, ? extends Iterator<? extends T>> mapper) Returns anExtended Iterator
consisting of the results of replacing each element of the givenbase
iterator with the contents of a mapped iterator produced by applying the provided mapping function (map
) to each element. A functional equivalent ofStream.flatMap(Function)
, but forExtendedIterator
s.- Type Parameters:
F
- the element type of the base iterator (from)T
- the element type of the new iterator (to)- Parameters:
base
-ExtendedIterator
with elements of typeF
mapper
-Function
map-function with Object of type ofF
(or any super type) as an input, and anIterator
of typeT
(or any extended type) as an output- Returns:
- new
ExtendedIterator
of typeF
-
concat
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> concat(org.apache.jena.util.iterator.ExtendedIterator<? extends X> a, org.apache.jena.util.iterator.ExtendedIterator<? extends X> b) Creates a lazily concatenatedExtended Iterator
whose elements are all the elements of the first iterator followed by all the elements of the second iterator. A functional equivalent ofStream.concat(Stream, Stream)
, but forExtendedIterator
s.- Type Parameters:
X
- the type of iterator elements- Parameters:
a
- the first iteratorb
- the second iterator- Returns:
- the concatenation of the two input iterators
- See Also:
-
concat
@SafeVarargs public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> concat(org.apache.jena.util.iterator.ExtendedIterator<? extends X>... iterators) Creates a lazily concatenatedExtended Iterator
whose elements are all the elements of the given iterators. If the specified array has length equals2
, than this method is equivalent to the methodconcat(ExtendedIterator, ExtendedIterator)
). AnExtendedIterator
-based functional equivalent of the expressionStream#of(Stream, ..., Stream).flatMap(Function.identity())
.- Type Parameters:
X
- the type of iterator elements- Parameters:
iterators
- Array of iterators- Returns:
- all input elements as a single
ExtendedIterator
of typeX
- See Also:
-
filter
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> filter(org.apache.jena.util.iterator.ExtendedIterator<X> iterator, Predicate<? super X> predicate) Returns an extended iterator consisting of the elements of the specified extended iterator that match the given predicate. A functional equivalent ofStream.filter(Predicate)
, but forExtendedIterator
s.- Type Parameters:
X
- the element type of the input and output iterators- Parameters:
iterator
-ExtendedIterator
with elements of typeX
predicate
-Predicate
to apply to elements of the iterator- Returns:
- a new iterator
-
peek
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> peek(org.apache.jena.util.iterator.ExtendedIterator<X> base, Consumer<? super X> action) Returns anExtended Iterator
consisting of the elements of the givenbase
iterator, additionally performing the providedaction
on each element as elements are consumed from the resulting iterator. A functional equivalent ofStream.peek(Consumer)
, but forExtendedIterator
s.- Type Parameters:
X
- the element type of the input and output iterators- Parameters:
base
-ExtendedIterator
with elements of typeX
action
-Consumer
action- Returns:
- new
ExtendedIterator
of typeX
-
distinct
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> distinct(org.apache.jena.util.iterator.ExtendedIterator<X> base) Returns anExtended Iterator
consisting of the distinct elements (according toObject.equals(Object)
) of the given iterator. A functional equivalent ofStream.distinct()
, but forExtendedIterator
s. Warning: the result is temporary stored in memory!- Type Parameters:
X
- the element type of the input and output iterators- Parameters:
base
-ExtendedIterator
with elements of typeX
- Returns:
- new
ExtendedIterator
of typeX
without duplicates
-
anyMatch
Returns whether any elements of the given iterator match the provided predicate. A functional equivalent ofStream.anyMatch(Predicate)
, but forIterator
s. -
allMatch
Returns whether all elements of the given iterator match the provided predicate. A functional equivalent ofStream.allMatch(Predicate)
, but forIterator
s.- Type Parameters:
X
- the element type of the iterator- Parameters:
iterator
-Iterator
with elements of typeX
predicate
-Predicate
to apply to elements of the iterator- Returns:
true
if either all elements of the iterator match the provided predicate or the iterator is empty, otherwisefalse
- See Also:
-
noneMatch
Returns whether no elements of the given iterator match the provided predicate. A functional equivalent ofStream.noneMatch(Predicate)
, but forIterator
s.- Type Parameters:
X
- the element type of the iterator- Parameters:
iterator
-Iterator
with elements of typeX
predicate
-Predicate
to apply to elements of the iterator- Returns:
true
if either no elements of the iterator match the provided predicate or the iterator is empty, otherwisefalse
- See Also:
-
findFirst
Returns anOptional
describing the first element of the iterator, or an emptyOptional
if the iterator is empty. A functional equivalent ofStream.findFirst()
, but forIterator
s. Warning: the method closes the specified iterator, so it is not possible to reuse it after calling this method.- Type Parameters:
X
- the element type of the iterator- Parameters:
iterator
-Iterator
, notnull
- Returns:
Optional
ofX
- Throws:
NullPointerException
- if the element selected isnull
-
count
Returns the count of elements in the given iterator. A functional equivalent ofStream.count()
, but forIterator
s. Warning: the method closes the specified iterator, so it is not possible to reuse it after.- Parameters:
iterator
-Iterator
, notnull
- Returns:
- long, the count of elements in the given
iterator
-
hasAtLeast
Answerstrue
iff the given iterator has more thann
or equal ton
elements.- Parameters:
n
- positive numberiterator
-Iterator
, notnull
- Returns:
true
if the specified iterator has at leastn
elements
-
hasExactly
Answerstrue
iff the given iterator has exactlyn
elements.- Parameters:
n
- positive numberiterator
-Iterator
, notnull
- Returns:
true
if the specified iterator has exactlyn
elements
-
forEach
PerformsforEach
. On finish iteration, the iterator will be closed. -
addAll
Puts all the remaining items of the given iterator into thecollection
, and returns this collection itself. This is a terminal operation.- Type Parameters:
X
- the element type of the iterator, notnull
C
- theCollection
type, notnull
- Parameters:
source
- theIterator
with elements of typeX
target
- the collection of typeC
- Returns:
C
, the same instance as specified
-
takeAsSet
Takes the specified number of items from the source iterator. -
toMap
public static <X,K, M toMapV, M extends Map<K, V>> (Iterator<X> iterator, Function<? super X, ? extends K> keyMapper, Function<? super X, ? extends V> valueMapper, BinaryOperator<V> mergeFunction, Supplier<M> mapSupplier) Returns aMap
(of the type ofM
) whose keys and values are the result of applying the provided mapping functions to the input elements. A functional equivalent ofstream.collect(Collectors.toMap(...))
, but for plainIterator
s. This method makes no guarantees about synchronization or atomicity properties of it.- Type Parameters:
X
- the type of the input elementsK
- the output type of the key mapping functionV
- the output type of the value mapping functionM
- the type of the resultingMap
- Parameters:
iterator
- input elements in the form ofIterator
keyMapper
- a mapping function to produce keysvalueMapper
- a mapping function to produce valuesmergeFunction
- a merge function, used to resolve collisions between values associated with the same key, as supplied toMap.merge(Object, Object, BiFunction)
mapSupplier
- a function which returns new, emptyMap
into which the results will be inserted- Returns:
- a
Map
whose keys are the result of applying a key mapping function to the input elements, and whose values are the result of applying a value mapping function to all input elements equal to the key and combining them using the merge function - See Also:
-
close
Closes iterator if it isCloseableIterator
.- Parameters:
iterator
-Iterator
-
of
Creates a newExtended Iterator
} containing the specified elements.- Type Parameters:
X
- the element type of the new iterator- Parameters:
members
- Array of elements of the typeX
- Returns:
- a fresh
ExtendedIterator
instance
-
of
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> of()Creates a newExtended Iterator
} containing nothing.- Type Parameters:
X
- the element type of the new iterator- Returns:
- a fresh
ExtendedIterator
instance
-
of
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> of(X item) Creates a newExtended Iterator
} containing a single specified element.- Type Parameters:
X
- the element type of the new iterator- Parameters:
item
- - an object of typeX
- Returns:
- a fresh
ExtendedIterator
instance
-
create
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> create(Collection<? extends X> members) Creates a newExtended Iterator
} over all elements of the specified collection.- Type Parameters:
X
- the element type of the new iterator- Parameters:
members
-Collection
of elements of the typeX
- Returns:
- a fresh
ExtendedIterator
instance
-
create
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> create(Iterator<? extends X> iterator) Answers anExtendedIterator
returning the elements of the specifiediterator
. If the giveniterator
is itself anExtendedIterator
, return that; otherwise wrapiterator
.- Type Parameters:
X
- the element type of the iterator- Parameters:
iterator
-Iterator
, notnull
- Returns:
ExtendedIterator
instance
-
create
public static <X> org.apache.jena.util.iterator.ExtendedIterator<X> create(Supplier<Iterator<? extends X>> provider) Creates a newExtended Iterator
over all elements of an iterator which will be created by theprovider
on first iteration. The returned iterator does not contain any elements, but they will be derived at once when calling any of theExtendedIterator
methods.The idea is to provide a truly lazy iterator and, subsequently, a stream (through the
asStream(Iterator)
method). When any distinct operation (i.e.distinct(ExtendedIterator)
orStream.distinct()
) is used, it, in fact, collects on demand an in-memorySet
containing all elements, but it will be appeared in process and an iterator or a stream initially weighs nothing. This method allows achieving a similar behavior: when creating anExtendedIterator
does not weight anything, but it materializes itself when processing. Therefore, operations such as(stream-1 + stream-2).findFirst()
will demand less memory.The returned iterator is not thread-safe, just as like any other RDF extended iterator we work with.
-