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 * https://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 18 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 package org.hipparchus.stat.descriptive; 23 24 import org.hipparchus.exception.LocalizedCoreFormats; 25 import org.hipparchus.exception.MathIllegalArgumentException; 26 import org.hipparchus.util.MathUtils; 27 28 /** 29 * Abstract base class for implementations of the 30 * {@link UnivariateStatistic} interface. 31 */ 32 public abstract class AbstractUnivariateStatistic 33 implements UnivariateStatistic { 34 35 /** Stored data. */ 36 private double[] storedData; 37 38 /** Default constructor. */ 39 protected AbstractUnivariateStatistic() { 40 // This constructor is intentionally empty. Nothing special is needed here. 41 } 42 43 /** 44 * Copy constructor, creates an identical copy 45 * of the {@code original}. 46 * 47 * @param original the instance to copy 48 * @throws org.hipparchus.exception.NullArgumentException if original is null 49 */ 50 protected AbstractUnivariateStatistic(AbstractUnivariateStatistic original) { 51 MathUtils.checkNotNull(original); 52 this.storedData = original.storedData != null ? original.storedData.clone() : null; 53 } 54 55 /** {@inheritDoc} */ 56 @Override 57 public abstract double evaluate(double[] values, int begin, int length) 58 throws MathIllegalArgumentException; 59 60 /** {@inheritDoc} */ 61 @Override 62 public abstract UnivariateStatistic copy(); 63 64 /** 65 * Set the data array. 66 * <p> 67 * The stored value is a copy of the parameter array, not the array itself. 68 * </p> 69 * @param values data array to store (may be null to remove stored data) 70 * @see #evaluate() 71 */ 72 public void setData(final double[] values) { 73 storedData = (values == null) ? null : values.clone(); 74 } 75 76 /** 77 * Get a copy of the stored data array. 78 * @return copy of the stored data array (may be null) 79 */ 80 public double[] getData() { 81 return (storedData == null) ? null : storedData.clone(); 82 } 83 84 /** 85 * Get a reference to the stored data array. 86 * @return reference to the stored data array (may be null) 87 */ 88 protected double[] getDataRef() { 89 return storedData; // NOPMD - returning an internal array is intentional and documented here 90 } 91 92 /** 93 * Set the data array. The input array is copied, not referenced. 94 * 95 * @param values data array to store 96 * @param begin the index of the first element to include 97 * @param length the number of elements to include 98 * @throws MathIllegalArgumentException if values is null or the indices 99 * are not valid 100 * @see #evaluate() 101 */ 102 public void setData(final double[] values, final int begin, final int length) 103 throws MathIllegalArgumentException { 104 MathUtils.checkNotNull(values, LocalizedCoreFormats.INPUT_ARRAY); 105 106 if (begin < 0) { 107 throw new MathIllegalArgumentException(LocalizedCoreFormats.START_POSITION, begin); 108 } 109 110 if (length < 0) { 111 throw new MathIllegalArgumentException(LocalizedCoreFormats.LENGTH, length); 112 } 113 114 if (begin + length > values.length) { 115 throw new MathIllegalArgumentException(LocalizedCoreFormats.SUBARRAY_ENDS_AFTER_ARRAY_END, 116 begin + length, values.length, true); 117 } 118 storedData = new double[length]; 119 System.arraycopy(values, begin, storedData, 0, length); 120 } 121 122 /** 123 * Returns the result of evaluating the statistic over the stored data. 124 * <p> 125 * The stored array is the one which was set by previous calls to 126 * {@link #setData(double[])}. 127 * 128 * @return the value of the statistic applied to the stored data 129 * @throws MathIllegalArgumentException if the stored data array is null 130 */ 131 public double evaluate() throws MathIllegalArgumentException { 132 return evaluate(storedData); 133 } 134 135 }