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