001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive;
017:
018: import org.apache.commons.math.util.MathUtils;
019: import java.io.Serializable;
020:
021: /**
022: *
023: * Abstract implementation of the {@link StorelessUnivariateStatistic} interface.
024: * <p>
025: * Provides default <code>evaluate()</code> and <code>incrementAll(double[])<code>
026: * implementations.
027: * <p>
028: * <strong>Note that these implementations are not synchronized.</strong>
029: *
030: * @version $Revision: 355770 $ $Date: 2005-12-10 12:48:57 -0700 (Sat, 10 Dec 2005) $
031: */
032: public abstract class AbstractStorelessUnivariateStatistic extends
033: AbstractUnivariateStatistic implements
034: StorelessUnivariateStatistic, Serializable {
035:
036: /** Serialization UID */
037: private static final long serialVersionUID = -44915725420072521L;
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[])}.
047: * <p>
048: * Implementations may override this method with a more efficient
049: * implementation that works directly with the input array.
050: * <p>
051: * If the array is null, an IllegalArgumentException is thrown.
052: *
053: * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[])
054: */
055: public double evaluate(final double[] values) {
056: if (values == null) {
057: throw new IllegalArgumentException(
058: "input value array is null");
059: }
060: return evaluate(values, 0, values.length);
061: }
062:
063: /**
064: * This default implementation calls {@link #clear}, then invokes
065: * {@link #increment} in a loop over the specified portion of the input
066: * array, and then uses {@link #getResult} to compute the return value.
067: * <p>
068: * Note that this implementation changes the internal state of the
069: * statistic. Its side effects are the same as invoking {@link #clear} and
070: * then {@link #incrementAll(double[], int, int)}.
071: * <p>
072: * Implementations may override this method with a more efficient
073: * implementation that works directly with the input array.
074: * <p>
075: * If the array is null or the index parameters are not valid, an
076: * IllegalArgumentException is thrown.
077: *
078: * @see org.apache.commons.math.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int)
079: */
080: public double evaluate(final double[] values, final int begin,
081: final int length) {
082: if (test(values, begin, length)) {
083: clear();
084: incrementAll(values, begin, length);
085: }
086: return getResult();
087: }
088:
089: /**
090: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
091: */
092: public abstract void clear();
093:
094: /**
095: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
096: */
097: public abstract double getResult();
098:
099: /**
100: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
101: */
102: public abstract void increment(final double d);
103:
104: /**
105: * This default implementation just calls {@link #increment} in a loop over
106: * the input array.
107: * <p>
108: * Throws IllegalArgumentException if the input values array is null.
109: *
110: * @param values values to add
111: * @throws IllegalArgumentException if values is null
112: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[])
113: */
114: public void incrementAll(double[] values) {
115: if (values == null) {
116: throw new IllegalArgumentException(
117: "input values array is null");
118: }
119: incrementAll(values, 0, values.length);
120: }
121:
122: /**
123: * This default implementation just calls {@link #increment} in a loop over
124: * the specified portion of the input array.
125: * <p>
126: * Throws IllegalArgumentException if the input values array is null.
127: *
128: * @param values array holding values to add
129: * @param begin index of the first array element to add
130: * @param length number of array elements to add
131: * @throws IllegalArgumentException if values is null
132: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[], int, int)
133: */
134: public void incrementAll(double[] values, int begin, int length) {
135: if (test(values, begin, length)) {
136: int k = begin + length;
137: for (int i = begin; i < k; i++) {
138: increment(values[i]);
139: }
140: }
141: }
142:
143: /**
144: * Returns true iff <code>object</code> is an
145: * <code>AbstractStorelessUnivariateStatistic</code> returning the same
146: * values as this for <code>getResult()</code> and <code>getN()</code>
147: * @param object object to test equality against.
148: * @return true if object returns the same value as this
149: */
150: public boolean equals(Object object) {
151: if (object == this ) {
152: return true;
153: }
154: if (object instanceof AbstractStorelessUnivariateStatistic == false) {
155: return false;
156: }
157: AbstractStorelessUnivariateStatistic stat = (AbstractStorelessUnivariateStatistic) object;
158: return (MathUtils.equals(stat.getResult(), this .getResult()) && MathUtils
159: .equals(stat.getN(), this .getN()));
160: }
161:
162: /**
163: * Returns hash code based on getResult() and getN()
164: *
165: * @return hash code
166: */
167: public int hashCode() {
168: return 31 * (31 + MathUtils.hash(getResult()))
169: + MathUtils.hash(getN());
170: }
171:
172: }
|