Class StreamUtils

java.lang.Object
org.aksw.commons.collections.utils.StreamUtils

public class StreamUtils extends Object
  • Constructor Details

    • StreamUtils

      public StreamUtils()
  • Method Details

    • iterable

      public static <T> Iterable<T> iterable(Stream<T> stream)
      Create a single-use Iterable from a stream. Allows for easier use of Streams in for-loops:
        Stream<T> stream = ...;
        for (T item : StreamUtils.iterableOf(stream)) {
        }
      
      Type Parameters:
      T -
      Parameters:
      stream -
      Returns:
    • expectOneItem

      public static <T> T expectOneItem(Stream<T> stream)
    • expectZeroOrOneItems

      public static <T> T expectZeroOrOneItems(Stream<T> stream)
    • streamToPairs

      public static <T> Stream<Map.Entry<T,T>> streamToPairs(Stream<T> stream)
    • mapToBatch

      public static <T> Stream<List<T>> mapToBatch(Stream<T> stream, int batchSize)
      Note we could implement another version where each batch's List is lazy loaded from the stream - but this would probably require complete consumption of each batch in order
      Parameters:
      stream -
      batchSize -
      Returns:
    • appendAction

      public static <T> Stream<T> appendAction(Stream<? extends T> stream, Runnable runnable)
      Creates a new stream which upon reaching its end performs and action. It concatenates the original stream with one having a single item that is filtered out again. The action is run as- part of the filter.
      Parameters:
      stream -
      runnable -
      Returns:
    • stream

      public static <S,X> Stream<X> stream(BiConsumer<S, Consumer<X>> fn, S baseSolution)
    • fromEnumerableResource

      public static <T,R,E> Stream<T> fromEnumerableResource(Callable<R> resourceSupplier, ThrowingFunction<? super R, E> toEnumerable, ThrowingFunction<? super E, T> nextRecord, BiPredicate<T, ? super E> hasEnded, ThrowingBiConsumer<? super R, ? super E> closer)
      Creates a stream over a resource via an enumerable. The resource is initialized lazily once the first item is read from the stream. The returned stream should be used in a try-with-resources block in order to close the underlying resource.
      Type Parameters:
      T - The record type (e.g. an array of Strings)
      R - The resource type (e.g. a java.sql.Connection)
      E - An enumerable (e.g. a java.sql.ResultSet)
      Parameters:
      resourceSupplier -
      toEnumerable -
      nextRecord -
      closer -
      Returns:
    • viaList

      public static <T> Stream<T> viaList(Stream<T> in, Consumer<List<T>> consumer)
    • println

      public static <T> Stream<T> println(Stream<T> in)