Max.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.statistics.descriptive;

  18. /**
  19.  * Returns the maximum of the available values. Uses {@link Math#max(double, double) Math.max} as an
  20.  * underlying function to compute the {@code maximum}.
  21.  *
  22.  * <ul>
  23.  *   <li>The result is {@link Double#NEGATIVE_INFINITY negative infinity} if no values are added.
  24.  *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
  25.  *   <li>The value {@code -0.0} is considered strictly smaller than {@code 0.0}.
  26.  * </ul>
  27.  *
  28.  * <p>This class is designed to work with (though does not require)
  29.  * {@linkplain java.util.stream streams}.
  30.  *
  31.  * <p><strong>This implementation is not thread safe.</strong>
  32.  * If multiple threads access an instance of this class concurrently,
  33.  * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
  34.  * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
  35.  *
  36.  * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
  37.  * and {@link StatisticAccumulator#combine(StatisticResult) combine}
  38.  * as {@code accumulator} and {@code combiner} functions of
  39.  * {@link java.util.stream.Collector Collector} on a parallel stream,
  40.  * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()}
  41.  * provides the necessary partitioning, isolation, and merging of results for
  42.  * safe and efficient parallel execution.
  43.  *
  44.  * @since 1.1
  45.  * @see Math#max(double, double)
  46.  */
  47. public final class Max implements DoubleStatistic, StatisticAccumulator<Max> {

  48.     /** Current maximum. */
  49.     private double maximum = Double.NEGATIVE_INFINITY;

  50.     /**
  51.      * Create an instance.
  52.      */
  53.     private Max() {
  54.         // No-op
  55.     }

  56.     /**
  57.      * Creates an instance.
  58.      *
  59.      * <p>The initial result is {@link Double#NEGATIVE_INFINITY negative infinity}.
  60.      *
  61.      * @return {@code Max} instance.
  62.      */
  63.     public static Max create() {
  64.         return new Max();
  65.     }

  66.     /**
  67.      * Returns an instance populated using the input {@code values}.
  68.      *
  69.      * <p>The result is {@code NaN} if any of the values is {@code NaN}.
  70.      *
  71.      * <p>When the input is an empty array, the result is
  72.      * {@link Double#NEGATIVE_INFINITY negative infinity}.
  73.      *
  74.      * @param values Values.
  75.      * @return {@code Max} instance.
  76.      */
  77.     public static Max of(double... values) {
  78.         return Statistics.add(new Max(), values);
  79.     }

  80.     /**
  81.      * Updates the state of the statistic to reflect the addition of {@code value}.
  82.      *
  83.      * @param value Value.
  84.      */
  85.     @Override
  86.     public void accept(double value) {
  87.         maximum = Math.max(maximum, value);
  88.     }

  89.     /**
  90.      * Gets the maximum of all input values.
  91.      *
  92.      * <p>When no values have been added, the result is
  93.      * {@link Double#NEGATIVE_INFINITY negative infinity}.
  94.      *
  95.      * @return maximum of all values.
  96.      */
  97.     @Override
  98.     public double getAsDouble() {
  99.         return maximum;
  100.     }

  101.     @Override
  102.     public Max combine(Max other) {
  103.         accept(other.getAsDouble());
  104.         return this;
  105.     }
  106. }