View Javadoc
1   package org.djutils.stats.summarizers.event;
2   
3   import java.io.Serializable;
4   
5   import org.djutils.event.Event;
6   import org.djutils.event.EventInterface;
7   import org.djutils.event.EventListenerInterface;
8   import org.djutils.event.EventProducer;
9   import org.djutils.stats.summarizers.Counter;
10  import org.djutils.stats.summarizers.CounterInterface;
11  
12  /**
13   * The Counter class defines a statistics event counter. It extends an EventProducer so it can keep listeners informed about new
14   * observations, and it listens to external events to be able to receive observations, in addition to the register(...) method.
15   * <p>
16   * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
18   * project is distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
20   * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
21   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
22   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
23   */
24  public class EventBasedCounter extends EventProducer implements EventListenerInterface, CounterInterface
25  {
26      /** */
27      private static final long serialVersionUID = 20200228L;
28  
29      /** The wrapped Counter. */
30      private final Counter wrappedCounter;
31  
32      /**
33       * Construct a new EventBasedCounter.
34       * @param description String; the description for this counter
35       */
36      public EventBasedCounter(final String description)
37      {
38          this.wrappedCounter = new Counter(description);
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public Serializable getSourceId()
44      {
45          return this;
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public long getCount()
51      {
52          return this.wrappedCounter.getCount();
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public long getN()
58      {
59          return this.wrappedCounter.getN();
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public void notify(final EventInterface event)
65      {
66          long value = 1;
67          if (event.getContent() instanceof Number)
68          {
69              value = Math.round(((Number) event.getContent()).doubleValue());
70          }
71          else
72          {
73              throw new IllegalArgumentException("event content for counter not a number but of type " + event.getClass());
74          }
75          register(value);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public long register(final long value)
81      {
82          this.wrappedCounter.register(value);
83          if (hasListeners())
84          {
85              fireEvent(new Event(StatisticsEvents.OBSERVATION_ADDED_EVENT, this, value));
86              fireEvents();
87          }
88          return value;
89      }
90  
91      /**
92       * Method that can be overridden to fire own events or additional events when ingesting an observation.
93       */
94      protected void fireEvents()
95      {
96          fireEvent(new Event(StatisticsEvents.N_EVENT, this, getN()));
97          fireEvent(new Event(StatisticsEvents.COUNT_EVENT, this, getCount()));
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public void initialize()
103     {
104         this.wrappedCounter.initialize();
105         fireEvent(new Event(StatisticsEvents.INITIALIZED_EVENT, this, null));
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public String getDescription()
111     {
112         return this.wrappedCounter.getDescription();
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public String toString()
118     {
119         return this.wrappedCounter.toString();
120     }
121 
122 }