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