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