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 import java.math.BigInteger;
20
21 /**
22 * Returns the maximum of the available values. Uses {@link Math#max(long, long) Math.max} as an
23 * underlying function to compute the {@code maximum}.
24 *
25 * <ul>
26 * <li>The result is {@link Long#MIN_VALUE} if no values are added.
27 * </ul>
28 *
29 * <p>This class is designed to work with (though does not require)
30 * {@linkplain java.util.stream streams}.
31 *
32 * <p><strong>This implementation is not thread safe.</strong>
33 * If multiple threads access an instance of this class concurrently,
34 * and at least one of the threads invokes the {@link java.util.function.LongConsumer#accept(long) accept} or
35 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
36 *
37 * <p>However, it is safe to use {@link java.util.function.LongConsumer#accept(long) accept}
38 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
39 * as {@code accumulator} and {@code combiner} functions of
40 * {@link java.util.stream.Collector Collector} on a parallel stream,
41 * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()}
42 * provides the necessary partitioning, isolation, and merging of results for
43 * safe and efficient parallel execution.
44 *
45 * @since 1.1
46 * @see Math#max(long, long)
47 */
48 public final class LongMax implements LongStatistic, StatisticAccumulator<LongMax> {
49
50 /** Current maximum. */
51 private long maximum = Long.MIN_VALUE;
52
53 /**
54 * Create an instance.
55 */
56 private LongMax() {
57 // No-op
58 }
59
60 /**
61 * Creates an instance.
62 *
63 * <p>The initial result is {@link Long#MIN_VALUE}.
64 *
65 * @return {@code LongMax} instance.
66 */
67 public static LongMax create() {
68 return new LongMax();
69 }
70
71 /**
72 * Returns an instance populated using the input {@code values}.
73 *
74 * <p>When the input is an empty array, the result is
75 * {@link Long#MIN_VALUE}.
76 *
77 * @param values Values.
78 * @return {@code LongMax} instance.
79 */
80 public static LongMax of(long... values) {
81 return Statistics.add(new LongMax(), values);
82 }
83
84 /**
85 * Returns an instance populated using the specified range of {@code values}.
86 *
87 * <p>When the range is empty, the result is
88 * {@link Long#MIN_VALUE}.
89 *
90 * @param values Values.
91 * @param from Inclusive start of the range.
92 * @param to Exclusive end of the range.
93 * @return {@code LongMax} instance.
94 * @throws IndexOutOfBoundsException if the sub-range is out of bounds
95 * @since 1.2
96 */
97 public static LongMax ofRange(long[] values, int from, int to) {
98 Statistics.checkFromToIndex(from, to, values.length);
99 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 LongMax} instance.
111 */
112 static LongMax createFromRange(long[] values, int from, int to) {
113 return Statistics.add(new LongMax(), 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(long 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 Long#MIN_VALUE}.
131 *
132 * @return maximum of all values.
133 */
134 @Override
135 public long getAsLong() {
136 return maximum;
137 }
138
139 /**
140 * Gets the maximum of all input values.
141 *
142 * <p>This method will throw an {@link ArithmeticException} if the {@code long}
143 * maximum overflows an {@code int}; or no values have been added.
144 *
145 * @return maximum of all values.
146 * @see Math#toIntExact(long)
147 */
148 @Override
149 public int getAsInt() {
150 return Math.toIntExact(maximum);
151 }
152
153 @Override
154 public double getAsDouble() {
155 return maximum;
156 }
157
158 @Override
159 public BigInteger getAsBigInteger() {
160 return BigInteger.valueOf(maximum);
161 }
162
163 @Override
164 public LongMax combine(LongMax other) {
165 accept(other.getAsLong());
166 return this;
167 }
168 }