Package no.digipost
Class DiggCollectors
- java.lang.Object
-
- no.digipost.DiggCollectors
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> EmptyResultIfEmptySourceCollector<T,OneTimeAssignment<T>,T>allowAtMostOne()This is a collector for accessing the expected singular only element of aStream, as it will throw an exception if more than one element is processed.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 aStream, as it will throw the exception yielded from the given function if more than one element is processed.static <X extends Throwable>
Collector<Throwable,?,X>asSuppressedExceptionsOf(X exception)Add exceptions assuppressedexception to a given exception.static <T1,T2>
Collector<ViewableAsTuple<T1,Optional<T2>>,?,Map<T1,List<T2>>>toMultimap()A multimap maps from keys to lists, and this collector will arrangetuplesby putting each distinctfirst tuple-elementas keys of the resulting map, mapping them to aList, and adding eachsecond tuple-elementto the list.static <T,V>
Collector<T,?,Map<T,List<V>>>toMultimap(Function<? super T,Optional<V>> extractor)static <T,K,V>
Collector<T,?,Map<K,List<V>>>toMultimap(Function<? super T,K> keyExtractor, Function<? super T,Optional<V>> extractor)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 onefirstvalue and a List of values as thesecondvalue, and this collector will collecttupleswhere 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.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 onefirstvalue and a List of values as thesecondvalue, and this collector will collecttupleswhere 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.static <T> EmptyResultIfEmptySourceCollector<T,?,NonEmptyList<T>>toNonEmptyList()Collect element(s) to aNonEmptyList.static <X extends Throwable>
EmptyResultIfEmptySourceCollector<X,?,X>toSingleExceptionWithSuppressed()Collapse exceptions by taking the first (if any) and add every exception after the first assuppressedto the first one.
-
-
-
Method Detail
-
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 onefirstvalue and a List of values as thesecondvalue, and this collector will collecttupleswhere 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, aConflictingElementEncounteredis thrown.- Type Parameters:
T1- The type of the first tuple element, which will also become the type of the first element of the resultingTuple.T2- The type of the second tuple element, which will become theList<T2>type of the second element of the resultingTuple.- 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 onefirstvalue and a List of values as thesecondvalue, and this collector will collecttupleswhere 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 givenexceptionOnNonDistinctFirstElementfunction is thrown.- Type Parameters:
T1- The type of the first tuple element, which will also become the type of the first element of the resultingTuple.T2- The type of the second tuple element, which will become theList<T2>type of the second element of the resultingTuple.- Parameters:
exceptionOnNonDistinctFirstElement- the function will be given the already collected multituple as its first argument and the unexpected conflicting tuple with non-distinctfirstvalue 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 arrangetuplesby putting each distinctfirst tuple-elementas keys of the resulting map, mapping them to aList, and adding eachsecond tuple-elementto the list.- Type Parameters:
T1- The type of the first tuple element, which will become the key type of the resultingMap.T2- The type of the second tuple element, which will become theList<T2>value type of the resultingMap.- 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 aStream, as it will throw an exception if more than one element is processed. This should be used in preference of theStream.findFirst()orStream.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 aStream, 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:
allowAtMostOne()
-
asSuppressedExceptionsOf
public static <X extends Throwable> Collector<Throwable,?,X> asSuppressedExceptionsOf(X exception)
Add exceptions assuppressedexception 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 assuppressedto the first one.- Returns:
- the collector
-
toNonEmptyList
public static <T> EmptyResultIfEmptySourceCollector<T,?,NonEmptyList<T>> toNonEmptyList()
Collect element(s) to aNonEmptyList. If this collector is used with aNonEmptyStream, the resulting list will be directly yielded by thecollectoperation, otherwise with regularstreams, the result will be anOptional<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 resultingNonEmptyList- Returns:
- the collector
-
-