1 package org.djutils.stats.summarizers.event;
2
3 import java.rmi.RemoteException;
4
5 import org.djutils.event.Event;
6 import org.djutils.event.EventListener;
7 import org.djutils.event.EventListenerMap;
8 import org.djutils.event.EventProducer;
9 import org.djutils.event.LocalEventProducer;
10 import org.djutils.exceptions.Throw;
11 import org.djutils.stats.summarizers.Tally;
12 import org.djutils.stats.summarizers.quantileaccumulator.NoStorageAccumulator;
13 import org.djutils.stats.summarizers.quantileaccumulator.QuantileAccumulator;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class EventBasedTally extends Tally implements EventProducer, EventListener
30 {
31
32 private static final long serialVersionUID = 20200228L;
33
34
35 private EventProducer eventProducer = null;
36
37
38
39
40
41
42 public EventBasedTally(final String description, final QuantileAccumulator quantileAccumulator)
43 {
44 this(description, quantileAccumulator, new LocalEventProducer());
45 }
46
47
48
49
50
51 public EventBasedTally(final String description)
52 {
53 this(description, new NoStorageAccumulator());
54 }
55
56
57
58
59
60
61
62 public EventBasedTally(final String description, final EventProducer eventProducer)
63 {
64 this(description, new NoStorageAccumulator(), eventProducer);
65 }
66
67
68
69
70
71
72
73 public EventBasedTally(final String description, final QuantileAccumulator quantileAccumulator,
74 final EventProducer eventProducer)
75 {
76 super(description, quantileAccumulator);
77 Throw.whenNull(eventProducer, "eventProducer cannot be null");
78 this.eventProducer = eventProducer;
79 }
80
81
82 @Override
83 public EventListenerMap getEventListenerMap() throws RemoteException
84 {
85 return this.eventProducer.getEventListenerMap();
86 }
87
88
89 @Override
90 public void initialize()
91 {
92 super.initialize();
93 if (this.eventProducer != null)
94 {
95 try
96 {
97 this.eventProducer.fireEvent(StatisticsEvents.INITIALIZED_EVENT);
98 }
99 catch (RemoteException exception)
100 {
101 throw new RuntimeException(exception);
102 }
103 }
104 }
105
106
107 @Override
108 @SuppressWarnings("checkstyle:designforextension")
109 public void notify(final Event event)
110 {
111 if (!(event.getContent() instanceof Number))
112 {
113 throw new IllegalArgumentException("Tally does not accept " + event);
114 }
115 double value = ((Number) event.getContent()).doubleValue();
116 register(value);
117 }
118
119
120 @Override
121 public double register(final double value)
122 {
123 super.register(value);
124 try
125 {
126 if (hasListeners())
127 {
128 this.eventProducer.fireEvent(StatisticsEvents.OBSERVATION_ADDED_EVENT, value);
129 fireEvents();
130 }
131 }
132 catch (RemoteException exception)
133 {
134 throw new RuntimeException(exception);
135 }
136 return value;
137 }
138
139
140
141
142
143 protected void fireEvents() throws RemoteException
144 {
145 this.eventProducer.fireEvent(StatisticsEvents.N_EVENT, getN());
146 this.eventProducer.fireEvent(StatisticsEvents.MIN_EVENT, getMin());
147 this.eventProducer.fireEvent(StatisticsEvents.MAX_EVENT, getMax());
148 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_MEAN_EVENT, getPopulationMean());
149 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_VARIANCE_EVENT, getPopulationVariance());
150 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_SKEWNESS_EVENT, getPopulationSkewness());
151 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_KURTOSIS_EVENT, getPopulationKurtosis());
152 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_EXCESS_KURTOSIS_EVENT, getPopulationExcessKurtosis());
153 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_STDEV_EVENT, getPopulationStDev());
154 this.eventProducer.fireEvent(StatisticsEvents.SUM_EVENT, getSum());
155 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_MEAN_EVENT, getSampleMean());
156 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_VARIANCE_EVENT, getSampleVariance());
157 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_SKEWNESS_EVENT, getSampleSkewness());
158 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_KURTOSIS_EVENT, getSampleKurtosis());
159 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_EXCESS_KURTOSIS_EVENT, getSampleExcessKurtosis());
160 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_STDEV_EVENT, getSampleStDev());
161 }
162
163
164 @Override
165 @SuppressWarnings("checkstyle:designforextension")
166 public String toString()
167 {
168 return "EventBasedTally" + super.toString().substring(5);
169 }
170
171 }