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 019import java.math.BigInteger; 020 021/** 022 * Returns the maximum of the available values. Uses {@link Math#max(int, int) Math.max} as an 023 * underlying function to compute the {@code maximum}. 024 * 025 * <ul> 026 * <li>The result is {@link Integer#MIN_VALUE} if no values are added. 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.IntConsumer#accept(int) 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.IntConsumer#accept(int) 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(int, int) 047 */ 048public final class IntMax implements IntStatistic, StatisticAccumulator<IntMax> { 049 050 /** Current maximum. */ 051 private int maximum = Integer.MIN_VALUE; 052 053 /** 054 * Create an instance. 055 */ 056 private IntMax() { 057 // No-op 058 } 059 060 /** 061 * Creates an instance. 062 * 063 * <p>The initial result is {@link Integer#MIN_VALUE}. 064 * 065 * @return {@code IntMax} instance. 066 */ 067 public static IntMax create() { 068 return new IntMax(); 069 } 070 071 /** 072 * Returns an instance populated using the input {@code values}. 073 * 074 * <p>When the input is an empty array, the result is 075 * {@link Integer#MIN_VALUE}. 076 * 077 * @param values Values. 078 * @return {@code IntMax} instance. 079 */ 080 public static IntMax of(int... values) { 081 return Statistics.add(new IntMax(), values); 082 } 083 084 /** 085 * Returns an instance populated using the specified range of {@code values}. 086 * 087 * <p>When the range is empty, the result is 088 * {@link Integer#MIN_VALUE}. 089 * 090 * @param values Values. 091 * @param from Inclusive start of the range. 092 * @param to Exclusive end of the range. 093 * @return {@code IntMax} instance. 094 * @throws IndexOutOfBoundsException if the sub-range is out of bounds 095 * @since 1.2 096 */ 097 public static IntMax ofRange(int[] values, int from, int to) { 098 Statistics.checkFromToIndex(from, to, values.length); 099 return createFromRange(values, from, to); 100 } 101 102 /** 103 * Create an instance using the specified range of {@code values}. 104 * 105 * <p>Warning: No range checks are performed. 106 * 107 * @param values Values. 108 * @param from Inclusive start of the range. 109 * @param to Exclusive end of the range. 110 * @return {@code IntMax} instance. 111 */ 112 static IntMax createFromRange(int[] values, int from, int to) { 113 return Statistics.add(new IntMax(), values, from, to); 114 } 115 116 /** 117 * Updates the state of the statistic to reflect the addition of {@code value}. 118 * 119 * @param value Value. 120 */ 121 @Override 122 public void accept(int value) { 123 maximum = Math.max(maximum, value); 124 } 125 126 /** 127 * Gets the maximum of all input values. 128 * 129 * <p>When no values have been added, the result is 130 * {@link Integer#MIN_VALUE}. 131 * 132 * @return maximum of all values. 133 */ 134 @Override 135 public int getAsInt() { 136 return maximum; 137 } 138 139 @Override 140 public long getAsLong() { 141 return maximum; 142 } 143 144 @Override 145 public double getAsDouble() { 146 return maximum; 147 } 148 149 @Override 150 public BigInteger getAsBigInteger() { 151 return BigInteger.valueOf(maximum); 152 } 153 154 @Override 155 public IntMax combine(IntMax other) { 156 accept(other.getAsInt()); 157 return this; 158 } 159}