T
- iterated type@Beta @PublicAPI public interface CloseableIterator<T> extends java.util.Iterator<T>, java.io.Closeable
IOException
potentially thrown from a delegate Closeable.close()
is expected to be wrapped in to an
IllegalStateException
.
For providing an implementation one can use CloseableIterators.wrap(java.util.Iterator)
,
CloseableIterators.wrap(java.util.Iterator, java.io.Closeable)
or .
Please note that this interface is marked with @Beta annotation, so it can be subject of incompatible changes or removal in later releases.
Modifier and Type | Method and Description |
---|---|
default void |
close() |
default CloseableIterator<T> |
forward(long maxItems)
Forward this instance by discarding items.
|
default CloseableIterator<T> |
limit(long limit)
Return a view limiting the returned element count.
|
default CloseableIterator<T> |
limit(long limit,
java.util.function.Consumer<java.lang.Long> afterLimitReachedOrNull)
Return a view limiting the returned element count.
|
default <U> CloseableIterator<U> |
transform(java.util.function.Function<T,U> transformation)
Apply a transformation of the elements of this Iterator.
|
default <U> CloseableIterator<U> |
transformIterator(java.util.function.Function<java.util.Iterator<T>,java.util.Iterator<U>> processor)
Apply a transformation on this Iterator.
|
default <U> CloseableIterator<U> |
transformMultipleElements(java.util.function.Function<java.util.Iterator<T>,U> constructNext)
Transform multiple elements into a single element.
|
default void close() throws java.lang.IllegalStateException
close
in interface java.lang.AutoCloseable
close
in interface java.io.Closeable
java.lang.IllegalStateException
default <U> CloseableIterator<U> transform(java.util.function.Function<T,U> transformation)
U
- Transformed typetransformation
- Transformation to apply on returned elementsdefault CloseableIterator<T> forward(long maxItems)
maxItems
- Item count to discard.this
instancedefault CloseableIterator<T> limit(long limit)
limit
- Max number of elements returned by the viewdefault CloseableIterator<T> limit(long limit, java.util.function.Consumer<java.lang.Long> afterLimitReachedOrNull)
limit
- Max number of elements returned by the viewafterLimitReachedOrNull
- Optional callback to invoke when limit reached.
Will be invoked from Iterator.hasNext()
the first time it returns false
.
The role of this callback is to optionally sign that reading elements stopped
because of reaching the specified limit. When no
call made to Iterator.hasNext()
after reaching this limit no callback invocation
will be made. Also when when Iterator.next()
invoked after reaching the limit
it will throw a NoSuchElementException
but no callback invocation will be
made.default <U> CloseableIterator<U> transformIterator(java.util.function.Function<java.util.Iterator<T>,java.util.Iterator<U>> processor)
U
- Type of transformed elementsprocessor
- Function to call on this instancedefault <U> CloseableIterator<U> transformMultipleElements(java.util.function.Function<java.util.Iterator<T>,U> constructNext)
U
- Type of transformed elementsconstructNext
- Function to create next element