1 package org.djutils.stats.summarizers;
2
3 /**
4 * The Counter class defines a statistics event counter.
5 * <p>
6 * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
7 * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
8 * project is distributed under a three-clause BSD-style license, which can be found at
9 * <a href="https://simulation.tudelft.nl/dsol/3.0/license.html" target="_blank">
10 * https://simulation.tudelft.nl/dsol/3.0/license.html</a>. <br>
11 * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
12 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
13 */
14 public class Counter implements CounterInterface
15 {
16 /** */
17 private static final long serialVersionUID = 20200228L;
18
19 /** count represents the value of the counter. */
20 @SuppressWarnings("checkstyle:visibilitymodifier")
21 protected long count = 0;
22
23 /** n represents the number of measurements. */
24 @SuppressWarnings("checkstyle:visibilitymodifier")
25 protected long n = 0;
26
27 /** description refers to the title of this counter. */
28 @SuppressWarnings("checkstyle:visibilitymodifier")
29 protected String description;
30
31 /** the semaphore. */
32 private Object semaphore = new Object();
33
34 /**
35 * Constructs a new Counter.
36 * @param description String; the description for this counter
37 */
38 public Counter(final String description)
39 {
40 this.description = description;
41 initialize();
42 }
43
44 /** {@inheritDoc} */
45 @Override
46 public long getCount()
47 {
48 return this.count;
49 }
50
51 /** {@inheritDoc} */
52 @Override
53 public long getN()
54 {
55 return this.n;
56 }
57
58 /** {@inheritDoc} */
59 @Override
60 public long register(final long value)
61 {
62 synchronized (this.semaphore)
63 {
64 this.count += value;
65 this.n++;
66 }
67 return value;
68 }
69
70 /** {@inheritDoc} */
71 @Override
72 public void initialize()
73 {
74 synchronized (this.semaphore)
75 {
76 this.n = 0;
77 this.count = 0;
78 }
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public String getDescription()
84 {
85 return this.description;
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 public String toString()
91 {
92 return this.description;
93 }
94
95 }