Class LargestSliceCollector<E,N extends Number & Comparable<? super N>>
java.lang.Object
org.consensusj.analytics.util.collector.LargestSliceCollector<E,N>
- Type Parameters:
E
- The type of objectsN
- The subclass ofNumber
to use, must beComparable
- All Implemented Interfaces:
Collector<E,
LargestSliceAccumulator<E, N>, LargestSliceList<E, N>>
public class LargestSliceCollector<E,N extends Number & Comparable<? super N>>
extends Object
implements Collector<E,LargestSliceAccumulator<E,N>,LargestSliceList<E,N>>
Collector that collects the
n
objects containing the "largest slices" of an input stream of
type <E>
. Each object must have a numeric "slice size" of type <N>
determined by a sliceSizeExtractor
provided to the constructor. The result is a LargestSliceList
containing the n
largest slices
(LargestSliceList.getSliceList()
) and the sum of the slice sizes (LargestSliceList.getTotalOther()
) of
the filtered/excluded objects. The idea is the results can easily be converted to a Pie Chart.
The objects streamed must be unique, non-null, and a sliceSizeExtractor
must be provided that
will return a Comparable
subclass of Number
. The internal LargestSliceAccumulator
is
a mutable implementation of LargestSliceList
Note: Be careful to use only a single sliceSizeExtractor
or you can end up with LargestSliceList
objects
with incompatible search orders (comparators).
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Collector
Collector.Characteristics
-
Constructor Summary
ConstructorDescriptionLargestSliceCollector
(int n, Function<E, N> sliceSizeExtractor, N zero, BinaryOperator<N> additionOperator) Construct aLargestSliceCollector
to collectn
objects of type<E>
. -
Method Summary
-
Constructor Details
-
LargestSliceCollector
public LargestSliceCollector(int n, Function<E, N> sliceSizeExtractor, N zero, BinaryOperator<N> additionOperator) Construct aLargestSliceCollector
to collectn
objects of type<E>
. The remaining constructor parameters provide support for the necessary internal calculations for types<E>
and<N>
.- Parameters:
n
- Number of "slices" to collectsliceSizeExtractor
- AFunction
to return the slice size of an objectzero
- The value of zero for type NadditionOperator
- addition operator for type N
-
-
Method Details
-
supplier
- Specified by:
supplier
in interfaceCollector<E,
LargestSliceAccumulator<E, N extends Number & Comparable<? super N>>, LargestSliceList<E, N extends Number & Comparable<? super N>>>
-
accumulator
- Specified by:
accumulator
in interfaceCollector<E,
LargestSliceAccumulator<E, N extends Number & Comparable<? super N>>, LargestSliceList<E, N extends Number & Comparable<? super N>>>
-
combiner
- Specified by:
combiner
in interfaceCollector<E,
LargestSliceAccumulator<E, N extends Number & Comparable<? super N>>, LargestSliceList<E, N extends Number & Comparable<? super N>>>
-
finisher
Return a finisher function.- Specified by:
finisher
in interfaceCollector<E,
LargestSliceAccumulator<E, N extends Number & Comparable<? super N>>, LargestSliceList<E, N extends Number & Comparable<? super N>>> - Returns:
- A finisher function
-
characteristics
Note that our accumulator won't work correctly or efficiently if a single accumulator is used from multiple threads. It works with parallel streams, but not with a CONCURRENT collector. See: https://stackoverflow.com/questions/22350288/parallel-streams-collectors-and-thread-safety- Specified by:
characteristics
in interfaceCollector<E,
LargestSliceAccumulator<E, N extends Number & Comparable<? super N>>, LargestSliceList<E, N extends Number & Comparable<? super N>>> - Returns:
- The characteristics of this collector.
-