• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.io.Serializable;
20 
21 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
22 
23 /**
24  * Returns the maximum of the available values.
25  * <p>
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></p>
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  * @version $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
39  */
40 public class Max extends AbstractStorelessUnivariateStatistic implements Serializable {
41 
42     /** Serializable version identifier */
43     private static final long serialVersionUID = -5593383832225844641L;
44 
45     /** Number of values that have been added */
46     private long n;
47 
48     /** Current value of the statistic */
49     private double value;
50 
51     /**
52      * Create a Max instance
53      */
Max()54     public Max() {
55         n = 0;
56         value = Double.NaN;
57     }
58 
59     /**
60      * Copy constructor, creates a new {@code Max} identical
61      * to the {@code original}
62      *
63      * @param original the {@code Max} instance to copy
64      */
Max(Max original)65     public Max(Max original) {
66         copy(original, this);
67     }
68 
69     /**
70      * {@inheritDoc}
71      */
72     @Override
increment(final double d)73     public void increment(final double d) {
74         if (d > value || Double.isNaN(value)) {
75             value = d;
76         }
77         n++;
78     }
79 
80     /**
81      * {@inheritDoc}
82      */
83     @Override
clear()84     public void clear() {
85         value = Double.NaN;
86         n = 0;
87     }
88 
89     /**
90      * {@inheritDoc}
91      */
92     @Override
getResult()93     public double getResult() {
94         return value;
95     }
96 
97     /**
98      * {@inheritDoc}
99      */
getN()100     public long getN() {
101         return n;
102     }
103 
104     /**
105      * Returns the maximum of the entries in the specified portion of
106      * the input array, or <code>Double.NaN</code> if the designated subarray
107      * is empty.
108      * <p>
109      * Throws <code>IllegalArgumentException</code> if the array is null or
110      * the array index parameters are not valid.</p>
111      * <p>
112      * <ul>
113      * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
114      * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
115      * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
116      * the result is <code>Double.POSITIVE_INFINITY.</code></li>
117      * </ul></p>
118      *
119      * @param values the input array
120      * @param begin index of the first array element to include
121      * @param length the number of elements to include
122      * @return the maximum of the values or Double.NaN if length = 0
123      * @throws IllegalArgumentException if the array is null or the array index
124      *  parameters are not valid
125      */
126     @Override
evaluate(final double[] values, final int begin, final int length)127     public double evaluate(final double[] values, final int begin, final int length) {
128         double max = Double.NaN;
129         if (test(values, begin, length)) {
130             max = values[begin];
131             for (int i = begin; i < begin + length; i++) {
132                 if (!Double.isNaN(values[i])) {
133                     max = (max > values[i]) ? max : values[i];
134                 }
135             }
136         }
137         return max;
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
copy()144     public Max copy() {
145         Max result = new Max();
146         copy(this, result);
147         return result;
148     }
149 
150     /**
151      * Copies source to dest.
152      * <p>Neither source nor dest can be null.</p>
153      *
154      * @param source Max to copy
155      * @param dest Max to copy to
156      * @throws NullPointerException if either source or dest is null
157      */
copy(Max source, Max dest)158     public static void copy(Max source, Max dest) {
159         dest.setData(source.getDataRef());
160         dest.n = source.n;
161         dest.value = source.value;
162     }
163 }
164