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