Package dev.bannmann.labs.core
Class StreamExtras
java.lang.Object
dev.bannmann.labs.core.StreamExtras
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends @Nullable Object>
BinaryOperator<T> Use withStream.reduce(BinaryOperator)to ensure there is at most one element.static <T extends @Nullable Object,E extends RuntimeException>
BinaryOperator<T> atMostOneThrowing(Supplier<E> exception) Use withStream.reduce(BinaryOperator)to ensure there is at most one element.lastN(int limit) Collects the last n input elements (or fewer) into a list.
-
Method Details
-
atMostOne
Use withStream.reduce(BinaryOperator)to ensure there is at most one element. Note that if you want an exception in case no element exists, you should instead use Guava'sMoreCollectors.onlyElement().- Type Parameters:
T- element type- Returns:
- an operator to use with
Stream.reduce(BinaryOperator)
-
atMostOneThrowing
public static <T extends @Nullable Object,E extends RuntimeException> BinaryOperator<T> atMostOneThrowing(Supplier<E> exception) Use withStream.reduce(BinaryOperator)to ensure there is at most one element. Note that if you want an exception in case no element exists, you should instead use Guava'sMoreCollectors.onlyElement().- Type Parameters:
T- element typeE- type of the exception to throw if more than one element is encountered- Parameters:
exception- thrown if more than one element is encountered
-
lastN
Collects the last n input elements (or fewer) into a list.- Type Parameters:
T- element type- Parameters:
limit- the maximum number of elements. Must be greater than 0.- Throws:
IllegalArgumentException- iflimitis 0 or negative
-