IntMax.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. import java.math.BigInteger;

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

  47.     /** Current maximum. */
  48.     private int maximum = Integer.MIN_VALUE;

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

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

  65.     /**
  66.      * Returns an instance populated using the input {@code values}.
  67.      *
  68.      * <p>When the input is an empty array, the result is
  69.      * {@link Integer#MIN_VALUE}.
  70.      *
  71.      * @param values Values.
  72.      * @return {@code Max} instance.
  73.      */
  74.     public static IntMax of(int... values) {
  75.         return Statistics.add(new IntMax(), values);
  76.     }

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

  86.     /**
  87.      * Gets the maximum of all input values.
  88.      *
  89.      * <p>When no values have been added, the result is
  90.      * {@link Integer#MIN_VALUE}.
  91.      *
  92.      * @return maximum of all values.
  93.      */
  94.     @Override
  95.     public int getAsInt() {
  96.         return maximum;
  97.     }

  98.     @Override
  99.     public long getAsLong() {
  100.         return maximum;
  101.     }

  102.     @Override
  103.     public double getAsDouble() {
  104.         return maximum;
  105.     }

  106.     @Override
  107.     public BigInteger getAsBigInteger() {
  108.         return BigInteger.valueOf(maximum);
  109.     }

  110.     @Override
  111.     public IntMax combine(IntMax other) {
  112.         accept(other.getAsInt());
  113.         return this;
  114.     }
  115. }