Class 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 given Comparator.

    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
    Author:
    Franz Wilhelmstötter
    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 Detail

      • accept

        public void accept​(C object)
        Accept the element for min-max calculation.
        Specified by:
        accept in interface Consumer<C>
        Parameters:
        object - the element to use for min-max calculation
      • combine

        public MinMax<Ccombine​(MinMax<C> other)
        Combine two MinMax objects.
        Parameters:
        other - the other MinMax object to combine
        Returns:
        this
        Throws:
        NullPointerException - if the other object is null.
      • 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 or null if no element has been accepted yet.
        Returns:
        the current minimal object
      • max

        public C max()
        Return the current maximal object or null if no element has been accepted yet.
        Returns:
        the current maximal object
      • sameState

        public boolean sameState​(MinMax<C> other)
        Compares the state of two LongMomentStatistics objects. This is a replacement for the Object.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 the this and the other 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 value
        a - the first value to compare
        b - the second value to compare
        Returns:
        the minimum value, or null if both values are null. If only one value is null, the non null 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 value
        a - the first value to compare
        b - the second value to compare
        Returns:
        the maximum value, or null if both values are null. If only one value is null, the non null values is returned.
      • toMinMax

        public static <T> Collector<T,​?,​MinMax<T>> toMinMax​(Comparator<? super T> comparator)
        Return a Collector which calculates the minimum and maximum value. The given comparator 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 - the Comparator to use
        Returns:
        a Collector implementing the min-max reduction
        Throws:
        NullPointerException - if the given mapper is null
      • toMinMax

        public static <C extends Comparable<? super C>> Collector<C,​?,​MinMax<C>> toMinMax()
        Return a Collector 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 given mapper is null
      • of

        public static <T> MinMax<T> of​(Comparator<? super T> comparator)
        Create a new MinMax consumer with the given Comparator.
        Type Parameters:
        T - the element type
        Parameters:
        comparator - the comparator used for comparing two elements
        Returns:
        a new MinMax consumer
        Throws:
        NullPointerException - if the comparator is null.
      • of

        public static <C extends Comparable<? super C>> MinMax<C> of()
        Create a new MinMax 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()