Package com.complexible.common.base
Class Streams
- java.lang.Object
-
- com.complexible.common.base.Streams
-
public final class Streams extends java.lang.Object
Utility for streams
- Since:
- 4.0
- Version:
- 4.0
- Author:
- Michael Grove
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Streams.CheckedBiConsumer<U,T>
static interface
Streams.CheckedBiFunction<In1,In2,Out>
static interface
Streams.CheckedConsumer<T>
static interface
Streams.CheckedFunction<In,Out>
static interface
Streams.CheckedPredicate<T>
static interface
Streams.CheckedRunnable
static interface
Streams.CheckedSupplier<Out>
static class
Streams.UncheckedException
Exception indicating that Checked* threw an exception and the default handler was used
-
Field Summary
Fields Modifier and Type Field Description static boolean
NOT_PARALLEL
static boolean
PARALLEL
static long
UNKNOWN_SIZE
-
Constructor Summary
Constructors Constructor Description Streams()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <Input,Output>
OutputapplyTerminalAndClose(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>
voidforEachChecked(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 aStream
ofoptionals
into aStream
of the references they hold.static <T> java.util.stream.Stream<T>
stream(java.lang.Iterable<T> theIterable)
Return a newStream
from the givenIterable
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)
-
-
-
Field Detail
-
NOT_PARALLEL
public static final boolean NOT_PARALLEL
- See Also:
- Constant Field Values
-
PARALLEL
public static final boolean PARALLEL
- See Also:
- Constant Field Values
-
UNKNOWN_SIZE
public static final long UNKNOWN_SIZE
- See Also:
- Constant Field Values
-
-
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 aStream
ofoptionals
into aStream
of the references they hold. To be used withStream.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 newStream
from the givenIterable
- 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 streamtheFinisher
- 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()
-
-