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.math3.stat.descriptive;
018
019import org.apache.commons.math3.exception.util.LocalizedFormats;
020import org.apache.commons.math3.exception.MathIllegalArgumentException;
021import org.apache.commons.math3.exception.NullArgumentException;
022import org.apache.commons.math3.util.MathUtils;
023import org.apache.commons.math3.util.Precision;
024
025/**
026 *
027 * Abstract implementation of the {@link StorelessUnivariateStatistic} interface.
028 * <p>
029 * Provides default <code>evaluate()</code> and <code>incrementAll(double[])</code>
030 * implementations.</p>
031 * <p>
032 * <strong>Note that these implementations are not synchronized.</strong></p>
033 *
034 */
035public abstract class AbstractStorelessUnivariateStatistic
036    extends AbstractUnivariateStatistic
037    implements StorelessUnivariateStatistic {
038
039    /**
040     * This default implementation calls {@link #clear}, then invokes
041     * {@link #increment} in a loop over the the input array, and then uses
042     * {@link #getResult} to compute the return value.
043     * <p>
044     * Note that this implementation changes the internal state of the
045     * statistic.  Its side effects are the same as invoking {@link #clear} and
046     * then {@link #incrementAll(double[])}.</p>
047     * <p>
048     * Implementations may override this method with a more efficient and
049     * possibly more accurate implementation that works directly with the
050     * input array.</p>
051     * <p>
052     * If the array is null, a MathIllegalArgumentException is thrown.</p>
053     * @param values input array
054     * @return the value of the statistic applied to the input array
055     * @throws MathIllegalArgumentException if values is null
056     * @see org.apache.commons.math3.stat.descriptive.UnivariateStatistic#evaluate(double[])
057     */
058    @Override
059    public double evaluate(final double[] values) throws MathIllegalArgumentException {
060        if (values == null) {
061            throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
062        }
063        return evaluate(values, 0, values.length);
064    }
065
066    /**
067     * This default implementation calls {@link #clear}, then invokes
068     * {@link #increment} in a loop over the specified portion of the input
069     * array, and then uses {@link #getResult} to compute the return value.
070     * <p>
071     * Note that this implementation changes the internal state of the
072     * statistic.  Its side effects are the same as invoking {@link #clear} and
073     * then {@link #incrementAll(double[], int, int)}.</p>
074     * <p>
075     * Implementations may override this method with a more efficient and
076     * possibly more accurate implementation that works directly with the
077     * input array.</p>
078     * <p>
079     * If the array is null or the index parameters are not valid, an
080     * MathIllegalArgumentException is thrown.</p>
081     * @param values the input array
082     * @param begin the index of the first element to include
083     * @param length the number of elements to include
084     * @return the value of the statistic applied to the included array entries
085     * @throws MathIllegalArgumentException if the array is null or the indices are not valid
086     * @see org.apache.commons.math3.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int)
087     */
088    @Override
089    public double evaluate(final double[] values, final int begin,
090            final int length) throws MathIllegalArgumentException {
091        if (test(values, begin, length)) {
092            clear();
093            incrementAll(values, begin, length);
094        }
095        return getResult();
096    }
097
098    /**
099     * {@inheritDoc}
100     */
101    @Override
102    public abstract StorelessUnivariateStatistic copy();
103
104    /**
105     * {@inheritDoc}
106     */
107    public abstract void clear();
108
109    /**
110     * {@inheritDoc}
111     */
112    public abstract double getResult();
113
114    /**
115     * {@inheritDoc}
116     */
117    public abstract void increment(final double d);
118
119    /**
120     * This default implementation just calls {@link #increment} in a loop over
121     * the input array.
122     * <p>
123     * Throws IllegalArgumentException if the input values array is null.</p>
124     *
125     * @param values values to add
126     * @throws MathIllegalArgumentException if values is null
127     * @see org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[])
128     */
129    public void incrementAll(double[] values) throws MathIllegalArgumentException {
130        if (values == null) {
131            throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
132        }
133        incrementAll(values, 0, values.length);
134    }
135
136    /**
137     * This default implementation just calls {@link #increment} in a loop over
138     * the specified portion of the input array.
139     * <p>
140     * Throws IllegalArgumentException if the input values array is null.</p>
141     *
142     * @param values  array holding values to add
143     * @param begin   index of the first array element to add
144     * @param length  number of array elements to add
145     * @throws MathIllegalArgumentException if values is null
146     * @see org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[], int, int)
147     */
148    public void incrementAll(double[] values, int begin, int length) throws MathIllegalArgumentException {
149        if (test(values, begin, length)) {
150            int k = begin + length;
151            for (int i = begin; i < k; i++) {
152                increment(values[i]);
153            }
154        }
155    }
156
157    /**
158     * Returns true iff <code>object</code> is an
159     * <code>AbstractStorelessUnivariateStatistic</code> returning the same
160     * values as this for <code>getResult()</code> and <code>getN()</code>
161     * @param object object to test equality against.
162     * @return true if object returns the same value as this
163     */
164    @Override
165    public boolean equals(Object object) {
166        if (object == this ) {
167            return true;
168        }
169       if (object instanceof AbstractStorelessUnivariateStatistic == false) {
170            return false;
171        }
172        AbstractStorelessUnivariateStatistic stat = (AbstractStorelessUnivariateStatistic) object;
173        return Precision.equalsIncludingNaN(stat.getResult(), this.getResult()) &&
174               Precision.equalsIncludingNaN(stat.getN(), this.getN());
175    }
176
177    /**
178     * Returns hash code based on getResult() and getN()
179     *
180     * @return hash code
181     */
182    @Override
183    public int hashCode() {
184        return 31* (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN());
185    }
186
187}