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