1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.stat.descriptive;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.math.MathException;
24 import org.apache.commons.math.stat.descriptive.UnivariateStatistic;
25 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
26 import org.apache.commons.math.util.DefaultTransformer;
27 import org.apache.commons.math.util.NumberTransformer;
28
29
30
31
32 public class ListUnivariateImpl extends DescriptiveStatistics implements Serializable {
33
34
35 private static final long serialVersionUID = -8837442489133392138L;
36
37
38
39
40
41 protected List list;
42
43
44 protected NumberTransformer transformer;
45
46
47
48
49 public ListUnivariateImpl(){
50 this(new ArrayList());
51 }
52
53
54
55
56
57 public ListUnivariateImpl(List list) {
58 this(list, new DefaultTransformer());
59 }
60
61
62
63
64
65
66 public ListUnivariateImpl(List list, NumberTransformer transformer) {
67 super();
68 this.list = list;
69 this.transformer = transformer;
70 }
71
72
73
74
75 public double[] getValues() {
76
77 int length = list.size();
78
79
80
81
82
83
84 if (windowSize != DescriptiveStatistics.INFINITE_WINDOW &&
85 windowSize < list.size())
86 {
87 length = list.size() - Math.max(0, list.size() - windowSize);
88 }
89
90
91 double[] copiedArray = new double[length];
92
93 for (int i = 0; i < copiedArray.length; i++) {
94 copiedArray[i] = getElement(i);
95 }
96 return copiedArray;
97 }
98
99
100
101
102 public double getElement(int index) {
103
104 double value = Double.NaN;
105
106 int calcIndex = index;
107
108 if (windowSize != DescriptiveStatistics.INFINITE_WINDOW &&
109 windowSize < list.size())
110 {
111 calcIndex = (list.size() - windowSize) + index;
112 }
113
114
115 try {
116 value = transformer.transform(list.get(calcIndex));
117 } catch (MathException e) {
118 e.printStackTrace();
119 }
120
121 return value;
122 }
123
124
125
126
127 public long getN() {
128 int n = 0;
129
130 if (windowSize != DescriptiveStatistics.INFINITE_WINDOW) {
131 if (list.size() > windowSize) {
132 n = windowSize;
133 } else {
134 n = list.size();
135 }
136 } else {
137 n = list.size();
138 }
139 return n;
140 }
141
142
143
144
145 public void addValue(double v) {
146 list.add(new Double(v));
147 }
148
149
150
151
152
153 public void addObject(Object o) {
154 list.add(o);
155 }
156
157
158
159
160
161
162 public void clear() {
163 list.clear();
164 }
165
166
167
168
169
170
171 public double apply(UnivariateStatistic stat) {
172 double[] v = this.getValues();
173
174 if (v != null) {
175 return stat.evaluate(v, 0, v.length);
176 }
177 return Double.NaN;
178 }
179
180
181
182
183
184 public NumberTransformer getTransformer() {
185 return transformer;
186 }
187
188
189
190
191
192 public void setTransformer(NumberTransformer transformer) {
193 this.transformer = transformer;
194 }
195
196
197
198
199 public synchronized void setWindowSize(int windowSize) {
200 this.windowSize = windowSize;
201
202
203 int extra = list.size() - windowSize;
204 for (int i = 0; i < extra; i++) {
205 list.remove(0);
206 }
207 }
208
209
210
211
212 public synchronized int getWindowSize() {
213 return windowSize;
214 }
215
216 }