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 java.io.Serializable;
019:
020: import org.apache.commons.math.util.ResizableDoubleArray;
021:
022: /**
023: * Default implementation of
024: * {@link org.apache.commons.math.stat.descriptive.DescriptiveStatistics}.
025: *
026: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
027: */
028: public class DescriptiveStatisticsImpl extends DescriptiveStatistics
029: implements Serializable {
030:
031: /** Serializable version identifier */
032: private static final long serialVersionUID = -1868088725461221010L;
033:
034: /** hold the window size **/
035: protected int windowSize;
036:
037: /**
038: * Stored data values
039: */
040: protected ResizableDoubleArray eDA;
041:
042: /**
043: * Construct a DescriptiveStatisticsImpl with infinite window
044: */
045: public DescriptiveStatisticsImpl() {
046: this (INFINITE_WINDOW);
047: }
048:
049: /**
050: * Construct a DescriptiveStatisticsImpl with finite window
051: * @param window the finite window size.
052: */
053: public DescriptiveStatisticsImpl(int window) {
054: super ();
055: eDA = new ResizableDoubleArray();
056: setWindowSize(window);
057: }
058:
059: /**
060: * Access the window size.
061: * @return the current window size.
062: */
063: public int getWindowSize() {
064: return windowSize;
065: }
066:
067: /**
068: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getValues()
069: */
070: public double[] getValues() {
071:
072: double[] copiedArray = new double[eDA.getNumElements()];
073: System.arraycopy(eDA.getElements(), 0, copiedArray, 0, eDA
074: .getNumElements());
075: return copiedArray;
076: }
077:
078: /**
079: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getElement(int)
080: */
081: public double getElement(int index) {
082: return eDA.getElement(index);
083: }
084:
085: /**
086: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getN()
087: */
088: public long getN() {
089: return eDA.getNumElements();
090: }
091:
092: /**
093: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#addValue(double)
094: */
095: public void addValue(double v) {
096: if (windowSize != INFINITE_WINDOW) {
097: if (getN() == windowSize) {
098: eDA.addElementRolling(v);
099: } else if (getN() < windowSize) {
100: eDA.addElement(v);
101: }
102: } else {
103: eDA.addElement(v);
104: }
105: }
106:
107: /**
108: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#clear()
109: */
110: public void clear() {
111: eDA.clear();
112: }
113:
114: /**
115: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#setWindowSize(int)
116: */
117: public void setWindowSize(int windowSize) {
118: if (windowSize < 1) {
119: if (windowSize != INFINITE_WINDOW) {
120: throw new IllegalArgumentException(
121: "window size must be positive.");
122: }
123: }
124:
125: this .windowSize = windowSize;
126:
127: // We need to check to see if we need to discard elements
128: // from the front of the array. If the windowSize is less than
129: // the current number of elements.
130: if (windowSize != INFINITE_WINDOW
131: && windowSize < eDA.getNumElements()) {
132: eDA.discardFrontElements(eDA.getNumElements() - windowSize);
133: }
134: }
135:
136: /**
137: * Apply the given statistic to this univariate collection.
138: * @param stat the statistic to apply
139: * @return the computed value of the statistic.
140: */
141: public double apply(UnivariateStatistic stat) {
142: return stat.evaluate(eDA.getValues(), eDA.start(), eDA
143: .getNumElements());
144: }
145: }
|