View Javadoc
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.math4.legacy.stat.descriptive;
18  
19  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
20  import org.apache.commons.math4.legacy.exception.NotPositiveException;
21  import org.apache.commons.math4.legacy.exception.NullArgumentException;
22  import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
23  import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
24  import org.apache.commons.math4.legacy.core.MathArrays;
25  
26  /**
27   * Abstract base class for implementations of the {@link UnivariateStatistic} interface.
28   * <p>
29   * Provides a default implementation of <code>evaluate(double[]),</code>
30   * delegating to <code>evaluate(double[], int, int)</code> in the natural way.
31   */
32  public abstract class AbstractUnivariateStatistic
33      implements UnivariateStatistic {
34  
35      /** Stored data. */
36      private double[] storedData;
37  
38      /**
39       * {@inheritDoc}
40       */
41      @Override
42      public double evaluate(final double[] values) throws MathIllegalArgumentException {
43          MathArrays.verifyValues(values, 0, 0);
44          return evaluate(values, 0, values.length);
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public abstract double evaluate(double[] values, int begin, int length)
52          throws MathIllegalArgumentException;
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public abstract UnivariateStatistic copy();
59  
60      /**
61       * Set the data array.
62       * <p>
63       * The stored value is a copy of the parameter array, not the array itself.
64       * </p>
65       * @param values data array to store (may be null to remove stored data)
66       * @see #evaluate()
67       */
68      public void setData(final double[] values) {
69          storedData = (values == null) ? null : values.clone();
70      }
71  
72      /**
73       * Get a copy of the stored data array.
74       * @return copy of the stored data array (may be null)
75       */
76      public double[] getData() {
77          return (storedData == null) ? null : storedData.clone();
78      }
79  
80      /**
81       * Get a reference to the stored data array.
82       * @return reference to the stored data array (may be null)
83       */
84      protected double[] getDataRef() {
85          return storedData;
86      }
87  
88      /**
89       * Set the data array.  The input array is copied, not referenced.
90       *
91       * @param values data array to store
92       * @param begin the index of the first element to include
93       * @param length the number of elements to include
94       * @throws MathIllegalArgumentException if values is null or the indices
95       * are not valid
96       * @see #evaluate()
97       */
98      public void setData(final double[] values, final int begin, final int length)
99              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 }