1
2
3
4 package org.opentrafficsim.water.statistics;
5
6 import java.io.Serializable;
7
8 import org.apache.poi.ss.usermodel.Row;
9 import org.apache.poi.ss.usermodel.Sheet;
10
11 import com.thoughtworks.xstream.annotations.XStreamAlias;
12 import com.thoughtworks.xstream.annotations.XStreamOmitField;
13
14
15
16
17
18
19
20
21
22
23
24
25 @XStreamAlias("counter")
26 public class XCounter implements Serializable
27 {
28
29 @XStreamOmitField
30 private static final long serialVersionUID = 1L;
31
32
33 protected long count = Long.MIN_VALUE;
34
35
36 protected long n = Long.MIN_VALUE;
37
38
39 protected String description;
40
41
42 @XStreamOmitField
43 private Object semaphore = new Object();
44
45
46
47
48
49
50 public XCounter(final String description)
51 {
52 super();
53 this.description = description;
54 }
55
56
57
58
59
60
61 public long getCount()
62 {
63 return this.count;
64 }
65
66
67
68
69
70
71 public long getN()
72 {
73 return this.n;
74 }
75
76
77
78
79 public void count(final long value)
80 {
81 synchronized (this.semaphore)
82 {
83 this.setCount(this.count + value);
84 this.setN(this.n + 1);
85 }
86 }
87
88
89
90
91 @Override
92 public String toString()
93 {
94 return this.description;
95 }
96
97
98
99
100 public void initialize()
101 {
102 synchronized (this.semaphore)
103 {
104 this.setN(0);
105 this.setCount(0);
106 }
107 }
108
109
110
111
112
113
114 public boolean isInitialized()
115 {
116 return this.n != Long.MIN_VALUE;
117 }
118
119
120
121
122
123
124 private void setCount(final long count)
125 {
126 this.count = count;
127 }
128
129
130
131
132
133
134 private void setN(final long n)
135 {
136 this.n = n;
137 }
138
139
140
141
142
143
144 public String getDescription()
145 {
146 return this.description;
147 }
148
149
150
151
152
153
154
155 public int writeToExcel(final Sheet sheet, final int startRow)
156 {
157 int rownr = startRow;
158 Row row = sheet.createRow(rownr);
159 row.createCell(1).setCellValue(description);
160 row.createCell(2).setCellValue("count [n, telling]");
161 row.createCell(3).setCellValue(getN());
162 row.createCell(4).setCellValue(getCount());
163 return rownr + 1;
164 }
165 }