1 package org.djutils.stats.summarizers.event;
2
3 import java.io.Serializable;
4 import java.rmi.RemoteException;
5
6 import org.djutils.event.Event;
7 import org.djutils.event.EventListener;
8 import org.djutils.event.EventListenerMap;
9 import org.djutils.event.EventProducer;
10 import org.djutils.event.LocalEventProducer;
11 import org.djutils.exceptions.Throw;
12 import org.djutils.stats.summarizers.WeightedTally;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class EventBasedWeightedTally extends WeightedTally implements EventProducer, EventListener
28 {
29
30 private static final long serialVersionUID = 20200228L;
31
32
33 private EventProducer eventProducer = null;
34
35
36
37
38
39 public EventBasedWeightedTally(final String description)
40 {
41 this(description, new LocalEventProducer());
42 }
43
44
45
46
47
48
49 public EventBasedWeightedTally(final String description, final EventProducer eventProducer)
50 {
51 super(description);
52 Throw.whenNull(eventProducer, "eventProducer cannot be null");
53 this.eventProducer = eventProducer;
54 }
55
56
57 @Override
58 public EventListenerMap getEventListenerMap() throws RemoteException
59 {
60 return this.eventProducer.getEventListenerMap();
61 }
62
63
64 @Override
65 public void initialize()
66 {
67 super.initialize();
68 if (this.eventProducer != null)
69 {
70 try
71 {
72 this.eventProducer.fireEvent(StatisticsEvents.INITIALIZED_EVENT);
73 }
74 catch (RemoteException exception)
75 {
76 throw new RuntimeException(exception);
77 }
78 }
79 }
80
81
82 @Override
83 public void notify(final Event event)
84 {
85
86 Object[] content = (Object[]) event.getContent();
87 double weight = ((Number) content[0]).doubleValue();
88 double value = ((Number) content[1]).doubleValue();
89 register(weight, value);
90 }
91
92
93
94
95
96
97
98 @Override
99 public double register(final double weight, final double value)
100 {
101 super.register(weight, value);
102 try
103 {
104 if (hasListeners())
105 {
106 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_OBSERVATION_ADDED_EVENT,
107 new Serializable[] {weight, value});
108 fireEvents();
109 }
110 }
111 catch (RemoteException exception)
112 {
113 throw new RuntimeException(exception);
114 }
115 return value;
116 }
117
118
119
120
121
122 protected void fireEvents() throws RemoteException
123 {
124 this.eventProducer.fireEvent(StatisticsEvents.N_EVENT, getN());
125 this.eventProducer.fireEvent(StatisticsEvents.MIN_EVENT, getMin());
126 this.eventProducer.fireEvent(StatisticsEvents.MAX_EVENT, getMax());
127 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_POPULATION_MEAN_EVENT, getWeightedPopulationMean());
128 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_POPULATION_VARIANCE_EVENT, getWeightedPopulationVariance());
129 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_POPULATION_STDEV_EVENT, getWeightedPopulationStDev());
130 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SUM_EVENT, getWeightedSum());
131 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SAMPLE_MEAN_EVENT, getWeightedSampleMean());
132 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SAMPLE_VARIANCE_EVENT, getWeightedSampleVariance());
133 this.eventProducer.fireEvent(StatisticsEvents.WEIGHTED_SAMPLE_STDEV_EVENT, getWeightedSampleStDev());
134 }
135
136
137 @Override
138 public String toString()
139 {
140 return super.toString();
141 }
142
143 }