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