Class Streams


  • public final class Streams
    extends java.lang.Object

    Utility for streams

    Since:
    4.0
    Version:
    4.0
    Author:
    Michael Grove
    • Constructor Summary

      Constructors 
      Constructor Description
      Streams()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <Input,​Output>
      Output
      applyTerminalAndClose​(java.util.stream.Stream<Input> theStream, java.util.function.Function<java.util.stream.Stream<Input>,​Output> theFinisher)
      Apply the given terminal operation to the stream returning the result and close the stream.
      static <T> java.util.stream.Stream<T> concat​(java.util.stream.Stream<T> theStream, java.util.stream.Stream<T> theOtherStream, java.util.stream.Stream<T>... theMoreStreams)  
      static <T> java.util.function.Predicate<? super T> distinctByKey​(java.util.function.Function<? super T,​java.lang.Object> theKey)
      A simple stateful predicate for filtering out stream elements which agree on their key values.
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​java.util.HashMap<K,​V>>
      entriesToMap()  
      static <T> CloseableIterator<T> flatIterator​(java.util.stream.Stream<java.util.stream.Stream<T>> theStream)  
      static <T> java.util.stream.Stream<T> flatten​(java.util.stream.Stream<java.util.stream.Stream<T>> theStream)  
      static <T,​E extends java.lang.Throwable>
      void
      forEachChecked​(java.util.stream.Stream<T> theStream, Streams.CheckedConsumer<T> theConsumer)  
      static <T> java.util.function.Function<java.util.Optional<T>,​java.util.stream.Stream<T>> optional()
      Returns a function that will collapse a Stream of optionals into a Stream of the references they hold.
      static <T> java.util.stream.Stream<T> stream​(java.lang.Iterable<T> theIterable)
      Return a new Stream from the given Iterable
      static <T> java.util.stream.Stream<T> stream​(java.util.Iterator<? extends T> theIter)  
      static <T> java.util.stream.Stream<T> stream​(java.util.Iterator<? extends T> theIter, long theSize)  
      static <In1,​In2,​Out>
      java.util.function.BiFunction<In1,​In2,​Out>
      unchecked​(Streams.CheckedBiFunction<In1,​In2,​Out> theBiFunction)  
      static <In1,​In2,​Out>
      java.util.function.BiFunction<In1,​In2,​Out>
      unchecked​(Streams.CheckedBiFunction<In1,​In2,​Out> theBiFunction, java.util.function.Consumer<java.lang.Throwable> theHandler)  
      static <In,​Out>
      java.util.function.Function<In,​Out>
      unchecked​(Streams.CheckedFunction<In,​Out> theFunction)  
      static <In,​Out>
      java.util.function.Function<In,​Out>
      unchecked​(Streams.CheckedFunction<In,​Out> theFunction, java.util.function.Consumer<java.lang.Throwable> theHandler)  
      static <U,​T>
      java.util.function.BiConsumer<U,​T>
      uncheckedConsumer​(Streams.CheckedBiConsumer<U,​T> theConsumer)  
      static <U,​T>
      java.util.function.BiConsumer<U,​T>
      uncheckedConsumer​(Streams.CheckedBiConsumer<U,​T> theConsumer, java.util.function.Consumer<java.lang.Throwable> theHandler)  
      static <T> java.util.function.Consumer<T> uncheckedConsumer​(Streams.CheckedConsumer<T> theConsumer)  
      static <T> java.util.function.Consumer<T> uncheckedConsumer​(Streams.CheckedConsumer<T> theConsumer, java.util.function.Consumer<java.lang.Throwable> theHandler)  
      static <In> java.util.function.Predicate<In> uncheckedPredicate​(Streams.CheckedPredicate<In> thePredicate)  
      static <In> java.util.function.Predicate<In> uncheckedPredicate​(Streams.CheckedPredicate<In> thePredicate, java.util.function.Consumer<java.lang.Throwable> theHandler)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Streams

        public Streams()
    • Method Detail

      • optional

        public static <T> java.util.function.Function<java.util.Optional<T>,​java.util.stream.Stream<T>> optional()
        Returns a function that will collapse a Stream of optionals into a Stream of the references they hold. To be used with Stream.flatMap(Function).
        Type Parameters:
        T - the optional's type
        Returns:
        a Function which unrolls optionals
      • stream

        public static <T> java.util.stream.Stream<T> stream​(java.lang.Iterable<T> theIterable)
        Return a new Stream from the given Iterable
        Parameters:
        theIterable - the iterable
        Returns:
        a stream over the iterable
      • stream

        public static <T> java.util.stream.Stream<T> stream​(java.util.Iterator<? extends T> theIter)
      • stream

        public static <T> java.util.stream.Stream<T> stream​(java.util.Iterator<? extends T> theIter,
                                                            long theSize)
      • concat

        @SafeVarargs
        public static <T> java.util.stream.Stream<T> concat​(java.util.stream.Stream<T> theStream,
                                                            java.util.stream.Stream<T> theOtherStream,
                                                            java.util.stream.Stream<T>... theMoreStreams)
      • applyTerminalAndClose

        public static <Input,​Output> Output applyTerminalAndClose​(java.util.stream.Stream<Input> theStream,
                                                                        java.util.function.Function<java.util.stream.Stream<Input>,​Output> theFinisher)
        Apply the given terminal operation to the stream returning the result and close the stream.
        Parameters:
        theStream - the stream
        theFinisher - the function which will apply the terminal operation and return the result
        Returns:
        the result of the terminal operation.
      • forEachChecked

        public static <T,​E extends java.lang.Throwable> void forEachChecked​(java.util.stream.Stream<T> theStream,
                                                                                  Streams.CheckedConsumer<T> theConsumer)
                                                                           throws E extends java.lang.Throwable
        Throws:
        E extends java.lang.Throwable
      • unchecked

        public static <In,​Out> java.util.function.Function<In,​Out> unchecked​(Streams.CheckedFunction<In,​Out> theFunction)
      • unchecked

        public static <In,​Out> java.util.function.Function<In,​Out> unchecked​(Streams.CheckedFunction<In,​Out> theFunction,
                                                                                         java.util.function.Consumer<java.lang.Throwable> theHandler)
      • unchecked

        public static <In1,​In2,​Out> java.util.function.BiFunction<In1,​In2,​Out> unchecked​(Streams.CheckedBiFunction<In1,​In2,​Out> theBiFunction)
      • unchecked

        public static <In1,​In2,​Out> java.util.function.BiFunction<In1,​In2,​Out> unchecked​(Streams.CheckedBiFunction<In1,​In2,​Out> theBiFunction,
                                                                                                                 java.util.function.Consumer<java.lang.Throwable> theHandler)
      • uncheckedPredicate

        public static <In> java.util.function.Predicate<In> uncheckedPredicate​(Streams.CheckedPredicate<In> thePredicate)
      • uncheckedPredicate

        public static <In> java.util.function.Predicate<In> uncheckedPredicate​(Streams.CheckedPredicate<In> thePredicate,
                                                                               java.util.function.Consumer<java.lang.Throwable> theHandler)
      • uncheckedConsumer

        public static <T> java.util.function.Consumer<T> uncheckedConsumer​(Streams.CheckedConsumer<T> theConsumer)
      • uncheckedConsumer

        public static <U,​T> java.util.function.BiConsumer<U,​T> uncheckedConsumer​(Streams.CheckedBiConsumer<U,​T> theConsumer)
      • uncheckedConsumer

        public static <U,​T> java.util.function.BiConsumer<U,​T> uncheckedConsumer​(Streams.CheckedBiConsumer<U,​T> theConsumer,
                                                                                             java.util.function.Consumer<java.lang.Throwable> theHandler)
      • uncheckedConsumer

        public static <T> java.util.function.Consumer<T> uncheckedConsumer​(Streams.CheckedConsumer<T> theConsumer,
                                                                           java.util.function.Consumer<java.lang.Throwable> theHandler)
      • flatIterator

        public static <T> CloseableIterator<T> flatIterator​(java.util.stream.Stream<java.util.stream.Stream<T>> theStream)
      • flatten

        public static <T> java.util.stream.Stream<T> flatten​(java.util.stream.Stream<java.util.stream.Stream<T>> theStream)
      • distinctByKey

        public static <T> java.util.function.Predicate<? super T> distinctByKey​(java.util.function.Function<? super T,​java.lang.Object> theKey)
        A simple stateful predicate for filtering out stream elements which agree on their key values. TODO let the outer code create more optimized distinct filters e.g. for value types.
      • entriesToMap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​java.util.HashMap<K,​V>> entriesToMap()