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  
21  /**
22   * Abstract base class for all implementations of the 
23   * {@link UnivariateStatistic} interface.
24   * <p>
25   * Provides a default implementation of <code>evaluate(double[]),</code> 
26   * delegating to <code>evaluate(double[], int, int)</code> in the natural way.
27   * </p>
28   * <p>
29   * Also includes a <code>test</code> method that performs generic parameter
30   * validation for the <code>evaluate</code> methods.</p>
31   * 
32   * @version $Revision: 780541 $ $Date: 2009-05-31 20:47:02 -0400 (Sun, 31 May 2009) $
33   */
34  public abstract class AbstractUnivariateStatistic
35      implements UnivariateStatistic {
36  
37      /**
38       * {@inheritDoc}
39       */
40      public double evaluate(final double[] values) {
41          test(values, 0, 0);
42          return evaluate(values, 0, values.length);
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      public abstract double evaluate(final double[] values, final int begin, final int length);
49      
50      /**
51       * {@inheritDoc}
52       */
53      public abstract UnivariateStatistic copy();
54  
55      /**
56       * This method is used by <code>evaluate(double[], int, int)</code> methods
57       * to verify that the input parameters designate a subarray of positive length.
58       * <p>
59       * <ul>
60       * <li>returns <code>true</code> iff the parameters designate a subarray of 
61       * positive length</li>
62       * <li>throws <code>IllegalArgumentException</code> if the array is null or
63       * or the indices are invalid</li>
64       * <li>returns <code>false</li> if the array is non-null, but 
65       * <code>length</code> is 0.
66       * </ul></p>
67       *
68       * @param values the input array
69       * @param begin index of the first array element to include
70       * @param length the number of elements to include
71       * @return true if the parameters are valid and designate a subarray of positive length
72       * @throws IllegalArgumentException if the indices are invalid or the array is null
73       */
74      protected boolean test(
75          final double[] values,
76          final int begin,
77          final int length) {
78  
79          if (values == null) {
80              throw MathRuntimeException.createIllegalArgumentException("input values array is null");
81          }
82          
83          if (begin < 0) {
84              throw MathRuntimeException.createIllegalArgumentException(
85                    "start position cannot be negative ({0})", begin);
86          }
87          
88          if (length < 0) {
89              throw MathRuntimeException.createIllegalArgumentException(
90                    "length cannot be negative ({0})", length);
91          }
92          
93          if (begin + length > values.length) {
94              throw MathRuntimeException.createIllegalArgumentException(
95                    "subarray ends after array end");
96          }
97  
98          if (length == 0) {
99              return false;
100         }
101 
102         return true;
103 
104     }
105 }