View Javadoc
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.Counter;
10  
11  /**
12   * The Counter class defines a statistics event counter. It embeds an EventProducer so it can keep listeners informed about new
13   * observations, and it listens to external events to be able to receive observations, in addition to the register(...) method.
14   * <p>
15   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
17   * project is distributed under a three-clause BSD-style license, which can be found at
18   * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
19   * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
20   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
21   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
22   */
23  public class EventBasedCounter extends Counter implements EventProducer, EventListener
24  {
25      /** The embedded EventProducer. */
26      private EventProducer eventProducer = null;
27  
28      /**
29       * Construct a new EventBasedCounter.
30       * @param description the description for this counter
31       */
32      public EventBasedCounter(final String description)
33      {
34          this(description, new LocalEventProducer());
35      }
36  
37      /**
38       * Construct a new EventBasedCounter with a specific EventProducer, e.g. a remote one.
39       * @param description the description for this counter
40       * @param eventProducer the EventProducer to embed and use in this statistic
41       */
42      public EventBasedCounter(final String description, final EventProducer eventProducer)
43      {
44          super(description);
45          Throw.whenNull(eventProducer, "eventProducer cannot be null");
46          this.eventProducer = eventProducer;
47      }
48  
49      @Override
50      public EventListenerMap getEventListenerMap()
51      {
52          return this.eventProducer.getEventListenerMap();
53      }
54  
55      @Override
56      public void initialize()
57      {
58          super.initialize();
59          if (this.eventProducer != null)
60          {
61              this.eventProducer.fireEvent(StatisticsEvents.INITIALIZED_EVENT);
62          }
63      }
64  
65      @Override
66      public void notify(final Event event)
67      {
68          long value = 1;
69          if (event.getContent() instanceof Number)
70          {
71              value = Math.round(((Number) event.getContent()).doubleValue());
72          }
73          else
74          {
75              throw new IllegalArgumentException("event content for counter not a number but of type " + event.getClass());
76          }
77          register(value);
78      }
79  
80      @Override
81      public long register(final long value)
82      {
83          super.register(value);
84          if (hasListeners())
85          {
86              this.eventProducer.fireEvent(StatisticsEvents.OBSERVATION_ADDED_EVENT, value);
87              fireEvents();
88          }
89          return value;
90      }
91  
92      /**
93       * Method that can be overridden to fire own events or additional events when registering an observation.
94       */
95      protected void fireEvents()
96      {
97          this.eventProducer.fireEvent(StatisticsEvents.N_EVENT, getN());
98          this.eventProducer.fireEvent(StatisticsEvents.COUNT_EVENT, getCount());
99      }
100 
101 }