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