Package no.digipost

Class DiggCollectors

java.lang.Object
no.digipost.DiggCollectors

public final class DiggCollectors extends Object
Various Collector implementations.
  • Method Details

    • toMultituple

      public static <T1, T2> Collector<ViewableAsTuple<T1,Optional<T2>>,?,Optional<Tuple<T1,List<T2>>>> toMultituple()
      A multituple is similar to a multimap in that it consists of one first value and a List of values as the second value, and this collector will collect tuples where it is expected that all the first tuple-elements are equal, and re-arrange them by putting the distinct first element into a new Tuple, and collate each of the second elements into a new List which is set as the second element of the new Tuple. If non-distinct values of the first elements of the tuples are collected, a ConflictingElementEncountered is thrown.
      Type Parameters:
      T1 - The type of the first tuple element, which will also become the type of the first element of the resulting Tuple.
      T2 - The type of the second tuple element, which will become the List<T2> type of the second element of the resulting Tuple.
      Returns:
      the multituple collector.
    • toMultitupleOrThrowIfNonDistinct

      public static <T1, T2> Collector<ViewableAsTuple<T1,Optional<T2>>,?,Optional<Tuple<T1,List<T2>>>> toMultitupleOrThrowIfNonDistinct(BiFunction<? super Tuple<T1,List<T2>>,? super Tuple<T1,Optional<T2>>,? extends RuntimeException> exceptionOnNonDistinctFirstElement)
      A multituple is similar to a multimap in that it consists of one first value and a List of values as the second value, and this collector will collect tuples where it is expected that all the first tuple-elements are equal, and re-arrange them by putting the distinct first element into a new Tuple, and collate each of the second elements into a new List which is set as the second element of the new Tuple. If non-distinct values of the first elements of the tuples are collected, the exception returned from the given exceptionOnNonDistinctFirstElement function is thrown.
      Type Parameters:
      T1 - The type of the first tuple element, which will also become the type of the first element of the resulting Tuple.
      T2 - The type of the second tuple element, which will become the List<T2> type of the second element of the resulting Tuple.
      Parameters:
      exceptionOnNonDistinctFirstElement - the function will be given the already collected multituple as its first argument and the unexpected conflicting tuple with non-distinct first value as the second, which may be used to construct an exception to be thrown.
      Returns:
      the multituple collector.
    • toMultimap

      public static <T1, T2> Collector<ViewableAsTuple<T1,Optional<T2>>,?,Map<T1,List<T2>>> toMultimap()
      A multimap maps from keys to lists, and this collector will arrange tuples by putting each distinct first tuple-element as keys of the resulting map, mapping them to a List, and adding each second tuple-element to the list.
      Type Parameters:
      T1 - The type of the first tuple element, which will become the key type of the resulting Map.
      T2 - The type of the second tuple element, which will become the List<T2> value type of the resulting Map.
      Returns:
      the multimap collector.
    • toMultimap

      public static <T, V> Collector<T,?,Map<T,List<V>>> toMultimap(Function<? super T,Optional<V>> extractor)
    • toMultimap

      public static <T, K, V> Collector<T,?,Map<K,List<V>>> toMultimap(Function<? super T,K> keyExtractor, Function<? super T,Optional<V>> extractor)
    • allowAtMostOne

      public static <T> EmptyResultIfEmptySourceCollector<T,OneTimeAssignment<T>,T> allowAtMostOne()
      This is a collector for accessing the expected singular only element of a Stream, as it will throw an exception if more than one element is processed. This should be used in preference of the Stream.findFirst() or Stream.findAny() when it is imperative that the stream indeed yields a maximum of one single element, and any more elements is considered a programming error.
      Returns:
      the collector
    • allowAtMostOneOrElseThrow

      public static <T> EmptyResultIfEmptySourceCollector<T,OneTimeAssignment<T>,T> allowAtMostOneOrElseThrow(BiFunction<? super T,? super T,? extends RuntimeException> exceptionOnExcessiveElements)
      This is a collector for accessing the expected singular only element of a Stream, as it will throw the exception yielded from the given function if more than one element is processed.
      Parameters:
      exceptionOnExcessiveElements - the function will be given the first element yielded from the stream as its first argument and the unexpected excess one as the second, which may be used to construct an exception to be thrown.
      Returns:
      the collector
      See Also:
    • asSuppressedExceptionsOf

      public static <X extends Throwable> Collector<Throwable,?,X> asSuppressedExceptionsOf(X exception)
      Add exceptions as suppressed exception to a given exception.
      Parameters:
      exception - The exception to add the suppressed exceptions to.
      Returns:
      the collector
    • toSingleExceptionWithSuppressed

      public static <X extends Throwable> EmptyResultIfEmptySourceCollector<X,?,X> toSingleExceptionWithSuppressed()
      Collapse exceptions by taking the first (if any) and add every exception after the first as suppressed to the first one.
      Returns:
      the collector
    • toNonEmptyList

      public static <T> EmptyResultIfEmptySourceCollector<T,?,NonEmptyList<T>> toNonEmptyList()
      Collect element(s) to a NonEmptyList. If this collector is used with a NonEmptyStream, the resulting list will be directly yielded by the collect operation, otherwise with regular streams, the result will be an Optional<NonEmptyList>, which can be appropriately handled in the event of a non-empty list being impossible to contruct because there are no elements available to collect.
      Type Parameters:
      T - the type of elements contained in the resulting NonEmptyList
      Returns:
      the collector