View Javadoc

    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.stat.descriptive.rank;
    17
    18  import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
    19
    20  /***
    21   * Returns the maximum of the available values.
    22   * <p>
    23   * <ul>
    24   * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
    25   * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
    26   * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
    27   * the result is <code>Double.POSITIVE_INFINITY.</code></li>
    28   * </ul>
    29  * <p>
    30   * <strong>Note that this implementation is not synchronized.</strong> If
    31   * multiple threads access an instance of this class concurrently, and at least
    32   * one of the threads invokes the <code>increment()</code> or
    33   * <code>clear()</code> method, it must be synchronized externally.
    34   *
    35   * @version $Revision: 1.2 $ $Date: 2004/11/07 02:44:45 $
    36   */
    37  public class Max extends AbstractStorelessUnivariateStatistic {
    38
    39      /*** Serializable version identifier */
    40      static final long serialVersionUID = -5593383832225844641L;
    41
    42      /*** Number of values that have been added */
    43      private long n;
    44
    45      /*** Current value of the statistic */
    46      private double value;
    47
    48      /***
    49       * Create a Max instance
    50       */
    51      public Max() {
    52          n = 0;
    53          value = Double.NaN;
    54      }
    55
    56      /***
    57       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
    58       */
    59      public void increment(final double d) {
    60          if (d > value || Double.isNaN(value)) {
    61              value = d;
    62          }
    63          n++;
    64      }
    65
    66      /***
    67           * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
    68           */
    69      public void clear() {
    70          value = Double.NaN;
    71          n = 0;
    72      }
    73
    74      /***
    75       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
    76       */
    77      public double getResult() {
    78          return value;
    79      }
    80
    81      /***
    82       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
    83       */
    84      public long getN() {
    85          return n;
    86      }
    87
    88      /***
    89       * Returns the maximum of the entries in the specified portion of
    90       * the input array, or <code>Double.NaN</code> if the designated subarray
    91       * is empty.
    92       * <p>
    93       * Throws <code>IllegalArgumentException</code> if the array is null or
    94       * the array index parameters are not valid.
    95       * <p>
    96       * <ul>
    97       * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
    98       * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
    99       * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
    100      * the result is <code>Double.POSITIVE_INFINITY.</code></li>
    101      * </ul>
    102      *
    103      * @param values the input array
    104      * @param begin index of the first array element to include
    105      * @param length the number of elements to include
    106      * @return the maximum of the values or Double.NaN if length = 0
    107      * @throws IllegalArgumentException if the array is null or the array index
    108      *  parameters are not valid
    109      */
    110     public double evaluate(final double[] values, final int begin, final int length) {
    111         double max = Double.NaN;
    112         if (test(values, begin, length)) {
    113             max = values[begin];
    114             for (int i = begin; i < begin + length; i++) {
    115                 max = (max > values[i]) ? max : values[i];
    116             }
    117         }
    118         return max;
    119     }
    120 }