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;
17  
18  import java.io.Serializable;
19  
20  /**
21   * Abstract base class for all implementations of the 
22   * {@link UnivariateStatistic} interface.
23   * <p>
24   * Provides a default implementation of <code>evaluate(double[]),</code> 
25   * delegating to <code>evaluate(double[], int, int)</code> in the natural way.
26   * <p>
27   * Also includes a <code>test</code> method that performs generic parameter
28   * validation for the <code>evaluate</code> methods.
29   * <p>
30   * 
31   * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
32   */
33  public abstract class AbstractUnivariateStatistic
34      implements UnivariateStatistic, Serializable {
35      
36      /** Serialization UID */
37      private static final long serialVersionUID = -8007759382851708045L;
38  
39      /**
40       * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[])
41       */
42      public double evaluate(final double[] values) {
43          test(values, 0, 0);
44          return evaluate(values, 0, values.length);
45      }
46  
47      /**
48       * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int)
49       */
50      public abstract double evaluate(final double[] values, final int begin, final int length);
51  
52      /**
53       * This method is used by <code>evaluate(double[], int, int)</code> methods
54       * to verify that the input parameters designate a subarray of positive length.
55       * <p>
56       * <ul>
57       * <li>returns <code>true</code> iff the parameters designate a subarray of 
58       * positive length</li>
59       * <li>throws <code>IllegalArgumentException</code> if the array is null or
60       * or the indices are invalid</li>
61       * <li>returns <code>false</li> if the array is non-null, but 
62       * <code>length</code> is 0.
63       * </ul>
64       *
65       * @param values the input array
66       * @param begin index of the first array element to include
67       * @param length the number of elements to include
68       * @return true if the parameters are valid and designate a subarray of positive length
69       * @throws IllegalArgumentException if the indices are invalid or the array is null
70       */
71      protected boolean test(
72          final double[] values,
73          final int begin,
74          final int length) {
75  
76          if (values == null) {
77              throw new IllegalArgumentException("input value array is null");
78          }
79          
80          if (begin < 0) {
81              throw new IllegalArgumentException("start position cannot be negative");
82          }
83          
84          if (length < 0) {
85              throw new IllegalArgumentException("length cannot be negative");
86          }
87          
88          if (begin + length > values.length) {
89              throw new IllegalArgumentException(
90                  "begin + length > values.length");
91          }
92  
93          if (length == 0) {
94              return false;
95          }
96  
97          return true;
98  
99      }
100 }