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.math.stat.descriptive;
18  
19  import org.apache.commons.math.MathRuntimeException;
20  import org.apache.commons.math.util.MathUtils;
21  
22  /**
23   *
24   * Abstract implementation of the {@link StorelessUnivariateStatistic} interface.
25   * <p>
26   * Provides default <code>evaluate()</code> and <code>incrementAll(double[])<code>
27   * implementations.</p>
28   * <p>
29   * <strong>Note that these implementations are not synchronized.</strong></p>
30   *
31   * @version $Revision: 780637 $ $Date: 2009-06-01 08:48:32 -0400 (Mon, 01 Jun 2009) $
32   */
33  public abstract class AbstractStorelessUnivariateStatistic
34      extends AbstractUnivariateStatistic
35      implements StorelessUnivariateStatistic {
36      
37      /**
38       * This default implementation calls {@link #clear}, then invokes 
39       * {@link #increment} in a loop over the the input array, and then uses 
40       * {@link #getResult} to compute the return value.  
41       * <p>
42       * Note that this implementation changes the internal state of the
43       * statistic.  Its side effects are the same as invoking {@link #clear} and
44       * then {@link #incrementAll(double[])}.</p>
45       * <p>
46       * Implementations may override this method with a more efficient and
47       * possibly more accurate implementation that works directly with the
48       * input array.</p>
49       * <p>
50       * If the array is null, an IllegalArgumentException is thrown.</p>
51       * @param values input array
52       * @return the value of the statistic applied to the input array
53       * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[])
54       */
55      @Override
56      public double evaluate(final double[] values) {
57          if (values == null) {
58              throw MathRuntimeException.createIllegalArgumentException("input values array is null");
59          }
60          return evaluate(values, 0, values.length);
61      }
62      
63      /**
64       * This default implementation calls {@link #clear}, then invokes 
65       * {@link #increment} in a loop over the specified portion of the input 
66       * array, and then uses {@link #getResult} to compute the return value.  
67       * <p>
68       * Note that this implementation changes the internal state of the
69       * statistic.  Its side effects are the same as invoking {@link #clear} and
70       * then {@link #incrementAll(double[], int, int)}.</p>
71       * <p>
72       * Implementations may override this method with a more efficient and
73       * possibly more accurate implementation that works directly with the
74       * input array.</p>
75       * <p>
76       * If the array is null or the index parameters are not valid, an 
77       * IllegalArgumentException is thrown.</p>
78       * @param values the input array
79       * @param begin the index of the first element to include
80       * @param length the number of elements to include
81       * @return the value of the statistic applied to the included array entries
82       * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int)
83       */
84      @Override
85      public double evaluate(final double[] values, final int begin, final int length) {
86          if (test(values, begin, length)) {
87              clear();
88              incrementAll(values, begin, length);
89          }
90          return getResult();
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public abstract StorelessUnivariateStatistic copy();
98  
99      /**
100      * {@inheritDoc}
101      */
102     public abstract void clear();
103 
104     /**
105      * {@inheritDoc}
106      */
107     public abstract double getResult();
108 
109     /**
110      * {@inheritDoc}
111      */
112     public abstract void increment(final double d);
113     
114     /**
115      * This default implementation just calls {@link #increment} in a loop over
116      * the input array.   
117      * <p>
118      * Throws IllegalArgumentException if the input values array is null.</p>
119      * 
120      * @param values values to add
121      * @throws IllegalArgumentException if values is null
122      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[])
123      */
124     public void incrementAll(double[] values) {
125         if (values == null) {
126             throw MathRuntimeException.createIllegalArgumentException("input values array is null");
127         }
128         incrementAll(values, 0, values.length);
129     } 
130    
131     /**
132      * This default implementation just calls {@link #increment} in a loop over
133      * the specified portion of the input array.
134      * <p>
135      * Throws IllegalArgumentException if the input values array is null.</p>
136      * 
137      * @param values  array holding values to add
138      * @param begin   index of the first array element to add
139      * @param length  number of array elements to add
140      * @throws IllegalArgumentException if values is null
141      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[], int, int)
142      */
143     public void incrementAll(double[] values, int begin, int length) {
144         if (test(values, begin, length)) {
145             int k = begin + length;
146             for (int i = begin; i < k; i++) {
147                 increment(values[i]);
148             }   
149         }
150     }
151     
152     /**
153      * Returns true iff <code>object</code> is an 
154      * <code>AbstractStorelessUnivariateStatistic</code> returning the same
155      * values as this for <code>getResult()</code> and <code>getN()</code>
156      * @param object object to test equality against.
157      * @return true if object returns the same value as this
158      */
159     @Override
160     public boolean equals(Object object) {
161         if (object == this ) {
162             return true;
163         }
164        if (object instanceof AbstractStorelessUnivariateStatistic == false) {
165             return false;
166         }
167         AbstractStorelessUnivariateStatistic stat = (AbstractStorelessUnivariateStatistic) object;
168         return (MathUtils.equals(stat.getResult(), this.getResult()) && 
169                 MathUtils.equals(stat.getN(), this.getN()));
170     }
171     
172     /**
173      * Returns hash code based on getResult() and getN()
174      * 
175      * @return hash code
176      */
177     @Override
178     public int hashCode() {
179         return 31* (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN());
180     }
181 
182 }