View Javadoc
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   * <br>
18   * Copyright (c) 2011-2013 TU Delft, Faculty of TBM, Systems and Simulation <br>
19   * This software is licensed without restrictions to Nederlandse Organisatie voor Toegepast Natuurwetenschappelijk
20   * Onderzoek TNO (TNO), Erasmus University Rotterdam, Delft University of Technology, Panteia B.V., Stichting Projecten
21   * Binnenvaart, Ab Ovo Nederland B.V., Modality Software Solutions B.V., and Rijkswaterstaat - Dienst Water, Verkeer en
22   * Leefomgeving, including the right to sub-license sources and derived products to third parties. <br>
23   * 
24   * @version Mar 24, 2013 <br>
25   * @author <a href="http://tudelft.nl/averbraeck">Alexander Verbraeck </a>
26   */
27  @XStreamAlias("frequency")
28  public class XFrequency implements Serializable
29  {
30      /** */
31      @XStreamOmitField
32      private static final long serialVersionUID = 1L;
33  
34      /** count represents the values of the counters */
35      protected SortedMap<String, Long> frequency = new TreeMap<String, Long>();
36  
37      /** n represents the number of measurements */
38      protected long n = Long.MIN_VALUE;
39  
40      /** description refers to the title of this counter */
41      protected String description;
42  
43      /** the semaphore */
44      @XStreamOmitField
45      private Object semaphore = new Object();
46  
47      /**
48       * @param description the description of the statistic
49       */
50      public XFrequency(String description)
51      {
52          super();
53          this.description = description;
54      }
55  
56      /**
57       * Returns the current counter value for a key
58       * 
59       * @param key the string key for the counter
60       * @return long the counter value
61       */
62      public long getFrequency(final String key)
63      {
64          return this.frequency.get(key);
65      }
66  
67      /**
68       * Returns all counters
69       * 
70       * @return the counter values
71       */
72      public SortedMap<String, Long> getFrequencies()
73      {
74          return this.frequency;
75      }
76  
77      /**
78       * Returns the current number of observations
79       * 
80       * @return long the number of observations
81       */
82      public long getN()
83      {
84          return this.n;
85      }
86  
87      /**
88       * count frequency
89       * 
90       * @param key the key to count the value under
91       * @param value the value
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      * count 1
107      * 
108      * @param key key
109      */
110     public void count(final String key)
111     {
112         this.count(key, 1L);
113     }
114 
115     /**
116      * @see java.lang.Object#toString()
117      */
118     @Override
119     public String toString()
120     {
121         return this.description;
122     }
123 
124     /**
125      * initializes the counter
126      */
127     public void initialize()
128     {
129         synchronized (this.semaphore)
130         {
131             this.setN(0);
132             this.frequency.clear();
133         }
134     }
135 
136     /**
137      * is the counter initialized?
138      * 
139      * @return returns whether the counter is initialized
140      */
141     public boolean isInitialized()
142     {
143         return this.n != Long.MIN_VALUE;
144     }
145 
146     /**
147      * sets n
148      * 
149      * @param n the number of measurements
150      */
151     private void setN(final long n)
152     {
153         this.n = n;
154     }
155 
156     /**
157      * returns the description of the counter
158      * 
159      * @return String the description
160      */
161     public String getDescription()
162     {
163         return this.description;
164     }
165 
166     /**
167      * Write statistics to an excel spreadsheet, starting on row "startRow"
168      * @param sheet the excel sheet to write to 
169      * @param startRow the first row of writing
170      * @return first free row after writing
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 }