Package org.infinispan.commons.util
Class Closeables
java.lang.Object
org.infinispan.commons.util.Closeables
This class consists exclusively of static methods that operate on or return closeable interfaces. This is helpful
when wanting to change a given interface to an appropriate closeable interface.
- Since:
- 8.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> CloseableIterator
<E> Creates a closeable iterator from the given iterator that does nothing when close is called.static <R> CloseableIterator
<R> iterator
(BaseStream<R, Stream<R>> stream) Creates a closeable iterator that when closed will close the underlying stream as wellstatic <E> CloseableIterator
<E> iterator
(org.reactivestreams.Publisher<E> publisher, int fetchSize) Converts aPublisher
to aCloseableIterator
by utilizing items fetched into an array and refetched as they are consumed from the iterator.static <T> CloseableSpliterator
<T> spliterator
(Spliterator<T> spliterator) Creates a closeable spliterator from the given spliterator that does nothing when close is called.static <R> CloseableSpliterator
<R> spliterator
(BaseStream<R, Stream<R>> stream) Creates a closeable spliterator that when closed will close the underlying stream as wellstatic <E> CloseableSpliterator
<E> spliterator
(CloseableIterator<? extends E> iterator, long size, int characteristics) Takes a provided closeable iterator and wraps it appropriately so it can be used as a closeable spliterator that will close the iterator when the spliterator is closed.static <E> Stream
<E> stream
(CloseableIterator<E> iterator, boolean parallel, long size, int characteristics) Creates a stream that when closed will also close the underlying iteratorstatic <E> Stream
<E> stream
(CloseableSpliterator<E> spliterator, boolean parallel) Creates a stream that when closed will also close the underlying spliterator
-
Method Details
-
spliterator
public static <E> CloseableSpliterator<E> spliterator(CloseableIterator<? extends E> iterator, long size, int characteristics) Takes a provided closeable iterator and wraps it appropriately so it can be used as a closeable spliterator that will close the iterator when the spliterator is closed.- Type Parameters:
E
- The type that is the same between the iterator and spliterator- Parameters:
iterator
- The closeable iterator to change to a spliteratorsize
- The approximate size of the iterator. If no size is knownLong.MAX_VALUE
should be passedcharacteristics
- The characteristics of the given spliterator as defined on theSpliterator
interface- Returns:
- A spliterator that when closed will close the provided iterator
-
spliterator
Creates a closeable spliterator from the given spliterator that does nothing when close is called.- Type Parameters:
T
- The type of the spliterators- Parameters:
spliterator
- The spliterator to wrap to allow it to become a closeable spliterator.- Returns:
- A spliterator that does nothing when closed
-
spliterator
Creates a closeable spliterator that when closed will close the underlying stream as well- Type Parameters:
R
- The type of the stream- Parameters:
stream
- The stream to change into a closeable spliterator- Returns:
- A spliterator that when closed will also close the underlying stream
-
iterator
Creates a closeable iterator that when closed will close the underlying stream as well- Type Parameters:
R
- The type of the stream- Parameters:
stream
- The stream to change into a closeable iterator- Returns:
- An iterator that when closed will also close the underlying stream
-
iterator
Creates a closeable iterator from the given iterator that does nothing when close is called.- Type Parameters:
E
- The type of the iterators- Parameters:
iterator
- The iterator to wrap to allow it to become a closeable iterator- Returns:
- An iterator that does nothing when closed
-
stream
Creates a stream that when closed will also close the underlying spliterator- Type Parameters:
E
- the type of the stream- Parameters:
spliterator
- spliterator to back the stream and subsequently closeparallel
- whether or not the returned stream is parallel or not- Returns:
- the stream to use
-
stream
public static <E> Stream<E> stream(CloseableIterator<E> iterator, boolean parallel, long size, int characteristics) Creates a stream that when closed will also close the underlying iterator- Type Parameters:
E
- the type of the stream- Parameters:
iterator
- iterator to back the stream and subsequently closeparallel
- whether or not the returned stream is parallel or notsize
- the size of the iterator if known, otherwiseLong.MAX_VALUE
should be passed.characteristics
- the characteristics of the iterator to be used- Returns:
- the stream to use
-
iterator
public static <E> CloseableIterator<E> iterator(org.reactivestreams.Publisher<E> publisher, int fetchSize) Converts aPublisher
to aCloseableIterator
by utilizing items fetched into an array and refetched as they are consumed from the iterator. The iterator when closed will also close the underlyingSubscription
when subscribed to the publisher.- Type Parameters:
E
- value type- Parameters:
publisher
- the publisher to convertfetchSize
- how many entries to hold in memory at once in preparation for the iterators consumption- Returns:
- an iterator that when closed will cancel the subscription
-