Class NonEmptyStream<T>
- java.lang.Object
-
- no.digipost.stream.NonEmptyStream<T>
-
- Type Parameters:
T- the type of the stream elements
- All Implemented Interfaces:
AutoCloseable,BaseStream<T,Stream<T>>,Stream<T>
public class NonEmptyStream<T> extends Object implements Stream<T>
A stream which is guarantied to produce at least one element. It provides some extensions to the generalStreamAPI for processing the stream knowing it is non-empty, including, but not limited to:first()andany()limitToNonEmpty(long)collect(EmptyResultIfEmptySourceCollector)reduceFromFirst(BinaryOperator)
NonEmptyStream, such as: This selection may be extended in the future. For operations which can not guarantie the result will still be a non-empty stream, such asfilterandlimit, will yield a regularStream.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallMatch(Predicate<? super T> predicate)Tany()Return any element from the stream.booleananyMatch(Predicate<? super T> predicate)voidclose()<R> Rcollect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)<R,A>
Rcollect(Collector<? super T,A,R> collector)<A,R>
Rcollect(EmptyResultIfEmptySourceCollector<? super T,A,R> collector)Collect the stream elements by using aEmptyResultIfEmptySourceCollector.static <T> NonEmptyStream<T>concat(Stream<? extends T> a, NonEmptyStream<? extends T> b)Create a stream by concatenating a regularStreamfollowed by aNonEmptyStream, in the same manner asStream.concat(Stream, Stream).static <T> NonEmptyStream<T>concat(NonEmptyStream<? extends T> a, Stream<? extends T> b)Create a stream by concatenating aNonEmptyStreamfollowed by a regularStream, in the same manner asStream.concat(Stream, Stream).static <T> NonEmptyStream<T>concat(NonEmptyStream<? extends T> a, NonEmptyStream<? extends T> b)Create a stream by concatenating two non-empty streams, in the same manner asStream.concat(Stream, Stream).longcount()NonEmptyStream<T>distinct()Stream<T>filter(Predicate<? super T> predicate)Optional<T>findAny()Optional<T>findFirst()Tfirst()Return the first element of the stream.<R> Stream<R>flatMap(Function<? super T,? extends Stream<? extends R>> mapper)<R> NonEmptyStream<R>flatMap(ToNonEmptyStreamFunction<? super T,? extends R> mapper)Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.DoubleStreamflatMapToDouble(Function<? super T,? extends DoubleStream> mapper)IntStreamflatMapToInt(Function<? super T,? extends IntStream> mapper)LongStreamflatMapToLong(Function<? super T,? extends LongStream> mapper)voidforEach(Consumer<? super T> action)voidforEachOrdered(Consumer<? super T> action)static <T> NonEmptyStream<T>generate(Supplier<T> s)Create the same stream as produced byStream.generate(Supplier), but typed asNonEmptyStream.booleanisParallel()static <T> NonEmptyStream<T>iterate(T seed, UnaryOperator<T> f)Create the same stream as produced byStream.iterate(Object, UnaryOperator), but typed asNonEmptyStream.Iterator<T>iterator()Stream<T>limit(long maxSize)NonEmptyStream<T>limitToNonEmpty(long maxSizeMoreThanZero)Returns a stream consisting of the elements of this stream, truncated to be no longer thanmaxSizeMoreThanZeroin length.<R> NonEmptyStream<R>map(Function<? super T,? extends R> mapper)DoubleStreammapToDouble(ToDoubleFunction<? super T> mapper)IntStreammapToInt(ToIntFunction<? super T> mapper)LongStreammapToLong(ToLongFunction<? super T> mapper)Optional<T>max(Comparator<? super T> comparator)Optional<T>min(Comparator<? super T> comparator)booleannoneMatch(Predicate<? super T> predicate)static <T> NonEmptyStream<T>of(Supplier<? extends T> firstElement, Stream<T> remainingElements)Create a non-empty stream where the first element is resolved from aSupplier, and remaining elements are provided from another stream.static <T> NonEmptyStream<T>of(T singleElement)Create a non-empty stream containing a single element.static <T> NonEmptyStream<T>of(T firstElement, Stream<T> remainingElements)Create a non-empty stream whose elements are a given first value, and remaining elements are provided from another stream.static <T> NonEmptyStream<T>of(T firstElement, T... remainingElements)Create a non-empty stream whose elements are the specified values.NonEmptyStream<T>onClose(Runnable closeHandler)NonEmptyStream<T>parallel()NonEmptyStream<T>peek(Consumer<? super T> action)Optional<T>reduce(BinaryOperator<T> accumulator)Treduce(T identity, BinaryOperator<T> accumulator)<U> Ureduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)TreduceFromFirst(BinaryOperator<T> accumulator)Performs a reduction on the elements of this stream, using the first element of the stream (guarantied to be available) and an associative accumulation function, and returns the reduced value.NonEmptyStream<T>sequential()Stream<T>skip(long n)NonEmptyStream<T>sorted()NonEmptyStream<T>sorted(Comparator<? super T> comparator)Spliterator<T>spliterator()Object[]toArray()<A> A[]toArray(IntFunction<A[]> generator)NonEmptyStream<T>unordered()
-
-
-
Method Detail
-
of
public static <T> NonEmptyStream<T> of(T singleElement)
Create a non-empty stream containing a single element.- Type Parameters:
T- the type of the single element in the stream- Parameters:
singleElement- the element- Returns:
- the new singleton non-empty stream
-
of
@SafeVarargs public static <T> NonEmptyStream<T> of(T firstElement, T... remainingElements)
Create a non-empty stream whose elements are the specified values.- Type Parameters:
T- the type of stream elements- Parameters:
firstElement- the first elementremainingElements- the remaining elements after the first- Returns:
- the new non-empty stream
-
of
public static <T> NonEmptyStream<T> of(T firstElement, Stream<T> remainingElements)
Create a non-empty stream whose elements are a given first value, and remaining elements are provided from another stream.- Type Parameters:
T- the type of stream elements- Parameters:
firstElement- the first elementremainingElements- the remaining elements after the first- Returns:
- the new non-empty stream
-
of
public static <T> NonEmptyStream<T> of(Supplier<? extends T> firstElement, Stream<T> remainingElements)
Create a non-empty stream where the first element is resolved from aSupplier, and remaining elements are provided from another stream.- Type Parameters:
T- the type of stream elements- Parameters:
firstElement- the supplier of the first elementremainingElements- the remaining elements after the first- Returns:
- the new non-empty stream
-
concat
public static <T> NonEmptyStream<T> concat(NonEmptyStream<? extends T> a, Stream<? extends T> b)
Create a stream by concatenating aNonEmptyStreamfollowed by a regularStream, in the same manner asStream.concat(Stream, Stream). The resulting stream is also non-empty.- Type Parameters:
T- The type of stream elements- Parameters:
a- the first stream, non-emptyb- the second stream- Returns:
- the concatenation of the two input streams
- See Also:
Stream.concat(Stream, Stream)
-
concat
public static <T> NonEmptyStream<T> concat(Stream<? extends T> a, NonEmptyStream<? extends T> b)
Create a stream by concatenating a regularStreamfollowed by aNonEmptyStream, in the same manner asStream.concat(Stream, Stream). The resulting stream is also non-empty.- Type Parameters:
T- The type of stream elements- Parameters:
a- the first streamb- the second stream, non-empty- Returns:
- the concatenation of the two input streams
- See Also:
Stream.concat(Stream, Stream)
-
concat
public static <T> NonEmptyStream<T> concat(NonEmptyStream<? extends T> a, NonEmptyStream<? extends T> b)
Create a stream by concatenating two non-empty streams, in the same manner asStream.concat(Stream, Stream). The resulting stream is also non-empty.This method overload is needed to avoid ambiguity with
concat(NonEmptyStream, Stream)andconcat(Stream, NonEmptyStream)when concatenating two non-empty streams.- Type Parameters:
T- The type of stream elements- Parameters:
a- the first non-empty streamb- the second non-empty stream- Returns:
- the concatenation of the two input streams
- See Also:
Stream.concat(Stream, Stream)
-
iterate
public static <T> NonEmptyStream<T> iterate(T seed, UnaryOperator<T> f)
Create the same stream as produced byStream.iterate(Object, UnaryOperator), but typed asNonEmptyStream.- Type Parameters:
T- the type of stream elements- Parameters:
seed- the initial elementf- a function to be applied to to the previous element to produce a new element- Returns:
- the new infinite non-empty stream
- See Also:
Stream.iterate(Object, UnaryOperator)
-
generate
public static <T> NonEmptyStream<T> generate(Supplier<T> s)
Create the same stream as produced byStream.generate(Supplier), but typed asNonEmptyStream.- Type Parameters:
T- the type of stream elements- Parameters:
s- theSupplierof generated elements- Returns:
- a new infinite non-empty stream
- See Also:
Stream.generate(Supplier)
-
collect
public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
-
collect
public <A,R> R collect(EmptyResultIfEmptySourceCollector<? super T,A,R> collector)
Collect the stream elements by using aEmptyResultIfEmptySourceCollector.This is an extension to the general
StreamAPI, as a non-empty stream can always produce a value without needing a provided initial "identity" value.- Parameters:
collector- theCollectordescribing the reduction- Returns:
- the result from collecting the elements
- See Also:
EmptyResultIfEmptySourceCollector
-
map
public <R> NonEmptyStream<R> map(Function<? super T,? extends R> mapper)
-
spliterator
public Spliterator<T> spliterator()
- Specified by:
spliteratorin interfaceBaseStream<T,Stream<T>>
-
isParallel
public boolean isParallel()
- Specified by:
isParallelin interfaceBaseStream<T,Stream<T>>
-
sequential
public NonEmptyStream<T> sequential()
- Specified by:
sequentialin interfaceBaseStream<T,Stream<T>>
-
parallel
public NonEmptyStream<T> parallel()
- Specified by:
parallelin interfaceBaseStream<T,Stream<T>>
-
unordered
public NonEmptyStream<T> unordered()
- Specified by:
unorderedin interfaceBaseStream<T,Stream<T>>
-
onClose
public NonEmptyStream<T> onClose(Runnable closeHandler)
- Specified by:
onClosein interfaceBaseStream<T,Stream<T>>
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBaseStream<T,Stream<T>>
-
mapToInt
public IntStream mapToInt(ToIntFunction<? super T> mapper)
-
mapToLong
public LongStream mapToLong(ToLongFunction<? super T> mapper)
-
mapToDouble
public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
- Specified by:
mapToDoublein interfaceStream<T>
-
flatMap
public <R> NonEmptyStream<R> flatMap(ToNonEmptyStreamFunction<? super T,? extends R> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.This is an extension to the general
StreamAPI, as flat-mapping to non-empty streams will preserve the non-empty guarantee of the stream.- Type Parameters:
R- The element type of the new stream- Parameters:
mapper- a non-interfering, stateless function to apply to each element which produces a stream of new values- Returns:
- the new non-empty stream
-
flatMapToInt
public IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
- Specified by:
flatMapToIntin interfaceStream<T>
-
flatMapToLong
public LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
- Specified by:
flatMapToLongin interfaceStream<T>
-
flatMapToDouble
public DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
- Specified by:
flatMapToDoublein interfaceStream<T>
-
distinct
public NonEmptyStream<T> distinct()
-
sorted
public NonEmptyStream<T> sorted()
-
sorted
public NonEmptyStream<T> sorted(Comparator<? super T> comparator)
-
peek
public NonEmptyStream<T> peek(Consumer<? super T> action)
-
limitToNonEmpty
public NonEmptyStream<T> limitToNonEmpty(long maxSizeMoreThanZero)
Returns a stream consisting of the elements of this stream, truncated to be no longer thanmaxSizeMoreThanZeroin length.- Parameters:
maxSizeMoreThanZero- the number of elements the stream should be limited to, must be 1 or greater (>= 1)- Returns:
- the new non-empty stream
- Throws:
IllegalArgumentException- if the givenmaxSizeMoreThanZerois 0 or less- See Also:
limit(long)
-
forEachOrdered
public void forEachOrdered(Consumer<? super T> action)
- Specified by:
forEachOrderedin interfaceStream<T>
-
toArray
public <A> A[] toArray(IntFunction<A[]> generator)
-
reduce
public T reduce(T identity, BinaryOperator<T> accumulator)
-
reduceFromFirst
public T reduceFromFirst(BinaryOperator<T> accumulator)
Performs a reduction on the elements of this stream, using the first element of the stream (guarantied to be available) and an associative accumulation function, and returns the reduced value.This is an extension to the general
StreamAPI, as a non-empty stream can always produce a value without needing a provided initial "identity" value.- Parameters:
accumulator- an associative, non-interfering, stateless function for combining two values- Returns:
- the result of the reduction
- See Also:
reduce(Object, BinaryOperator)
-
reduce
public Optional<T> reduce(BinaryOperator<T> accumulator)
-
reduce
public <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
-
min
public Optional<T> min(Comparator<? super T> comparator)
-
max
public Optional<T> max(Comparator<? super T> comparator)
-
first
public T first()
Return the first element of the stream.This is an extension to the general
StreamAPI, as a non-empty stream is guarantied to always have at least one value.- Returns:
- the first value of the stream
- See Also:
findFirst()
-
findFirst
public Optional<T> findFirst()
As this is a non-empty stream, this method always returns a non-empty
Optionalwith the first value of the stream.
-
any
public T any()
Return any element from the stream.This is an extension to the general
StreamAPI, as a non-empty stream is guarantied to always have at least one value.- Returns:
- any value from the stream
- See Also:
findAny()
-
-