Package no.digipost.stream
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.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)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)booleanisParallel()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)static <T> NonEmptyStream<T>of(T singleElement)static <T> NonEmptyStream<T>of(T firstElement, Stream<T> remainingElements)static <T> NonEmptyStream<T>of(T firstElement, T... remainingElements)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)
-
of
@SafeVarargs public static <T> NonEmptyStream<T> of(T firstElement, T... remainingElements)
-
of
public static <T> NonEmptyStream<T> of(T firstElement, Stream<T> remainingElements)
-
of
public static <T> NonEmptyStream<T> of(Supplier<? extends T> firstElement, Stream<T> remainingElements)
-
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>
-
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()
-
-