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