1 package org.djutils.stats.summarizers.event;
2
3 import java.io.Serializable;
4 import java.util.Calendar;
5
6 import org.djutils.event.Event;
7 import org.djutils.event.EventInterface;
8 import org.djutils.event.EventListenerInterface;
9 import org.djutils.event.EventProducer;
10 import org.djutils.event.TimedEvent;
11 import org.djutils.stats.summarizers.TimestampTallyInterface;
12 import org.djutils.stats.summarizers.TimestampWeightedTally;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class EventBasedTimestampWeightedTally extends EventProducer implements EventListenerInterface, TimestampTallyInterface
29 {
30
31 private static final long serialVersionUID = 20200228L;
32
33
34 private TimestampWeightedTally wrappedTimestampWeightedTally;
35
36
37
38
39
40 public EventBasedTimestampWeightedTally(final String description)
41 {
42 this.wrappedTimestampWeightedTally = new TimestampWeightedTally(description);
43 initialize();
44 }
45
46
47 @Override
48 public Serializable getSourceId()
49 {
50 return this;
51 }
52
53
54 @Override
55 public void initialize()
56 {
57 this.wrappedTimestampWeightedTally.initialize();
58 fireEvent(new Event(StatisticsEvents.INITIALIZED_EVENT, this, null));
59 }
60
61
62 @Override
63 public final boolean isActive()
64 {
65 return this.wrappedTimestampWeightedTally.isActive();
66 }
67
68
69 @Override
70 public void endObservations(final Number timestamp)
71 {
72 this.wrappedTimestampWeightedTally.endObservations(timestamp);
73 fireEvents(timestamp.doubleValue(), this.wrappedTimestampWeightedTally.getLastValue());
74 }
75
76
77 @Override
78 public void endObservations(final Calendar timestamp)
79 {
80 this.wrappedTimestampWeightedTally.endObservations(timestamp);
81 fireEvents(timestamp.getTimeInMillis(), this.wrappedTimestampWeightedTally.getLastValue());
82 }
83
84
85 @Override
86 public void notify(final EventInterface event)
87 {
88 if (event instanceof TimedEvent<?>)
89 {
90 TimedEvent<?> timedEvent = (TimedEvent<?>) event;
91 double value = 0.0;
92 if (event.getContent() instanceof Number)
93 {
94 value = ((Number) event.getContent()).doubleValue();
95 }
96 else
97 {
98 throw new IllegalArgumentException(
99 "EventBasedTimestampWeightedTally.notify: Content " + event.getContent() + " should be a Number");
100 }
101 Object timestamp = timedEvent.getTimeStamp();
102 if (timestamp instanceof Number)
103 {
104 register(((Number) timestamp).doubleValue(), value);
105 }
106 else if (timestamp instanceof Calendar)
107 {
108 register(((Calendar) timestamp).getTimeInMillis(), value);
109 }
110 else
111 {
112 throw new IllegalArgumentException(
113 "EventBasedTimestampWeightedTally.notify: timestamp should be a Number or Calendar");
114 }
115 }
116 else
117 {
118 throw new IllegalArgumentException("EventBasedTimestampWeightedTally.notify: Event should be a TimedEvent");
119 }
120 }
121
122
123
124
125
126
127
128 public double register(final Calendar timestamp, final double value)
129 {
130 this.wrappedTimestampWeightedTally.register(timestamp, value);
131 fireEvents(timestamp, value);
132 return value;
133 }
134
135
136
137
138
139
140
141
142 public <T extends Number & Comparable<T>> double register(final T timestamp, final double value)
143 {
144 this.wrappedTimestampWeightedTally.register(timestamp, value);
145 fireEvents(timestamp, value);
146 return value;
147 }
148
149
150
151
152
153
154
155 private <T extends Serializable & Comparable<T>> void fireEvents(final T timestamp, final double value)
156 {
157 if (hasListeners())
158 {
159 this.fireEvent(
160 new Event(StatisticsEvents.TIMESTAMPED_OBSERVATION_ADDED_EVENT, this, new Object[] {timestamp, value}));
161 fireEvents(timestamp);
162 }
163 }
164
165
166
167
168
169
170 protected <T extends Serializable & Comparable<T>> void fireEvents(final T timestamp)
171 {
172 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_N_EVENT, this, getN(), timestamp));
173 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_MIN_EVENT, this, getMin(), timestamp));
174 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_MAX_EVENT, this, getMax(), timestamp));
175 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_POPULATION_MEAN_EVENT, this,
176 getWeightedPopulationMean(), timestamp));
177 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_POPULATION_VARIANCE_EVENT, this,
178 getWeightedPopulationVariance(), timestamp));
179 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_POPULATION_STDEV_EVENT, this,
180 getWeightedPopulationStDev(), timestamp));
181 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SUM_EVENT, this, getWeightedSum(), timestamp));
182 fireTimedEvent(
183 new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SAMPLE_MEAN_EVENT, this, getWeightedSampleMean(), timestamp));
184 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SAMPLE_VARIANCE_EVENT, this,
185 getWeightedSampleVariance(), timestamp));
186 fireTimedEvent(new TimedEvent<T>(StatisticsEvents.TIMED_WEIGHTED_SAMPLE_STDEV_EVENT, this, getWeightedSampleStDev(),
187 timestamp));
188 }
189
190
191 @Override
192 public final String getDescription()
193 {
194 return this.wrappedTimestampWeightedTally.getDescription();
195 }
196
197
198 @Override
199 public final long getN()
200 {
201 return this.wrappedTimestampWeightedTally.getN();
202 }
203
204
205 @Override
206 public final double getMax()
207 {
208 return this.wrappedTimestampWeightedTally.getMax();
209 }
210
211
212 @Override
213 public final double getMin()
214 {
215 return this.wrappedTimestampWeightedTally.getMin();
216 }
217
218
219 @Override
220 public final double getWeightedSampleMean()
221 {
222 return this.wrappedTimestampWeightedTally.getWeightedSampleMean();
223 }
224
225
226 @Override
227 public final double getWeightedSampleStDev()
228 {
229 return this.wrappedTimestampWeightedTally.getWeightedSampleStDev();
230 }
231
232
233 @Override
234 public final double getWeightedPopulationStDev()
235 {
236 return this.wrappedTimestampWeightedTally.getWeightedPopulationStDev();
237 }
238
239
240 @Override
241 public final double getWeightedSampleVariance()
242 {
243 return this.wrappedTimestampWeightedTally.getWeightedSampleVariance();
244 }
245
246
247 @Override
248 public final double getWeightedPopulationVariance()
249 {
250 return this.wrappedTimestampWeightedTally.getWeightedPopulationVariance();
251 }
252
253
254 @Override
255 public final double getWeightedSum()
256 {
257 return this.wrappedTimestampWeightedTally.getWeightedSum();
258 }
259
260
261 @Override
262 @SuppressWarnings("checkstyle:designforextension")
263 public String toString()
264 {
265 return this.wrappedTimestampWeightedTally.toString();
266 }
267
268 }