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: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.commons.math.MathException;
023: import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
024: import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
025: import org.apache.commons.math.util.DefaultTransformer;
026: import org.apache.commons.math.util.NumberTransformer;
027:
028: /**
029: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
030: */
031: public class ListUnivariateImpl extends DescriptiveStatistics implements
032: Serializable {
033:
034: /** Serializable version identifier */
035: private static final long serialVersionUID = -8837442489133392138L;
036:
037: /**
038: * Holds a reference to a list - GENERICs are going to make
039: * out lives easier here as we could only accept List<Number>
040: */
041: protected List list;
042:
043: /** Number Transformer maps Objects to Number for us. */
044: protected NumberTransformer transformer;
045:
046: /** hold the window size **/
047: protected int windowSize = DescriptiveStatistics.INFINITE_WINDOW;
048:
049: /**
050: * No argument Constructor
051: */
052: public ListUnivariateImpl() {
053: this (new ArrayList());
054: }
055:
056: /**
057: * Construct a ListUnivariate with a specific List.
058: * @param list The list that will back this DescriptiveStatistics
059: */
060: public ListUnivariateImpl(List list) {
061: this (list, new DefaultTransformer());
062: }
063:
064: /**
065: * Construct a ListUnivariate with a specific List.
066: * @param list The list that will back this DescriptiveStatistics
067: * @param transformer the number transformer used to convert the list items.
068: */
069: public ListUnivariateImpl(List list, NumberTransformer transformer) {
070: super ();
071: this .list = list;
072: this .transformer = transformer;
073: }
074:
075: /**
076: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getValues()
077: */
078: public double[] getValues() {
079:
080: int length = list.size();
081:
082: // If the window size is not INFINITE_WINDOW AND
083: // the current list is larger that the window size, we need to
084: // take into account only the last n elements of the list
085: // as definied by windowSize
086:
087: if (windowSize != DescriptiveStatistics.INFINITE_WINDOW
088: && windowSize < list.size()) {
089: length = list.size()
090: - Math.max(0, list.size() - windowSize);
091: }
092:
093: // Create an array to hold all values
094: double[] copiedArray = new double[length];
095:
096: for (int i = 0; i < copiedArray.length; i++) {
097: copiedArray[i] = getElement(i);
098: }
099: return copiedArray;
100: }
101:
102: /**
103: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getElement(int)
104: */
105: public double getElement(int index) {
106:
107: double value = Double.NaN;
108:
109: int calcIndex = index;
110:
111: if (windowSize != DescriptiveStatistics.INFINITE_WINDOW
112: && windowSize < list.size()) {
113: calcIndex = (list.size() - windowSize) + index;
114: }
115:
116: try {
117: value = transformer.transform(list.get(calcIndex));
118: } catch (MathException e) {
119: e.printStackTrace();
120: }
121:
122: return value;
123: }
124:
125: /**
126: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getN()
127: */
128: public long getN() {
129: int n = 0;
130:
131: if (windowSize != DescriptiveStatistics.INFINITE_WINDOW) {
132: if (list.size() > windowSize) {
133: n = windowSize;
134: } else {
135: n = list.size();
136: }
137: } else {
138: n = list.size();
139: }
140: return n;
141: }
142:
143: /**
144: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#addValue(double)
145: */
146: public void addValue(double v) {
147: list.add(new Double(v));
148: }
149:
150: /**
151: * Adds an object to this list.
152: * @param o Object to add to the list
153: */
154: public void addObject(Object o) {
155: list.add(o);
156: }
157:
158: /**
159: * Clears all statistics.
160: * <p>
161: * <strong>N.B.: </strong> This method has the side effect of clearing the underlying list.
162: */
163: public void clear() {
164: list.clear();
165: }
166:
167: /**
168: * Apply the given statistic to this univariate collection.
169: * @param stat the statistic to apply
170: * @return the computed value of the statistic.
171: */
172: public double apply(UnivariateStatistic stat) {
173: double[] v = this .getValues();
174:
175: if (v != null) {
176: return stat.evaluate(v, 0, v.length);
177: }
178: return Double.NaN;
179: }
180:
181: /**
182: * Access the number transformer.
183: * @return the number transformer.
184: */
185: public NumberTransformer getTransformer() {
186: return transformer;
187: }
188:
189: /**
190: * Modify the number transformer.
191: * @param transformer the new number transformer.
192: */
193: public void setTransformer(NumberTransformer transformer) {
194: this .transformer = transformer;
195: }
196:
197: /**
198: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#setWindowSize(int)
199: */
200: public synchronized void setWindowSize(int windowSize) {
201: this .windowSize = windowSize;
202: //Discard elements from the front of the list if the windowSize is less than
203: // the size of the list.
204: int extra = list.size() - windowSize;
205: for (int i = 0; i < extra; i++) {
206: list.remove(0);
207: }
208: }
209:
210: /**
211: * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getWindowSize
212: */
213: public int getWindowSize() {
214: return windowSize;
215: }
216:
217: }
|