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 @Override
82 public EventListenerMap getEventListenerMap() throws RemoteException
83 {
84 return this.eventProducer.getEventListenerMap();
85 }
86
87 @Override
88 public void initialize()
89 {
90 super.initialize();
91 if (this.eventProducer != null)
92 {
93 try
94 {
95 this.eventProducer.fireEvent(StatisticsEvents.INITIALIZED_EVENT);
96 }
97 catch (RemoteException exception)
98 {
99 throw new RuntimeException(exception);
100 }
101 }
102 }
103
104 @Override
105 @SuppressWarnings("checkstyle:designforextension")
106 public void notify(final Event event)
107 {
108 if (!(event.getContent() instanceof Number))
109 {
110 throw new IllegalArgumentException("Tally does not accept " + event);
111 }
112 double value = ((Number) event.getContent()).doubleValue();
113 register(value);
114 }
115
116 @Override
117 public double register(final double value)
118 {
119 super.register(value);
120 try
121 {
122 if (hasListeners())
123 {
124 this.eventProducer.fireEvent(StatisticsEvents.OBSERVATION_ADDED_EVENT, value);
125 fireEvents();
126 }
127 }
128 catch (RemoteException exception)
129 {
130 throw new RuntimeException(exception);
131 }
132 return value;
133 }
134
135
136
137
138
139 protected void fireEvents() throws RemoteException
140 {
141 this.eventProducer.fireEvent(StatisticsEvents.N_EVENT, getN());
142 this.eventProducer.fireEvent(StatisticsEvents.MIN_EVENT, getMin());
143 this.eventProducer.fireEvent(StatisticsEvents.MAX_EVENT, getMax());
144 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_MEAN_EVENT, getPopulationMean());
145 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_VARIANCE_EVENT, getPopulationVariance());
146 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_SKEWNESS_EVENT, getPopulationSkewness());
147 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_KURTOSIS_EVENT, getPopulationKurtosis());
148 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_EXCESS_KURTOSIS_EVENT, getPopulationExcessKurtosis());
149 this.eventProducer.fireEvent(StatisticsEvents.POPULATION_STDEV_EVENT, getPopulationStDev());
150 this.eventProducer.fireEvent(StatisticsEvents.SUM_EVENT, getSum());
151 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_MEAN_EVENT, getSampleMean());
152 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_VARIANCE_EVENT, getSampleVariance());
153 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_SKEWNESS_EVENT, getSampleSkewness());
154 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_KURTOSIS_EVENT, getSampleKurtosis());
155 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_EXCESS_KURTOSIS_EVENT, getSampleExcessKurtosis());
156 this.eventProducer.fireEvent(StatisticsEvents.SAMPLE_STDEV_EVENT, getSampleStDev());
157 }
158
159 @Override
160 @SuppressWarnings("checkstyle:designforextension")
161 public String toString()
162 {
163 return "EventBasedTally" + super.toString().substring(5);
164 }
165
166 }