001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math4.legacy.stat.descriptive; 018 019import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException; 020import org.apache.commons.math4.legacy.exception.NotPositiveException; 021import org.apache.commons.math4.legacy.exception.NullArgumentException; 022import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException; 023import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; 024import org.apache.commons.math4.legacy.core.MathArrays; 025 026/** 027 * Abstract base class for implementations of the {@link UnivariateStatistic} interface. 028 * <p> 029 * Provides a default implementation of <code>evaluate(double[]),</code> 030 * delegating to <code>evaluate(double[], int, int)</code> in the natural way. 031 */ 032public abstract class AbstractUnivariateStatistic 033 implements UnivariateStatistic { 034 035 /** Stored data. */ 036 private double[] storedData; 037 038 /** 039 * {@inheritDoc} 040 */ 041 @Override 042 public double evaluate(final double[] values) throws MathIllegalArgumentException { 043 MathArrays.verifyValues(values, 0, 0); 044 return evaluate(values, 0, values.length); 045 } 046 047 /** 048 * {@inheritDoc} 049 */ 050 @Override 051 public abstract double evaluate(double[] values, int begin, int length) 052 throws MathIllegalArgumentException; 053 054 /** 055 * {@inheritDoc} 056 */ 057 @Override 058 public abstract UnivariateStatistic copy(); 059 060 /** 061 * Set the data array. 062 * <p> 063 * The stored value is a copy of the parameter array, not the array itself. 064 * </p> 065 * @param values data array to store (may be null to remove stored data) 066 * @see #evaluate() 067 */ 068 public void setData(final double[] values) { 069 storedData = (values == null) ? null : values.clone(); 070 } 071 072 /** 073 * Get a copy of the stored data array. 074 * @return copy of the stored data array (may be null) 075 */ 076 public double[] getData() { 077 return (storedData == null) ? null : storedData.clone(); 078 } 079 080 /** 081 * Get a reference to the stored data array. 082 * @return reference to the stored data array (may be null) 083 */ 084 protected double[] getDataRef() { 085 return storedData; 086 } 087 088 /** 089 * Set the data array. The input array is copied, not referenced. 090 * 091 * @param values data array to store 092 * @param begin the index of the first element to include 093 * @param length the number of elements to include 094 * @throws MathIllegalArgumentException if values is null or the indices 095 * are not valid 096 * @see #evaluate() 097 */ 098 public void setData(final double[] values, final int begin, final int length) 099 throws MathIllegalArgumentException { 100 if (values == null) { 101 throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); 102 } 103 104 if (begin < 0) { 105 throw new NotPositiveException(LocalizedFormats.START_POSITION, begin); 106 } 107 108 if (length < 0) { 109 throw new NotPositiveException(LocalizedFormats.LENGTH, length); 110 } 111 112 if (begin + length > values.length) { 113 throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END, 114 begin + length, values.length, true); 115 } 116 storedData = new double[length]; 117 System.arraycopy(values, begin, storedData, 0, length); 118 } 119 120 /** 121 * Returns the result of evaluating the statistic over the stored data. 122 * <p> 123 * The stored array is the one which was set by previous calls to {@link #setData(double[])}. 124 * </p> 125 * @return the value of the statistic applied to the stored data 126 * @throws MathIllegalArgumentException if the stored data array is null 127 */ 128 public double evaluate() throws MathIllegalArgumentException { 129 return evaluate(storedData); 130 } 131}