All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.util.Closeables Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.commons.util;

import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * 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
 */
public class Closeables {
   private Closeables() { }

   /**
    * 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.
    * @param iterator The closeable iterator to change to a spliterator
    * @param size The approximate size of the iterator.  If no size is known {@link Long#MAX_VALUE} should be passed
    * @param characteristics The characteristics of the given spliterator as defined on the {@link Spliterator}
    *                        interface
    * @param  The type that is the same between the iterator and spliterator
    * @return A spliterator that when closed will close the provided iterator
    */
   public static  CloseableSpliterator spliterator(CloseableIterator iterator, long size,
                                                  int characteristics) {
      return new CloseableIteratorAsCloseableSpliterator<>(iterator, size, characteristics);
   }

   /**
    * Creates a closeable spliterator from the given spliterator that does nothing when close is called.
    * @param spliterator The spliterator to wrap to allow it to become a closeable spliterator.
    * @param  The type of the spliterators
    * @return A spliterator that does nothing when closed
    */
   public static  CloseableSpliterator spliterator(Spliterator spliterator) {
      return new SpliteratorAsCloseableSpliterator<>(spliterator);
   }

   /**
    * Creates a closeable spliterator that when closed will close the underlying stream as well
    * @param stream The stream to change into a closeable spliterator
    * @param  The type of the stream
    * @return A spliterator that when closed will also close the underlying stream
    */
   public static  CloseableSpliterator spliterator(Stream stream) {
      return new StreamToCloseableSpliterator<>(stream);
   }

   /**
    * Creates a closeable iterator that when closed will close the underlying stream as well
    * @param stream The stream to change into a closeable iterator
    * @param  The type of the stream
    * @return An iterator that when closed will also close the underlying stream
    */
   public static  CloseableIterator iterator(Stream stream) {
      return new StreamToCloseableIterator<>(stream);
   }

   /**
    * Creates a closeable iterator from the given iterator that does nothing when close is called.
    * @param iterator The iterator to wrap to allow it to become a closeable iterator
    * @param  The type of the iterators
    * @return An iterator that does nothing when closed
    */
   public static  CloseableIterator iterator(Iterator iterator) {
      return new IteratorAsCloseableIterator<>(iterator);
   }

   private static class IteratorAsCloseableIterator implements CloseableIterator {
      private final Iterator iterator;

      public IteratorAsCloseableIterator(Iterator iterator) {
         this.iterator = iterator;
      }

      @Override
      public void close() {
         // This does nothing
      }

      @Override
      public boolean hasNext() {
         return iterator.hasNext();
      }

      @Override
      public E next() {
         return iterator.next();
      }

      @Override
      public void remove() {
         iterator.remove();
      }
   }

   private static class SpliteratorAsCloseableSpliterator implements CloseableSpliterator {
      private final Spliterator spliterator;

      public SpliteratorAsCloseableSpliterator(Spliterator spliterator) {
         this.spliterator = spliterator;
      }

      @Override
      public void close() {

      }

      @Override
      public boolean tryAdvance(Consumer action) {
         return spliterator.tryAdvance(action);
      }

      @Override
      public Spliterator trySplit() {
         return spliterator.trySplit();
      }

      @Override
      public long estimateSize() {
         return spliterator.estimateSize();
      }

      @Override
      public int characteristics() {
         return spliterator.characteristics();
      }
   }

   private static class CloseableIteratorAsCloseableSpliterator extends SpliteratorAsCloseableSpliterator {
      private final CloseableIterator iterator;

      CloseableIteratorAsCloseableSpliterator(CloseableIterator iterator, long size, int characteristics) {
         super(Spliterators.spliterator(iterator, size, characteristics));
         this.iterator = iterator;
      }

      @Override
      public void close() {
         this.iterator.close();
      }
   }

   private static class StreamToCloseableIterator extends IteratorAsCloseableIterator {
      private final Stream stream;

      public StreamToCloseableIterator(Stream stream) {
         super(stream.iterator());
         this.stream = stream;
      }

      @Override
      public void close() {
         stream.close();
      }
   }

   private static class StreamToCloseableSpliterator extends SpliteratorAsCloseableSpliterator {
      private final Stream stream;

      public StreamToCloseableSpliterator(Stream stream) {
         super(stream.spliterator());
         this.stream = stream;
      }

      @Override
      public void close() {
         stream.close();
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy