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