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