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