001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.statistics.descriptive; 018 019/** 020 * Returns the maximum of the available values. Uses {@link Math#max(double, double) Math.max} as an 021 * underlying function to compute the {@code maximum}. 022 * 023 * <ul> 024 * <li>The result is {@link Double#NEGATIVE_INFINITY negative infinity} if no values are added. 025 * <li>The result is {@code NaN} if any of the values is {@code NaN}. 026 * <li>The value {@code -0.0} is considered strictly smaller than {@code 0.0}. 027 * </ul> 028 * 029 * <p>This class is designed to work with (though does not require) 030 * {@linkplain java.util.stream streams}. 031 * 032 * <p><strong>This implementation is not thread safe.</strong> 033 * If multiple threads access an instance of this class concurrently, 034 * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or 035 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally. 036 * 037 * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept} 038 * and {@link StatisticAccumulator#combine(StatisticResult) combine} 039 * as {@code accumulator} and {@code combiner} functions of 040 * {@link java.util.stream.Collector Collector} on a parallel stream, 041 * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()} 042 * provides the necessary partitioning, isolation, and merging of results for 043 * safe and efficient parallel execution. 044 * 045 * @since 1.1 046 * @see Math#max(double, double) 047 */ 048public final class Max implements DoubleStatistic, StatisticAccumulator<Max> { 049 050 /** Current maximum. */ 051 private double maximum = Double.NEGATIVE_INFINITY; 052 053 /** 054 * Create an instance. 055 */ 056 private Max() { 057 // No-op 058 } 059 060 /** 061 * Creates an instance. 062 * 063 * <p>The initial result is {@link Double#NEGATIVE_INFINITY negative infinity}. 064 * 065 * @return {@code Max} instance. 066 */ 067 public static Max create() { 068 return new Max(); 069 } 070 071 /** 072 * Returns an instance populated using the input {@code values}. 073 * 074 * <p>The result is {@code NaN} if any of the values is {@code NaN}. 075 * 076 * <p>When the input is an empty array, the result is 077 * {@link Double#NEGATIVE_INFINITY negative infinity}. 078 * 079 * @param values Values. 080 * @return {@code Max} instance. 081 */ 082 public static Max of(double... values) { 083 return Statistics.add(new Max(), values); 084 } 085 086 /** 087 * Updates the state of the statistic to reflect the addition of {@code value}. 088 * 089 * @param value Value. 090 */ 091 @Override 092 public void accept(double value) { 093 maximum = Math.max(maximum, value); 094 } 095 096 /** 097 * Gets the maximum of all input values. 098 * 099 * <p>When no values have been added, the result is 100 * {@link Double#NEGATIVE_INFINITY negative infinity}. 101 * 102 * @return maximum of all values. 103 */ 104 @Override 105 public double getAsDouble() { 106 return maximum; 107 } 108 109 @Override 110 public Max combine(Max other) { 111 accept(other.getAsDouble()); 112 return this; 113 } 114}