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.math.stat.descriptive.rank;
18
19 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
20
21 /**
22 * Returns the maximum of the available values.
23 * <p>
24 * <ul>
25 * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
26 * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
27 * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
28 * the result is <code>Double.POSITIVE_INFINITY.</code></li>
29 * </ul></p>
30 * <p>
31 * <strong>Note that this implementation is not synchronized.</strong> If
32 * multiple threads access an instance of this class concurrently, and at least
33 * one of the threads invokes the <code>increment()</code> or
34 * <code>clear()</code> method, it must be synchronized externally.</p>
35 *
36 * @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $
37 */
38 public class Max extends AbstractStorelessUnivariateStatistic {
39
40 /** Serializable version identifier */
41 private static final long serialVersionUID = -5593383832225844641L;
42
43 /** Number of values that have been added */
44 private long n;
45
46 /** Current value of the statistic */
47 private double value;
48
49 /**
50 * Create a Max instance
51 */
52 public Max() {
53 n = 0;
54 value = Double.NaN;
55 }
56
57 /**
58 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
59 */
60 public void increment(final double d) {
61 if (d > value || Double.isNaN(value)) {
62 value = d;
63 }
64 n++;
65 }
66
67 /**
68 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
69 */
70 public void clear() {
71 value = Double.NaN;
72 n = 0;
73 }
74
75 /**
76 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
77 */
78 public double getResult() {
79 return value;
80 }
81
82 /**
83 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
84 */
85 public long getN() {
86 return n;
87 }
88
89 /**
90 * Returns the maximum of the entries in the specified portion of
91 * the input array, or <code>Double.NaN</code> if the designated subarray
92 * is empty.
93 * <p>
94 * Throws <code>IllegalArgumentException</code> if the array is null or
95 * the array index parameters are not valid.</p>
96 * <p>
97 * <ul>
98 * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
99 * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
100 * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
101 * the result is <code>Double.POSITIVE_INFINITY.</code></li>
102 * </ul></p>
103 *
104 * @param values the input array
105 * @param begin index of the first array element to include
106 * @param length the number of elements to include
107 * @return the maximum of the values or Double.NaN if length = 0
108 * @throws IllegalArgumentException if the array is null or the array index
109 * parameters are not valid
110 */
111 public double evaluate(final double[] values, final int begin, final int length) {
112 double max = Double.NaN;
113 if (test(values, begin, length)) {
114 max = values[begin];
115 for (int i = begin; i < begin + length; i++) {
116 if (!Double.isNaN(values[i])) {
117 max = (max > values[i]) ? max : values[i];
118 }
119 }
120 }
121 return max;
122 }
123 }