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