Package io.jenetics.stat
Class MinMax<C>
- java.lang.Object
-
- io.jenetics.stat.MinMax<C>
-
- All Implemented Interfaces:
Consumer<C>
public final class MinMax<C> extends Object implements Consumer<C>
This consumer class is used for calculating the min and max value according to the givenComparator
.This class is designed to work with (though does not require) streams. For example, you can compute minimum and maximum values with:
final Stream<Integer> stream = ... final MinMax<Integer> minMax = stream.collect( MinMax::of, MinMax::accept, MinMax::combine );
- Since:
- 3.0
- Version:
- 6.0
- Implementation Note:
- This implementation is not thread safe. However, it is safe to use on a
parallel stream, because the parallel implementation of
Stream.collect()
provides the necessary partitioning, isolation, and merging of results for safe and efficient parallel execution.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(C object)
Accept the element for min-max calculation.MinMax<C>
combine(MinMax<C> other)
Combine twoMinMax
objects.long
count()
Returns the count of values recorded.C
max()
Return the current maximal object ornull
if no element has been accepted yet.static <T> T
max(Comparator<? super T> comp, T a, T b)
Return the maximum of two values, according the given comparator.C
min()
Return the current minimal object ornull
if no element has been accepted yet.static <T> T
min(Comparator<? super T> comp, T a, T b)
Return the minimum of two values, according the given comparator.static <C extends Comparable<? super C>>
MinMax<C>of()
Create a newMinMax
consumer.static <T> MinMax<T>
of(Comparator<? super T> comparator)
Create a newMinMax
consumer with the givenComparator
.boolean
sameState(MinMax<C> other)
Compares the state of twoLongMomentStatistics
objects.static <C extends Comparable<? super C>>
Collector<C,?,MinMax<C>>toMinMax()
Return aCollector
which calculates the minimum and maximum value.static <T> Collector<T,?,MinMax<T>>
toMinMax(Comparator<? super T> comparator)
Return aCollector
which calculates the minimum and maximum value.static <C extends Comparable<? super C>>
Function<C,Stream<C>>toStrictlyDecreasing()
Return a new flat-mapper function, which guarantees a strictly decreasing stream, from an arbitrarily ordered source stream.static <T> Function<T,Stream<T>>
toStrictlyImproving(Comparator<? super T> comparator)
Return a new flat-mapper function, which guarantees a strictly improving stream, from an arbitrarily ordered source stream.static <C extends Comparable<? super C>>
Function<C,Stream<C>>toStrictlyIncreasing()
Return a new flat-mapper function, which guarantees a strictly increasing stream, from an arbitrarily ordered source stream.String
toString()
-
-
-
Method Detail
-
combine
public MinMax<C> combine(MinMax<C> other)
Combine twoMinMax
objects.- Parameters:
other
- the otherMinMax
object to combine- Returns:
this
- Throws:
NullPointerException
- if theother
object isnull
.
-
count
public long count()
Returns the count of values recorded.- Returns:
- the count of recorded values
-
min
public C min()
Return the current minimal object ornull
if no element has been accepted yet.- Returns:
- the current minimal object
-
max
public C max()
Return the current maximal object ornull
if no element has been accepted yet.- Returns:
- the current maximal object
-
sameState
public boolean sameState(MinMax<C> other)
Compares the state of twoLongMomentStatistics
objects. This is a replacement for theObject.equals(Object)
which is not advisable to implement for this mutable object. If two object have the same state, it has still the same state when updated with the same value.final MinMax mm1 = ...; final MinMax mm2 = ...; if (mm1.sameState(mm2)) { final long value = random.nextInt(1_000_000); mm1.accept(value); mm2.accept(value); assert mm1.sameState(mm2); assert mm2.sameState(mm1); assert mm1.sameState(mm1); }
- Parameters:
other
- the other object for the test- Returns:
true
thethis
and theother
objects have the same state,false
otherwise- Since:
- 3.7
-
min
public static <T> T min(Comparator<? super T> comp, T a, T b)
Return the minimum of two values, according the given comparator.null
values are allowed.- Type Parameters:
T
- the type of the compared objects- Parameters:
comp
- the comparator used for determining the min valuea
- the first value to compareb
- the second value to compare- Returns:
- the minimum value, or
null
if both values arenull
. If only one value isnull
, the nonnull
values is returned.
-
max
public static <T> T max(Comparator<? super T> comp, T a, T b)
Return the maximum of two values, according the given comparator.null
values are allowed.- Type Parameters:
T
- the type of the compared objects- Parameters:
comp
- the comparator used for determining the max valuea
- the first value to compareb
- the second value to compare- Returns:
- the maximum value, or
null
if both values arenull
. If only one value isnull
, the nonnull
values is returned.
-
toMinMax
public static <T> Collector<T,?,MinMax<T>> toMinMax(Comparator<? super T> comparator)
Return aCollector
which calculates the minimum and maximum value. The givencomparator
is used for comparing two objects.final Comparator<SomeObject> comparator = ... final Stream<SomeObject> stream = ... final MinMax<SomeObject> moments = stream .collect(doubleMoments.toMinMax(comparator));
- Type Parameters:
T
- the type of the input elements- Parameters:
comparator
- theComparator
to use- Returns:
- a
Collector
implementing the min-max reduction - Throws:
NullPointerException
- if the givenmapper
isnull
-
toMinMax
public static <C extends Comparable<? super C>> Collector<C,?,MinMax<C>> toMinMax()
Return aCollector
which calculates the minimum and maximum value. The reducing objects must be comparable.final Stream<SomeObject> stream = ... final MinMax<SomeObject> moments = stream .collect(doubleMoments.toMinMax(comparator));
- Type Parameters:
C
- the type of the input elements- Returns:
- a
Collector
implementing the min-max reduction - Throws:
NullPointerException
- if the givenmapper
isnull
-
of
public static <T> MinMax<T> of(Comparator<? super T> comparator)
Create a newMinMax
consumer with the givenComparator
.- Type Parameters:
T
- the element type- Parameters:
comparator
- the comparator used for comparing two elements- Returns:
- a new
MinMax
consumer - Throws:
NullPointerException
- if thecomparator
isnull
.
-
of
public static <C extends Comparable<? super C>> MinMax<C> of()
Create a newMinMax
consumer.- Type Parameters:
C
- the element type- Returns:
- a new
MinMax
consumer
-
toStrictlyIncreasing
public static <C extends Comparable<? super C>> Function<C,Stream<C>> toStrictlyIncreasing()
Return a new flat-mapper function, which guarantees a strictly increasing stream, from an arbitrarily ordered source stream. Note that this function doesn't sort the stream. It just skips the out of order elements.final ISeq<Integer> values = new Random().ints(0, 100) .boxed() .limit(100) .flatMap(MinMax.toStrictlyIncreasing()) .collect(ISeq.toISeq()); System.out.println(values); // [6,47,65,78,96,96,99]
- Type Parameters:
C
- the comparable type- Returns:
- a new flat-mapper function
- Since:
- 5.0
-
toStrictlyDecreasing
public static <C extends Comparable<? super C>> Function<C,Stream<C>> toStrictlyDecreasing()
Return a new flat-mapper function, which guarantees a strictly decreasing stream, from an arbitrarily ordered source stream. Note that this function doesn't sort the stream. It just skips the out of order elements.final ISeq<Integer> values = new Random().ints(0, 100) .boxed() .limit(100) .flatMap(MinMax.toStrictlyDecreasing()) .collect(ISeq.toISeq()); System.out.println(values); // [45,32,15,12,3,1]
- Type Parameters:
C
- the comparable type- Returns:
- a new flat-mapper function
- Since:
- 5.0
-
toStrictlyImproving
public static <T> Function<T,Stream<T>> toStrictlyImproving(Comparator<? super T> comparator)
Return a new flat-mapper function, which guarantees a strictly improving stream, from an arbitrarily ordered source stream. Note that this function doesn't sort the stream. It just skips the out of order elements.final ISeq<Integer> values = new Random().ints(0, 100) .boxed() .limit(100) .flatMap(MinMax.toStrictlyImproving(Comparator.naturalOrder())) .collect(ISeq.toISeq()); System.out.println(values); // [6,47,65,78,96,96,99]
- Type Parameters:
T
- the element type- Parameters:
comparator
- the comparator used for testing the elements- Returns:
- a new flat-mapper function
- Since:
- 6.0
- See Also:
toStrictlyIncreasing()
,toStrictlyDecreasing()
-
-