View Javadoc
1   /**
2    * 
3    */
4   package org.opentrafficsim.water.statistics;
5   
6   import java.io.Serializable;
7   
8   import org.apache.poi.ss.usermodel.Row;
9   import org.apache.poi.ss.usermodel.Sheet;
10  
11  import com.thoughtworks.xstream.annotations.XStreamAlias;
12  import com.thoughtworks.xstream.annotations.XStreamOmitField;
13  
14  /**
15   * <br>
16   * Copyright (c) 2011-2013 TU Delft, Faculty of TBM, Systems and Simulation <br>
17   * This software is licensed without restrictions to Nederlandse Organisatie voor Toegepast Natuurwetenschappelijk
18   * Onderzoek TNO (TNO), Erasmus University Rotterdam, Delft University of Technology, Panteia B.V., Stichting Projecten
19   * Binnenvaart, Ab Ovo Nederland B.V., Modality Software Solutions B.V., and Rijkswaterstaat - Dienst Water, Verkeer en
20   * Leefomgeving, including the right to sub-license sources and derived products to third parties. <br>
21   * 
22   * @version Apr 1, 2013 <br>
23   * @author <a href="http://tudelft.nl/averbraeck">Alexander Verbraeck </a>
24   */
25  @XStreamAlias("counter")
26  public class XCounter implements Serializable
27  {
28      /** */
29      @XStreamOmitField
30      private static final long serialVersionUID = 1L;
31  
32      /** count represents the value of the counter */
33      protected long count = Long.MIN_VALUE;
34  
35      /** n represents the number of measurements */
36      protected long n = Long.MIN_VALUE;
37  
38      /** description refers to the title of this counter */
39      protected String description;
40  
41      /** the semaphore */
42      @XStreamOmitField
43      private Object semaphore = new Object();
44  
45      /**
46       * constructs a new CounterTest
47       * 
48       * @param description the description for this counter
49       */
50      public XCounter(final String description)
51      {
52          super();
53          this.description = description;
54      }
55  
56      /**
57       * Returns the current counter value
58       * 
59       * @return long the counter value
60       */
61      public long getCount()
62      {
63          return this.count;
64      }
65  
66      /**
67       * Returns the current number of observations
68       * 
69       * @return long the number of observations
70       */
71      public long getN()
72      {
73          return this.n;
74      }
75  
76      /**
77       * @param value the value
78       */
79      public void count(final long value)
80      {
81          synchronized (this.semaphore)
82          {
83              this.setCount(this.count + value);
84              this.setN(this.n + 1);
85          }
86      }
87  
88      /**
89       * @see java.lang.Object#toString()
90       */
91      @Override
92      public String toString()
93      {
94          return this.description;
95      }
96  
97      /**
98       * initializes the counter
99       */
100     public void initialize()
101     {
102         synchronized (this.semaphore)
103         {
104             this.setN(0);
105             this.setCount(0);
106         }
107     }
108 
109     /**
110      * is the counter initialized?
111      * 
112      * @return returns whether the counter is initialized
113      */
114     public boolean isInitialized()
115     {
116         return this.n != Long.MIN_VALUE;
117     }
118 
119     /**
120      * sets the count
121      * 
122      * @param count the value
123      */
124     private void setCount(final long count)
125     {
126         this.count = count;
127     }
128 
129     /**
130      * sets n
131      * 
132      * @param n the number of measurements
133      */
134     private void setN(final long n)
135     {
136         this.n = n;
137     }
138 
139     /**
140      * returns the description of the counter
141      * 
142      * @return String the description
143      */
144     public String getDescription()
145     {
146         return this.description;
147     }
148 
149     /**
150      * Write statistics to an excel spreadsheet, starting on row "startRow"
151      * @param sheet the sheet
152      * @param startRow the first row
153      * @return first free row after writing
154      */
155     public int writeToExcel(final Sheet sheet, final int startRow)
156     {
157         int rownr = startRow;
158         Row row = sheet.createRow(rownr);
159         row.createCell(1).setCellValue(description);
160         row.createCell(2).setCellValue("count [n, telling]");
161         row.createCell(3).setCellValue(getN());
162         row.createCell(4).setCellValue(getCount());
163         return rownr + 1;
164     }
165 }