Class CustomCollectors

java.lang.Object
dev.metaschema.core.util.CustomCollectors

public final class CustomCollectors extends Object
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 distinctByKey
 
 Stream<Item> uniqueItems = items.stream()
     .collect(CustomCollectors.distinctByKey(Item::getId));
 

 // Example 2: Using toSequence
 
 ISequence<IItem> sequence = items.stream()
     .collect(CustomCollectors.toSequence());
 
 
Since:
1.0.0
  • Method Details

    • identity

      @NonNull public static <T> Function<T,T> identity()
      An implementation of Function.identity() that respects non-nullness.
      Type Parameters:
      T - the Java type of the identity object
      Returns:
      the identity function
    • joiningWithOxfordComma

      public static Collector<CharSequence,?,String> joiningWithOxfordComma(@NonNull String conjunction)
      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 provided keyMapper. 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 streams
      K - the key type
      Parameters:
      stream - the stream to reduce
      keyMapper - 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 provided keyMapper. When a duplicate key is encountered, the provided duplicateHandler is 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 streams
      K - the key type
      Parameters:
      stream - the stream to reduce
      keyMapper - the key function to use to find unique items
      duplicateHander - used to determine which of two duplicates to keep
      Returns:
      a new stream
    • toMap

      @NonNull public static <T, K, V> Collector<T,?,Map<K,V>> toMap(@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 type
      K - the map key Java type
      V - the map value Java type
      Parameters:
      keyMapper - the function used to produce the map's key based on the provided item
      valueMapper - the function used to produce the map's value based on the provided item
      duplicateHander - the handler used to manage duplicate key insertion
      Returns:
      the collector
    • toMap

      @NonNull public static <T, K, V, M extends Map<K, V>> Collector<T,?,M> toMap(@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 type
      K - the map key Java type
      V - the map value Java type
      M - the Java type of the resulting map
      Parameters:
      keyMapper - the function used to produce the map's key based on the provided item
      valueMapper - the function used to produce the map's value based on the provided item
      duplicateHander - the handler used to manage duplicate key insertion
      supplier - the supplier used to create the resulting map
      Returns:
      the collector
    • useFirstMapper

      @NonNull public static <T> BinaryOperator<T> useFirstMapper()
      A binary operator that will always use the first of two values.
      Type Parameters:
      T - the item type
      Returns:
      the operator
    • useLastMapper

      @NonNull public static <T> BinaryOperator<T> 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,?,ISequence<ITEM_TYPE>> toSequence()
      A Collector implementation 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