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 String; the description for this counter
47       */
48      public XCounter(final String description)
49      {
50          this.description = description;
51      }
52  
53      /**
54       * Returns the current counter value.
55       * @return long the counter value
56       */
57      public long getCount()
58      {
59          return this.count;
60      }
61  
62      /**
63       * Returns the current number of observations.
64       * @return long the number of observations
65       */
66      public long getN()
67      {
68          return this.n;
69      }
70  
71      /**
72       * @param value long; the value
73       */
74      public void count(final long value)
75      {
76          synchronized (this.semaphore)
77          {
78              this.setCount(this.count + value);
79              this.setN(this.n + 1);
80          }
81      }
82  
83      /**
84       * @see java.lang.Object#toString()
85       */
86      @Override
87      public String toString()
88      {
89          return this.description;
90      }
91  
92      /**
93       * initializes the counter.
94       */
95      public void initialize()
96      {
97          synchronized (this.semaphore)
98          {
99              this.setN(0);
100             this.setCount(0);
101         }
102     }
103 
104     /**
105      * is the counter initialized?
106      * @return returns whether the counter is initialized
107      */
108     public boolean isInitialized()
109     {
110         return this.n != Long.MIN_VALUE;
111     }
112 
113     /**
114      * sets the count.
115      * @param count long; the value
116      */
117     private void setCount(final long count)
118     {
119         this.count = count;
120     }
121 
122     /**
123      * sets n.
124      * @param n long; the number of measurements.
125      */
126     private void setN(final long n)
127     {
128         this.n = n;
129     }
130 
131     /**
132      * returns the description of the counter.
133      * @return String the description
134      */
135     public String getDescription()
136     {
137         return this.description;
138     }
139 
140     /**
141      * Write statistics to an excel spreadsheet, starting on row "startRow".
142      * @param sheet Sheet; the sheet
143      * @param startRow int; the first row
144      * @return first free row after writing
145      */
146     public int writeToExcel(final Sheet sheet, final int startRow)
147     {
148         int rownr = startRow;
149         Row row = sheet.createRow(rownr);
150         row.createCell(1).setCellValue(description);
151         row.createCell(2).setCellValue("count [n, telling]");
152         row.createCell(3).setCellValue(getN());
153         row.createCell(4).setCellValue(getCount());
154         return rownr + 1;
155     }
156 }