Interface Reducer<C,T>
- Type Parameters:
C- the type of criteriaT- the element type
- All Known Implementing Classes:
RankedReducer
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A functional interface whose implementations can either reduce a supplied
List of elements representing a successful selection to a single element normally drawn or
calculated from the selection according to some criteria, or fail gracefully in the face of ambiguity by
invoking a supplied failure handler.
The reduction may be a simple filtering operation, or may be a summing or aggregating operation, or anything else.
This interface is conceptually subordinate to, but should not be confused with, the Reducible
interface.
Reducer implementations are often used to help build Reducible implementations. See, for example,
Reducible.ofCaching(Selectable, Reducer, BiFunction).
- Author:
- Laird Nelson
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <C,T> T Throws anUnsatisfiedReductionExceptionifelementsis empty, throws anAmbiguousReductionExceptionif the size ofelementsis greater than1, and returns the sole element ofelementsotherwise.static <C,T> Reducer <C, T> Returns aReducerwhosereduce(List, Object, BiFunction)method, for a givenListof elements, returns the result of invoking its supplied failure handler.static <C,T> Reducer <C, T> ofSimple()default TInvokes thereduce(List, Object, BiFunction)method with the supplied arguments and a reference to thefail(List, Object)method, and returns the result.reduce(List<? extends T> elements, C c, BiFunction<? super List<? extends T>, ? super C, ? extends T> failureHandler) Performs some kind of reductive or filtering operation on the suppliedList, according to the supplied criteria, and returns the single result, or, if reduction fails, invokes the suppliedBiFunctionwith a sublist representing a partial reduction (or an empty list representing a reduction that simply could not be performed), along with the supplied criteria, and returns its result.default Treduce(Selectable<? super C, ? extends T> f, C c) Invokes thereduce(List, Object, BiFunction)method with the return value of an invocation of theselect(Object)method on the suppliedSelectablesupplied withc, and the suppliedcand a reference to thefail(List, Object)method, and returns the result.default Treduce(Selectable<? super C, ? extends T> f, C c, BiFunction<? super List<? extends T>, ? super C, ? extends T> failureHandler) Invokes thereduce(List, Object, BiFunction)method with the return value of an invocation of theselect(Object)method on the suppliedSelectablesupplied withc, and the suppliedcandfailureHandlerarguments, and returns the result.static <A,B, C> C returnNull(A a, B b) Returnsnullwhen invoked, regardless of arguments.
-
Method Details
-
reduce
T reduce(List<? extends T> elements, C c, BiFunction<? super List<? extends T>, ? super C, ? extends T> failureHandler) Performs some kind of reductive or filtering operation on the suppliedList, according to the supplied criteria, and returns the single result, or, if reduction fails, invokes the suppliedBiFunctionwith a sublist representing a partial reduction (or an empty list representing a reduction that simply could not be performed), along with the supplied criteria, and returns its result.Implementations of this method must return determinate values.
- Parameters:
elements- an immutableListto reduce; must not benull; represents a successful selection from a larger collection of elementsc- the criteria effectively describing the initial selection and the desired reduction; may benullto indicate no criteria; may be ignored if not needed by an implementationfailureHandler- aBiFunctionreceiving a failed reduction (usually a portion of the suppliedelements), and the selection and reduction criteria, that returns a substitute reduction (or, more commonly, throws an exception); must not benull; must be invoked if reduction fails or undefined behavior may result- Returns:
- a single, possibly
null, element normally drawn or computed from the suppliedelements, or a synthetic value returned by an invocation of the suppliedfailureHandler'sapply(Object, Object)method - Throws:
NullPointerException- ifelementsorfailureHandlerisnullReductionException- if thefailureHandlerfunction throws aReductionException- See Also:
-
reduce
default T reduce(Selectable<? super C, ? extends T> f, C c, BiFunction<? super List<? extends T>, ? super C, ? extends T> failureHandler) Invokes thereduce(List, Object, BiFunction)method with the return value of an invocation of theselect(Object)method on the suppliedSelectablesupplied withc, and the suppliedcandfailureHandlerarguments, and returns the result.- Parameters:
f- aSelectable; must not benullc- the criteria effectively describing the initial selection and the desired reduction; may benullto indicate no criteria; may be ignored if not needed by an implementationfailureHandler- aBiFunctionreceiving a failed reduction (usually a portion of the suppliedelements), and the selection and reduction criteria, that returns a substitute reduction (or, more commonly, throws an exception); must not benull; must be invoked if reduction fails or undefined behavior may result- Returns:
- a single, possibly
null, element normally drawn or computed from theListreturned from the suppliedSelectable'sselect(Object)method, or a synthetic value returned by an invocation of the suppliedfailureHandler'sapply(Object, Object)method - Throws:
NullPointerException- ifforfailureHandlerisnull- See Also:
-
reduce
Invokes thereduce(List, Object, BiFunction)method with the return value of an invocation of theselect(Object)method on the suppliedSelectablesupplied withc, and the suppliedcand a reference to thefail(List, Object)method, and returns the result.- Parameters:
f- aSelectable; must not benullc- the criteria effectively describing the initial selection and the desired reduction; may benullto indicate no criteria; may be ignored if not needed by an implementation- Returns:
- a single, possibly
null, element normally drawn or computed from theListreturned from the suppliedSelectable'sselect(Object)method, or the sole element returned by an invocation of thefail(List, Object)method - Throws:
NullPointerException- ifforfailureHandlerisnullUnsatisfiedReductionException- if an invocation of thefail(List, Object)method throws anUnsatisfiedReductionExceptionAmbiguousReductionException- if an invocation of thefail(List, Object)method throws anAmbiguousReductionException- See Also:
-
reduce
Invokes thereduce(List, Object, BiFunction)method with the supplied arguments and a reference to thefail(List, Object)method, and returns the result.- Parameters:
elements- an immutableListto reduce; must not benull; represents a successful selection from a larger collection of elementsc- the criteria effectively describing the initial selection and the desired reduction; may benullto indicate no criteria; may be ignored if not needed by an implementation- Returns:
- a single, possibly
null, element normally drawn or computed from the suppliedelements, or a synthetic value returned by an invocation of the suppliedfailureHandler'sapply(Object, Object)method - Throws:
NullPointerException- ifelementsisnull- See Also:
-
ofSimple
Returns aReducerwhosereduce(List, Object, BiFunction)method, for a givenListof elements, returns theList's sole element if theListhas exactly one element, returnsnullif theListis empty, and returns the result of invoking its supplied failure handler otherwise.- Type Parameters:
C- the type of the criteriaT- the type of the elements- Returns:
- a
Reducer; nevernull - See Also:
-
ofFailing
Returns aReducerwhosereduce(List, Object, BiFunction)method, for a givenListof elements, returns the result of invoking its supplied failure handler.- Type Parameters:
C- the type of the criteriaT- the type of the elements- Returns:
- a
Reducer; nevernull - See Also:
-
fail
Throws anUnsatisfiedReductionExceptionifelementsis empty, throws anAmbiguousReductionExceptionif the size ofelementsis greater than1, and returns the sole element ofelementsotherwise.A reference to this method is often used as a failure handler in an invocation of the
reduce(List, Object, BiFunction)method.- Type Parameters:
C- the type of the criteriaT- the type of the elements- Parameters:
elements- aListof elements under reduction; must not benullc- a criteria object; may benull- Returns:
- the sole element present in
elements, which may benull - Throws:
NullPointerException- ifelementsisnullUnsatisfiedReductionException- ifelementsis emptyAmbiguousReductionException- if the size ofelementsis greater than1- See Also:
-
returnNull
Returnsnullwhen invoked, regardless of arguments.A reference to this method can be used as a failure handler in an invocation of the
reduce(List, Object, BiFunction)method.- Type Parameters:
A- the type of the first parameter; ignoredB- the type of the second parameter; ignoredC- the type of the return type; ignored- Parameters:
a- an object; may benull; ignoredb- an object; may benull; ignored- Returns:
nullwhen invoked
-