java.lang.Object
dev.metaschema.core.util.CustomCollectors
Provides a variety of collector and other stream utilities.
This class provides thread-safe collectors and utilities for stream operations. Common usage patterns include:
// Example 1: Using distinctByKeyStream<Item> uniqueItems = items.stream() .collect(CustomCollectors.distinctByKey(Item::getId));// Example 2: Using toSequenceISequence<IItem> sequence = items.stream() .collect(CustomCollectors.toSequence());
- Since:
- 1.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA handler that supports resolving duplicate keys while inserting values into a map. -
Method Summary
Modifier and TypeMethodDescriptionstatic <V,K> Stream<V> distinctByKey(Stream<V> stream, Function<? super V, ? extends K> keyMapper) Produce a new stream with duplicates removed based on the providedkeyMapper.static <V,K> Stream<V> distinctByKey(Stream<V> stream, Function<? super V, ? extends K> keyMapper, CustomCollectors.DuplicateHandler<K, V> duplicateHander) Produce a new stream with duplicates removed based on the providedkeyMapper.static <T> Function<T,T> identity()An implementation ofFunction.identity()that respects non-nullness.static Collector<CharSequence,?, String> joiningWithOxfordComma(String conjunction) Joins a sequence of string values using oxford-style serial commas.toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, CustomCollectors.DuplicateHandler<K, V> duplicateHander) Produces a map collector that uses the provided key and value mappers, and a duplicate hander to manage duplicate key insertion.toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, CustomCollectors.DuplicateHandler<K, V> duplicateHander, Supplier<M> supplier) Produces a map collector that uses the provided key and value mappers, and a duplicate hander to manage duplicate key insertion.ACollectorimplementation to generates a sequence from a stream of Metapath items.static <T> BinaryOperator<T>A binary operator that will always use the first of two values.static <T> BinaryOperator<T>A binary operator that will always use the second of two values.
-
Method Details
-
identity
An implementation ofFunction.identity()that respects non-nullness.- Type Parameters:
T- the Java type of the identity object- Returns:
- the identity function
-
joiningWithOxfordComma
Joins a sequence of string values using oxford-style serial commas.- Parameters:
conjunction- the conjunction to use after the penultimate comma (e.g., and, or)- Returns:
- a collector that will perform the joining
-
distinctByKey
public static <V,K> Stream<V> distinctByKey(@NonNull Stream<V> stream, @NonNull Function<? super V, ? extends K> keyMapper) Produce a new stream with duplicates removed based on the providedkeyMapper. When a duplicate key is encountered, the second item is used. The original sequencing is preserved if the input stream is sequential.Note: This method uses an underlying map that is not thread safe to maintain insertion order.
- Type Parameters:
V- the item value for the streamsK- the key type- Parameters:
stream- the stream to reducekeyMapper- the key function to use to find unique items- Returns:
- a new stream
-
distinctByKey
public static <V,K> Stream<V> distinctByKey(@NonNull Stream<V> stream, @NonNull Function<? super V, ? extends K> keyMapper, @NonNull CustomCollectors.DuplicateHandler<K, V> duplicateHander) Produce a new stream with duplicates removed based on the providedkeyMapper. When a duplicate key is encountered, the providedduplicateHandleris used to determine which item to keep. The original sequencing is preserved if the input stream is sequential.Note: This method uses an underlying map that is not thread safe to maintain insertion order.
- Type Parameters:
V- the item value for the streamsK- the key type- Parameters:
stream- the stream to reducekeyMapper- the key function to use to find unique itemsduplicateHander- used to determine which of two duplicates to keep- Returns:
- a new stream
-
toMap
@NonNull public static <T,K, Collector<T,V> ?, toMapMap<K, V>> (@NonNull Function<? super T, ? extends K> keyMapper, @NonNull Function<? super T, ? extends V> valueMapper, @NonNull CustomCollectors.DuplicateHandler<K, V> duplicateHander) Produces a map collector that uses the provided key and value mappers, and a duplicate hander to manage duplicate key insertion.- Type Parameters:
T- the item Java typeK- the map key Java typeV- the map value Java type- Parameters:
keyMapper- the function used to produce the map's key based on the provided itemvalueMapper- the function used to produce the map's value based on the provided itemduplicateHander- the handler used to manage duplicate key insertion- Returns:
- the collector
-
toMap
@NonNull public static <T,K, Collector<T,V, M extends Map<K, V>> ?, toMapM> (@NonNull Function<? super T, ? extends K> keyMapper, @NonNull Function<? super T, ? extends V> valueMapper, @NonNull CustomCollectors.DuplicateHandler<K, V> duplicateHander, Supplier<M> supplier) Produces a map collector that uses the provided key and value mappers, and a duplicate hander to manage duplicate key insertion.- Type Parameters:
T- the item Java typeK- the map key Java typeV- the map value Java typeM- the Java type of the resulting map- Parameters:
keyMapper- the function used to produce the map's key based on the provided itemvalueMapper- the function used to produce the map's value based on the provided itemduplicateHander- the handler used to manage duplicate key insertionsupplier- the supplier used to create the resulting map- Returns:
- the collector
-
useFirstMapper
A binary operator that will always use the first of two values.- Type Parameters:
T- the item type- Returns:
- the operator
-
useLastMapper
A binary operator that will always use the second of two values.- Type Parameters:
T- the item type- Returns:
- the operator
-
toSequence
@NonNull public static <ITEM_TYPE extends IItem> Collector<ITEM_TYPE,?, toSequence()ISequence<ITEM_TYPE>> ACollectorimplementation to generates a sequence from a stream of Metapath items.- Type Parameters:
ITEM_TYPE- the Java type of the items- Returns:
- a collector that will generate a sequence
-