Package org.keycloak.utils
Class StreamsUtil
java.lang.Object
org.keycloak.utils.StreamsUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Stream<Collection<T>>chunkedStream(Stream<T> originalStream, int chunkSize) A Java stream utility that splits a stream into chunks of a fixed size.static <T> Stream<T>Returns the original stream that is closed on terminating operation.static <T> Predicate<T>distinctByKey(Function<? super T, ?> keyExtractor) distinctByKey is not supposed to be used with parallel streams To make this method synchronized useConcurrentHashMap<Object, Boolean>instead of HashSetstatic <T> Stream<T>paginatedStream(Stream<T> originalStream, Integer first, Integer max) static <T> Stream<T>prepareSortedStreamToWorkInsideOfFlatMapWithTerminalOperations(Stream<T> originalStream) This works around a bug in JDK 21 (but no longer in JDK 24) where a sorted stream has all its elements processed when used inside of a flatmap and a terminal operation like limit() is used outside of it.static <T> Stream<T>throwIfEmpty(Stream<T> stream, RuntimeException ex) Returns the original stream if the stream is not empty.
-
Constructor Details
-
StreamsUtil
public StreamsUtil()
-
-
Method Details
-
closing
Returns the original stream that is closed on terminating operation. It is used, for example, for closing hibernate provided streams since it is required by hibernate documentation.- Parameters:
stream- the stream which is expected to be closed on termination- Returns:
- stream that will be closed on terminating operation
-
throwIfEmpty
Returns the original stream if the stream is not empty. Otherwise throws the provided exception.- Parameters:
stream- Stream to be examined.ex- Exception to be thrown if the stream is empty.- Returns:
- Stream
-
paginatedStream
Returns the original stream that is limited withskipandlimitfunctions based on values offirstandmaxparameters.- Type Parameters:
T- Type of items in the stream- Parameters:
originalStream- Stream to be limited.first- Index of first item to be returned by the stream. Ignored if negative, zeronull.max- Maximum number of items to be returned by the stream. Ignored if negative ornull.- Returns:
- Stream
-
distinctByKey
distinctByKey is not supposed to be used with parallel streams To make this method synchronized useConcurrentHashMap<Object, Boolean>instead of HashSet -
chunkedStream
A Java stream utility that splits a stream into chunks of a fixed size. Last chunk in the stream might be smaller than the desired size. Ordering guarantees depend on underlying stream.- Type Parameters:
T- The type of the stream- Parameters:
originalStream- The original streamchunkSize- The chunk size- Returns:
- The stream in chunks
-
prepareSortedStreamToWorkInsideOfFlatMapWithTerminalOperations
public static <T> Stream<T> prepareSortedStreamToWorkInsideOfFlatMapWithTerminalOperations(Stream<T> originalStream) This works around a bug in JDK 21 (but no longer in JDK 24) where a sorted stream has all its elements processed when used inside of a flatmap and a terminal operation like limit() is used outside of it. See StreamUtilTests.testSortedInsideOfFlatMapShouldRespectTerminalOperation for an example. Possible JDK-8196106 as the reference to the bug.- Type Parameters:
T- The type of the stream- Parameters:
originalStream- The original stream- Returns:
- The stream that is lazily evaluating
-