1 package org.djutils.stats.summarizers;
2
3
4
5
6
7
8
9
10
11
12
13
14 public class Counter implements CounterInterface
15 {
16
17 private static final long serialVersionUID = 20200228L;
18
19
20 @SuppressWarnings("checkstyle:visibilitymodifier")
21 protected long count = 0;
22
23
24 @SuppressWarnings("checkstyle:visibilitymodifier")
25 protected long n = 0;
26
27
28 @SuppressWarnings("checkstyle:visibilitymodifier")
29 protected String description;
30
31
32 private Object semaphore = new Object();
33
34
35
36
37
38 public Counter(final String description)
39 {
40 this.description = description;
41 initialize();
42 }
43
44
45 @Override
46 public long getCount()
47 {
48 return this.count;
49 }
50
51
52 @Override
53 public long getN()
54 {
55 return this.n;
56 }
57
58
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
71 @Override
72 public void initialize()
73 {
74 synchronized (this.semaphore)
75 {
76 this.n = 0;
77 this.count = 0;
78 }
79 }
80
81
82 @Override
83 public String getDescription()
84 {
85 return this.description;
86 }
87
88
89 @Override
90 public String toString()
91 {
92 return this.description;
93 }
94
95 }