View Javadoc
1   package org.djutils.stats.summarizers;
2   
3   import org.djutils.exceptions.Throw;
4   
5   /**
6    * The Counter class defines a statistics event counter.
7    * <p>
8    * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9    * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
10   * project is distributed under a three-clause BSD-style license, which can be found at
11   * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
12   * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
13   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
14   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
15   */
16  public class Counter implements Statistic
17  {
18      /** count represents the value of the counter. */
19      private long count = 0;
20  
21      /** n represents the number of measurements. */
22      private long n = 0;
23  
24      /** description refers to the title of this counter. */
25      private String description;
26  
27      /** the semaphore. */
28      private Object semaphore = new Object();
29  
30      /**
31       * Constructs a new Counter.
32       * @param description the description for this counter
33       */
34      public Counter(final String description)
35      {
36          Throw.whenNull(description, "description cannot be null");
37          this.description = description;
38          initialize();
39      }
40  
41      /**
42       * Returns the current counter value.
43       * @return the counter value
44       */
45      public long getCount()
46      {
47          return this.count;
48      }
49  
50      @Override
51      public long getN()
52      {
53          return this.n;
54      }
55  
56      @Override
57      public void setDescription(final String description)
58      {
59          this.description = description;
60      }
61  
62      /**
63       * Process one observed value.
64       * @param value the value to process
65       * @return the value
66       */
67      public long register(final long value)
68      {
69          synchronized (this.semaphore)
70          {
71              this.count += value;
72              this.n++;
73          }
74          return value;
75      }
76  
77      /**
78       * Initialize the counter.
79       */
80      @Override
81      public void initialize()
82      {
83          synchronized (this.semaphore)
84          {
85              this.n = 0;
86              this.count = 0;
87          }
88      }
89  
90      @Override
91      public String getDescription()
92      {
93          return this.description;
94      }
95  
96      /**
97       * Return a string representing a header for a textual table with a monospaced font that can contain multiple statistics.
98       * @return header for the textual table.
99       */
100     public static String reportHeader()
101     {
102         return "-".repeat(72) + String.format("%n| %-48.48s | %6.6s | %8.8s |%n", "Counter name", "n", "count")
103                 + "-".repeat(72);
104     }
105 
106     @Override
107     public String reportLine()
108     {
109         return String.format("| %-48.48s | %6d | %8d |", getDescription(), getN(), getCount());
110     }
111 
112     /**
113      * Return a string representing a footer for a textual table with a monospaced font that can contain multiple statistics.
114      * @return footer for the textual table
115      */
116     public static String reportFooter()
117     {
118         return "-".repeat(72);
119     }
120 
121     @Override
122     public String toString()
123     {
124         return "Counter [description=" + this.description + ", n=" + this.n + ", count=" + this.count + "]";
125     }
126     
127 }