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 anUnsatisfiedReductionException
ifelements
is empty, throws anAmbiguousReductionException
if the size ofelements
is greater than1
, and returns the sole element ofelements
otherwise.static <C,
T> Reducer <C, T> Returns aReducer
whosereduce(List, Object, BiFunction)
method, for a givenList
of elements, returns the result of invoking its supplied failure handler.static <C,
T> Reducer <C, T> ofSimple()
default T
Invokes 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 suppliedBiFunction
with 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 T
reduce
(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 suppliedSelectable
supplied withc
, and the suppliedc
and a reference to thefail(List, Object)
method, and returns the result.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 suppliedSelectable
supplied withc
, and the suppliedc
andfailureHandler
arguments, and returns the result.static <A,
B, C> C returnNull
(A a, B b) Returnsnull
when 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 suppliedBiFunction
with 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 immutableList
to 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 benull
to indicate no criteria; may be ignored if not needed by an implementationfailureHandler
- aBiFunction
receiving 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
- ifelements
orfailureHandler
isnull
ReductionException
- if thefailureHandler
function 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 suppliedSelectable
supplied withc
, and the suppliedc
andfailureHandler
arguments, and returns the result.- Parameters:
f
- aSelectable
; must not benull
c
- the criteria effectively describing the initial selection and the desired reduction; may benull
to indicate no criteria; may be ignored if not needed by an implementationfailureHandler
- aBiFunction
receiving 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 theList
returned from the suppliedSelectable
'sselect(Object)
method, or a synthetic value returned by an invocation of the suppliedfailureHandler
'sapply(Object, Object)
method - Throws:
NullPointerException
- iff
orfailureHandler
isnull
- See Also:
-
reduce
Invokes thereduce(List, Object, BiFunction)
method with the return value of an invocation of theselect(Object)
method on the suppliedSelectable
supplied withc
, and the suppliedc
and a reference to thefail(List, Object)
method, and returns the result.- Parameters:
f
- aSelectable
; must not benull
c
- the criteria effectively describing the initial selection and the desired reduction; may benull
to indicate no criteria; may be ignored if not needed by an implementation- Returns:
- a single, possibly
null
, element normally drawn or computed from theList
returned from the suppliedSelectable
'sselect(Object)
method, or the sole element returned by an invocation of thefail(List, Object)
method - Throws:
NullPointerException
- iff
orfailureHandler
isnull
UnsatisfiedReductionException
- if an invocation of thefail(List, Object)
method throws anUnsatisfiedReductionException
AmbiguousReductionException
- 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 immutableList
to 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 benull
to 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
- ifelements
isnull
- See Also:
-
ofSimple
Returns aReducer
whosereduce(List, Object, BiFunction)
method, for a givenList
of elements, returns theList
's sole element if theList
has exactly one element, returnsnull
if theList
is 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 aReducer
whosereduce(List, Object, BiFunction)
method, for a givenList
of 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 anUnsatisfiedReductionException
ifelements
is empty, throws anAmbiguousReductionException
if the size ofelements
is greater than1
, and returns the sole element ofelements
otherwise.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
- aList
of elements under reduction; must not benull
c
- a criteria object; may benull
- Returns:
- the sole element present in
elements
, which may benull
- Throws:
NullPointerException
- ifelements
isnull
UnsatisfiedReductionException
- ifelements
is emptyAmbiguousReductionException
- if the size ofelements
is greater than1
- See Also:
-
returnNull
Returnsnull
when 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:
null
when invoked
-