Class Closeables


  • public class Closeables
    extends Object
    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 Detail

      • 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 spliterator
        size - The approximate size of the iterator. If no size is known Long.MAX_VALUE should be passed
        characteristics - The characteristics of the given spliterator as defined on the Spliterator interface
        Returns:
        A spliterator that when closed will close the provided iterator
      • spliterator

        public static <T> CloseableSpliterator<T> spliterator​(Spliterator<T> 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

        public static <R> CloseableSpliterator<R> spliterator​(BaseStream<R,​Stream<R>> stream)
        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

        public static <R> CloseableIterator<R> iterator​(BaseStream<R,​Stream<R>> stream)
        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

        public static <E> CloseableIterator<E> iterator​(Iterator<? extends E> 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

        public static <E> Stream<E> stream​(CloseableSpliterator<E> spliterator,
                                           boolean parallel)
        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 close
        parallel - 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 close
        parallel - whether or not the returned stream is parallel or not
        size - the size of the iterator if known, otherwise Long.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 a Publisher to a CloseableIterator by utilizing items fetched into an array and refetched as they are consumed from the iterator. The iterator when closed will also close the underlying Subscription when subscribed to the publisher.
        Type Parameters:
        E - value type
        Parameters:
        publisher - the publisher to convert
        fetchSize - 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