1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.adapters;
21
22 import java.util.Map;
23
24 import com.cosylab.gui.displayers.DataConsumer;
25 import com.cosylab.gui.displayers.DataState;
26 import com.cosylab.gui.displayers.DoubleConsumer;
27 import com.cosylab.util.CommonException;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class DataInterceptor implements DoubleConsumer
49 {
50
51
52
53
54
55
56
57
58
59 class Value
60 {
61 double value;
62 long timestamp;
63
64
65
66
67
68
69
70 public Value(long timestamp, double value)
71 {
72 this.value = value;
73 this.timestamp = timestamp;
74 }
75 }
76
77
78 private DoubleConsumer consumer;
79
80
81 private long timeInterval;
82 private double deviation;
83 private double average;
84 private int numberOfValues;
85 private double sum;
86 private double sumSquare;
87 private long startTime;
88 private long lastTime;
89 private Value maxValue;
90 private Value minValue;
91 private DataState state;
92
93
94
95
96 public DataInterceptor()
97 {
98 this.timeInterval = 0;
99 reset();
100 }
101
102
103
104
105
106
107
108 public DataInterceptor(DoubleConsumer consumer, long timeInterval)
109 {
110 this.consumer = consumer;
111 this.timeInterval = timeInterval;
112 reset();
113 }
114
115
116
117
118
119
120 public void setTimeInterval(long timeInterval)
121 {
122 this.timeInterval = timeInterval;
123 reset();
124 }
125
126
127
128
129
130
131 public long getTimeInterval()
132 {
133 return timeInterval;
134 }
135
136
137
138
139
140
141 public double getDeviation()
142 {
143 return deviation;
144 }
145
146
147
148
149
150
151 public double getAverage()
152 {
153 return average;
154 }
155
156
157
158
159 public void updateValue(long timestamp, double value)
160 throws CommonException
161 {
162 if (numberOfValues == 0) {
163 startTime = timestamp;
164 maxValue = new Value(timestamp, value);
165 minValue = new Value(timestamp, value);
166 }
167
168 sumSquare += value*value;
169 sum += value;
170 numberOfValues++;
171
172 if (timestamp > startTime + timeInterval) {
173 lastTime = timestamp;
174 dispatch();
175 }
176 }
177
178 private void dispatch() throws CommonException
179 {
180 average = sum / numberOfValues;
181
182 deviation = Math.sqrt(sumSquare/numberOfValues - average*average);
183
184
185 consumer.updateValue(startTime, average);
186 if (maxValue.timestamp <= minValue.timestamp) {
187 consumer.updateValue(maxValue.timestamp, maxValue.value);
188 consumer.updateValue(minValue.timestamp, minValue.value);
189 } else {
190 consumer.updateValue(minValue.timestamp, minValue.value);
191 consumer.updateValue(maxValue.timestamp, maxValue.value);
192 }
193 consumer.updateValue(lastTime, average);
194
195 reset();
196 }
197
198 private void reset()
199 {
200 sum = 0;
201 numberOfValues = 0;
202 sumSquare = 0;
203 maxValue = null;
204 minValue = null;
205 }
206
207
208
209
210
211
212
213 public void setConsumer(DoubleConsumer newConsumer)
214 {
215 this.consumer = newConsumer;
216 reset();
217 }
218
219
220
221
222 public DataConsumer getDataConsumer(Class type)
223 {
224 if (type.isAssignableFrom(this.getClass())) {
225 return this;
226 }
227
228 return null;
229 }
230
231
232
233
234 public DataConsumer getDefaultDataConsumer()
235 {
236 return this;
237 }
238
239
240
241
242 public void updateDataState(DataState datastate)
243 {
244 this.state = datastate;
245 }
246
247
248
249
250 public void setCharacteristics(Map characteristics)
251 {
252 consumer.setCharacteristics(characteristics);
253 }
254
255
256
257
258 public String getName()
259 {
260 return consumer.getName();
261 }
262
263
264
265
266 public String[] getSupportedCharacteristics()
267 {
268 return consumer.getSupportedCharacteristics();
269 }
270
271
272
273
274 public Class[] getSupportedConsumerTypes()
275 {
276 return new Class[]{ DoubleConsumer.class };
277 }
278 }
279
280