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.math4.legacy.stat.descriptive.rank;
18
19 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
20 import org.apache.commons.math4.legacy.exception.NullArgumentException;
21 import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic;
22 import org.apache.commons.math4.legacy.core.MathArrays;
23
24 /**
25 * Returns the maximum of the available values.
26 * <ul>
27 * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
28 * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
29 * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
30 * the result is <code>Double.POSITIVE_INFINITY.</code></li>
31 * </ul>
32 * <p>
33 * <strong>Note that this implementation is not synchronized.</strong> If
34 * multiple threads access an instance of this class concurrently, and at least
35 * one of the threads invokes the <code>increment()</code> or
36 * <code>clear()</code> method, it must be synchronized externally.</p>
37 */
38 public class Max extends AbstractStorelessUnivariateStatistic {
39 /** Number of values that have been added. */
40 private long n;
41
42 /** Current value of the statistic. */
43 private double value;
44
45 /**
46 * Create a Max instance.
47 */
48 public Max() {
49 n = 0;
50 value = Double.NaN;
51 }
52
53 /**
54 * Copy constructor, creates a new {@code Max} identical
55 * to the {@code original}.
56 *
57 * @param original the {@code Max} instance to copy
58 * @throws NullArgumentException if original is null
59 */
60 public Max(Max original) throws NullArgumentException {
61 copy(original, this);
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 @Override
68 public void increment(final double d) {
69 if (d > value || Double.isNaN(value)) {
70 value = d;
71 }
72 n++;
73 }
74
75 /**
76 * {@inheritDoc}
77 */
78 @Override
79 public void clear() {
80 value = Double.NaN;
81 n = 0;
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 @Override
88 public double getResult() {
89 return value;
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 @Override
96 public long getN() {
97 return n;
98 }
99
100 /**
101 * Returns the maximum of the entries in the specified portion of
102 * the input array, or <code>Double.NaN</code> if the designated subarray
103 * is empty.
104 * <p>
105 * Throws <code>MathIllegalArgumentException</code> if the array is null or
106 * the array index parameters are not valid.</p>
107 * <ul>
108 * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
109 * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
110 * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
111 * the result is <code>Double.POSITIVE_INFINITY.</code></li>
112 * </ul>
113 *
114 * @param values the input array
115 * @param begin index of the first array element to include
116 * @param length the number of elements to include
117 * @return the maximum of the values or Double.NaN if length = 0
118 * @throws MathIllegalArgumentException if the array is null or the array index
119 * parameters are not valid
120 */
121 @Override
122 public double evaluate(final double[] values, final int begin, final int length)
123 throws MathIllegalArgumentException {
124
125 double max = Double.NaN;
126 if (MathArrays.verifyValues(values, begin, length)) {
127 max = values[begin];
128 for (int i = begin; i < begin + length; i++) {
129 if (!Double.isNaN(values[i])) {
130 max = (max > values[i]) ? max : values[i];
131 }
132 }
133 }
134 return max;
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 @Override
141 public Max copy() {
142 Max result = new Max();
143 // No try-catch or advertised exception because args are non-null
144 copy(this, result);
145 return result;
146 }
147
148 /**
149 * Copies source to dest.
150 * <p>Neither source nor dest can be null.</p>
151 *
152 * @param source Max to copy
153 * @param dest Max to copy to
154 * @throws NullArgumentException if either source or dest is null
155 */
156 public static void copy(Max source, Max dest)
157 throws NullArgumentException {
158 NullArgumentException.check(source);
159 NullArgumentException.check(dest);
160 dest.n = source.n;
161 dest.value = source.value;
162 }
163 }