LongMax.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(long, long) Math.max} as an
  21.  * underlying function to compute the {@code maximum}.
  22.  *
  23.  * <ul>
  24.  *   <li>The result is {@link Long#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.LongConsumer#accept(long) 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.LongConsumer#accept(long) 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(long, long)
  45.  */
  46. public final class LongMax implements LongStatistic, StatisticAccumulator<LongMax> {

  47.     /** Current maximum. */
  48.     private long maximum = Long.MIN_VALUE;

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

  55.     /**
  56.      * Creates an instance.
  57.      *
  58.      * <p>The initial result is {@link Long#MIN_VALUE}.
  59.      *
  60.      * @return {@code Min} instance.
  61.      */
  62.     public static LongMax create() {
  63.         return new LongMax();
  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 Long#MIN_VALUE}.
  70.      *
  71.      * @param values Values.
  72.      * @return {@code Min} instance.
  73.      */
  74.     public static LongMax of(long... values) {
  75.         return Statistics.add(new LongMax(), 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(long 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 Long#MIN_VALUE}.
  91.      *
  92.      * @return maximum of all values.
  93.      */
  94.     @Override
  95.     public long getAsLong() {
  96.         return maximum;
  97.     }

  98.     /**
  99.      * Gets the maximum of all input values.
  100.      *
  101.      * <p>This method will throw an {@link ArithmeticException} if the {@code long}
  102.      * maximum overflows an {@code int}; or no values have been added.
  103.      *
  104.      * @return maximum of all values.
  105.      * @see Math#toIntExact(long)
  106.      */
  107.     @Override
  108.     public int getAsInt() {
  109.         return Math.toIntExact(maximum);
  110.     }

  111.     @Override
  112.     public double getAsDouble() {
  113.         return maximum;
  114.     }

  115.     @Override
  116.     public BigInteger getAsBigInteger() {
  117.         return BigInteger.valueOf(maximum);
  118.     }

  119.     @Override
  120.     public LongMax combine(LongMax other) {
  121.         accept(other.getAsLong());
  122.         return this;
  123.     }
  124. }