AbstractUnivariateStatistic.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.MathUtils;

  25. /**
  26.  * Abstract base class for implementations of the
  27.  * {@link UnivariateStatistic} interface.
  28.  */
  29. public abstract class AbstractUnivariateStatistic
  30.     implements UnivariateStatistic {

  31.     /** Stored data. */
  32.     private double[] storedData;

  33.     /** Default constructor. */
  34.     protected AbstractUnivariateStatistic() {
  35.         // This constructor is intentionally empty. Nothing special is needed here.
  36.     }

  37.     /**
  38.      * Copy constructor, creates an identical copy
  39.      * of the {@code original}.
  40.      *
  41.      * @param original the instance to copy
  42.      * @throws org.hipparchus.exception.NullArgumentException  if original is null
  43.      */
  44.     protected AbstractUnivariateStatistic(AbstractUnivariateStatistic original) {
  45.         MathUtils.checkNotNull(original);
  46.         this.storedData = original.storedData != null ? original.storedData.clone() : null;
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public abstract double evaluate(double[] values, int begin, int length)
  51.         throws MathIllegalArgumentException;

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public abstract UnivariateStatistic copy();

  55.     /**
  56.      * Set the data array.
  57.      * <p>
  58.      * The stored value is a copy of the parameter array, not the array itself.
  59.      * </p>
  60.      * @param values data array to store (may be null to remove stored data)
  61.      * @see #evaluate()
  62.      */
  63.     public void setData(final double[] values) {
  64.         storedData = (values == null) ? null : values.clone();
  65.     }

  66.     /**
  67.      * Get a copy of the stored data array.
  68.      * @return copy of the stored data array (may be null)
  69.      */
  70.     public double[] getData() {
  71.         return (storedData == null) ? null : storedData.clone();
  72.     }

  73.     /**
  74.      * Get a reference to the stored data array.
  75.      * @return reference to the stored data array (may be null)
  76.      */
  77.     protected double[] getDataRef() {
  78.         return storedData; // NOPMD - returning an internal array is intentional and documented here
  79.     }

  80.     /**
  81.      * Set the data array.  The input array is copied, not referenced.
  82.      *
  83.      * @param values data array to store
  84.      * @param begin the index of the first element to include
  85.      * @param length the number of elements to include
  86.      * @throws MathIllegalArgumentException if values is null or the indices
  87.      * are not valid
  88.      * @see #evaluate()
  89.      */
  90.     public void setData(final double[] values, final int begin, final int length)
  91.             throws MathIllegalArgumentException {
  92.         MathUtils.checkNotNull(values, LocalizedCoreFormats.INPUT_ARRAY);

  93.         if (begin < 0) {
  94.             throw new MathIllegalArgumentException(LocalizedCoreFormats.START_POSITION, begin);
  95.         }

  96.         if (length < 0) {
  97.             throw new MathIllegalArgumentException(LocalizedCoreFormats.LENGTH, length);
  98.         }

  99.         if (begin + length > values.length) {
  100.             throw new MathIllegalArgumentException(LocalizedCoreFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
  101.                                                 begin + length, values.length, true);
  102.         }
  103.         storedData = new double[length];
  104.         System.arraycopy(values, begin, storedData, 0, length);
  105.     }

  106.     /**
  107.      * Returns the result of evaluating the statistic over the stored data.
  108.      * <p>
  109.      * The stored array is the one which was set by previous calls to
  110.      * {@link #setData(double[])}.
  111.      *
  112.      * @return the value of the statistic applied to the stored data
  113.      * @throws MathIllegalArgumentException if the stored data array is null
  114.      */
  115.     public double evaluate() throws MathIllegalArgumentException {
  116.         return evaluate(storedData);
  117.     }

  118. }