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      /** */
19      private static final long serialVersionUID = 20200228L;
20  
21      /** count represents the value of the counter. */
22      private long count = 0;
23  
24      /** n represents the number of measurements. */
25      private long n = 0;
26  
27      /** description refers to the title of this counter. */
28      private String description;
29  
30      /** the semaphore. */
31      private Object semaphore = new Object();
32  
33      /**
34       * Constructs a new Counter.
35       * @param description the description for this counter
36       */
37      public Counter(final String description)
38      {
39          Throw.whenNull(description, "description cannot be null");
40          this.description = description;
41          initialize();
42      }
43  
44      /**
45       * Returns the current counter value.
46       * @return the counter value
47       */
48      public long getCount()
49      {
50          return this.count;
51      }
52  
53      @Override
54      public long getN()
55      {
56          return this.n;
57      }
58  
59      @Override
60      public void setDescription(final String description)
61      {
62          this.description = description;
63      }
64  
65      /**
66       * Process one observed value.
67       * @param value the value to process
68       * @return the value
69       */
70      public long register(final long value)
71      {
72          synchronized (this.semaphore)
73          {
74              this.count += value;
75              this.n++;
76          }
77          return value;
78      }
79  
80      /**
81       * Initialize the counter.
82       */
83      @Override
84      public void initialize()
85      {
86          synchronized (this.semaphore)
87          {
88              this.n = 0;
89              this.count = 0;
90          }
91      }
92  
93      @Override
94      public String getDescription()
95      {
96          return this.description;
97      }
98  
99      /**
100      * Return a string representing a header for a textual table with a monospaced font that can contain multiple statistics.
101      * @return header for the textual table.
102      */
103     public static String reportHeader()
104     {
105         return "-".repeat(72) + String.format("%n| %-48.48s | %6.6s | %8.8s |%n", "Counter name", "n", "count")
106                 + "-".repeat(72);
107     }
108 
109     @Override
110     public String reportLine()
111     {
112         return String.format("| %-48.48s | %6d | %8d |", getDescription(), getN(), getCount());
113     }
114 
115     /**
116      * Return a string representing a footer for a textual table with a monospaced font that can contain multiple statistics.
117      * @return footer for the textual table
118      */
119     public static String reportFooter()
120     {
121         return "-".repeat(72);
122     }
123 
124     @Override
125     public String toString()
126     {
127         return "Counter [description=" + this.description + ", n=" + this.n + ", count=" + this.count + "]";
128     }
129     
130 }