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 java.io.Serializable; 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: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $ 33 */ 34 public abstract class AbstractUnivariateStatistic 35 implements UnivariateStatistic, Serializable { 36 37 /** Serialization UID */ 38 private static final long serialVersionUID = -8007759382851708045L; 39 40 /** 41 * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[]) 42 */ 43 public double evaluate(final double[] values) { 44 test(values, 0, 0); 45 return evaluate(values, 0, values.length); 46 } 47 48 /** 49 * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int) 50 */ 51 public abstract double evaluate(final double[] values, final int begin, final int length); 52 53 /** 54 * This method is used by <code>evaluate(double[], int, int)</code> methods 55 * to verify that the input parameters designate a subarray of positive length. 56 * <p> 57 * <ul> 58 * <li>returns <code>true</code> iff the parameters designate a subarray of 59 * positive length</li> 60 * <li>throws <code>IllegalArgumentException</code> if the array is null or 61 * or the indices are invalid</li> 62 * <li>returns <code>false</li> if the array is non-null, but 63 * <code>length</code> is 0. 64 * </ul></p> 65 * 66 * @param values the input array 67 * @param begin index of the first array element to include 68 * @param length the number of elements to include 69 * @return true if the parameters are valid and designate a subarray of positive length 70 * @throws IllegalArgumentException if the indices are invalid or the array is null 71 */ 72 protected boolean test( 73 final double[] values, 74 final int begin, 75 final int length) { 76 77 if (values == null) { 78 throw new IllegalArgumentException("input value array is null"); 79 } 80 81 if (begin < 0) { 82 throw new IllegalArgumentException("start position cannot be negative"); 83 } 84 85 if (length < 0) { 86 throw new IllegalArgumentException("length cannot be negative"); 87 } 88 89 if (begin + length > values.length) { 90 throw new IllegalArgumentException( 91 "begin + length > values.length"); 92 } 93 94 if (length == 0) { 95 return false; 96 } 97 98 return true; 99 100 } 101 }