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(String description)
50      {
51          super();
52          this.description = description;
53      }
54  
55      /**
56       * Returns the current counter value for a key
57       * @param key String; the string key for the counter
58       * @return long the counter value
59       */
60      public long getFrequency(final String key)
61      {
62          return this.frequency.get(key);
63      }
64  
65      /**
66       * Returns all counters
67       * @return the counter values
68       */
69      public SortedMap<String, Long> getFrequencies()
70      {
71          return this.frequency;
72      }
73  
74      /**
75       * Returns the current number of observations
76       * @return long the number of observations
77       */
78      public long getN()
79      {
80          return this.n;
81      }
82  
83      /**
84       * count frequency
85       * @param key String; the key to count the value under
86       * @param value long; the value
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      * count 1
102      * @param key String; key
103      */
104     public void count(final String key)
105     {
106         this.count(key, 1L);
107     }
108 
109     /**
110      * @see java.lang.Object#toString()
111      */
112     @Override
113     public String toString()
114     {
115         return this.description;
116     }
117 
118     /**
119      * initializes the counter
120      */
121     public void initialize()
122     {
123         synchronized (this.semaphore)
124         {
125             this.setN(0);
126             this.frequency.clear();
127         }
128     }
129 
130     /**
131      * is the counter initialized?
132      * @return returns whether the counter is initialized
133      */
134     public boolean isInitialized()
135     {
136         return this.n != Long.MIN_VALUE;
137     }
138 
139     /**
140      * sets n
141      * @param n long; the number of measurements
142      */
143     private void setN(final long n)
144     {
145         this.n = n;
146     }
147 
148     /**
149      * returns the description of the counter
150      * @return String the description
151      */
152     public String getDescription()
153     {
154         return this.description;
155     }
156 
157     /**
158      * Write statistics to an excel spreadsheet, starting on row "startRow"
159      * @param sheet Sheet; the excel sheet to write to
160      * @param startRow int; the first row of writing
161      * @return first free row after writing
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 }