1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }