Class Collector<R>
- Type Parameters:
R
- the type of results
Constructions are not represented as function objects of type
Supplier<R>
but rather, by double inversion of control, as
objects of type Consumer<Consumer<R>>
. They are invoked
with a temporary functional object of type Consumer<R>
that
acts as a sink for results. The sink objects becomes
invalid after the results of a single construction have been
collected; no reference to it should escape.
For example, a construction that yields all elements of a given
list could be represented as sink -> { for(R elem : list)
sink.accept(r);}
, or more concisely as list::forEach
.
Sink objects do not accept null
results; a will be thrown.
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfindAllResults
(Consumer<Consumer<R>> construction) Find all results of the given construction.findFirstResult
(Consumer<Consumer<R>> construction) Find the first result of the given construction.findUniqueResult
(Consumer<Consumer<R>> construction) Find the unique result of the given construction.
-
Constructor Details
-
Collector
public Collector()
-
-
Method Details
-
findFirstResult
Find the first result of the given construction.The construction is aborted, by means of exception handling, immediately after yielding the first result. Any necessary cleanup operations must be done in
finally
blocks.- Parameters:
construction
- the construction- Returns:
- the first result yielded by the construction wrapped in
Optional.of
, orOptional.empty()
if there is none
-
findUniqueResult
public Optional<R> findUniqueResult(Consumer<Consumer<R>> construction) throws Collector.NonUniqueException Find the unique result of the given construction.- Parameters:
construction
- the construction- Returns:
- the first and only result yielded by the construction wrapped in
Optional.of
, orOptional.empty()
if there is none - Throws:
Collector.NonUniqueException
- if there is more than one result
-
findAllResults
Find all results of the given construction.- Parameters:
construction
- the construction- Returns:
- a list of all results, in the order they have been yielded
-