View Javadoc

1   /*
2    * @(#)CounterTest.java Apr 3, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.jstats.statistics;
11  
12  import javax.swing.table.TableModel;
13  
14  import nl.tudelft.simulation.event.Event;
15  import nl.tudelft.simulation.event.EventInterface;
16  import nl.tudelft.simulation.event.EventListenerInterface;
17  import nl.tudelft.simulation.event.EventType;
18  
19  /***
20   * The Counter class defines a statistics event counter.
21   * <p>
22   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
23   * University of Technology </a>, the Netherlands. <br>
24   * See for project information <a
25   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
26   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
27   * License (GPL) </a>, no warranty <br>
28   * 
29   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
30   *         Jacobs </a>
31   * @version 1.12, 2004-03-18
32   * @since 1.2
33   */
34  public class Counter extends StatisticsObject implements EventListenerInterface
35  {
36  	/*** COUNT_EVENT is fired whenever setCount() is invoked */
37  	public static final EventType COUNT_EVENT = new EventType("COUNT_EVENT");
38  
39  	/*** N_EVENT is fired on every new measurement */
40  	public static final EventType N_EVENT = new EventType("N_EVENT");
41  
42  	/***
43  	 * count represents the value of the counter
44  	 * 
45  	 * @uml.property name="count"
46  	 */
47  	protected long count = Long.MIN_VALUE;
48  
49  	/***
50  	 * n represents the number of measurements
51  	 * 
52  	 * @uml.property name="n"
53  	 */
54  	protected long n = Long.MIN_VALUE;
55  
56  	/***
57  	 * description refers to the title of this counter
58  	 * 
59  	 * @uml.property name="description"
60  	 */
61  	protected String description;
62  
63  
64  	/*** the semaphore */
65  	private Object semaphore = new Object();
66  
67  	/***
68  	 * constructs a new CounterTest
69  	 * 
70  	 * @param description the description for this counter
71  	 */
72  	public Counter(final String description)
73  	{
74  		super();
75  		this.description = description;
76  	}
77  
78  	/***
79  	 * Returns the current counter value
80  	 * 
81  	 * @return long the counter value
82  	 * 
83  	 * @uml.property name="count"
84  	 */
85  	public long getCount()
86  	{
87  		return this.count;
88  	}
89  
90  	/***
91  	 * Returns the current number of observations
92  	 * 
93  	 * @return long the number of observations
94  	 * 
95  	 * @uml.property name="n"
96  	 */
97  	public long getN()
98  	{
99  		return this.n;
100 	}
101 
102 
103 	/***
104 	 * @see nl.tudelft.simulation.event.EventListenerInterface
105 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
106 	 */
107 	public void notify(final EventInterface event)
108 	{
109 		if (!(event.getContent() instanceof Number))
110 		{
111 			throw new IllegalArgumentException("event value !=Number.lang");
112 		}
113 		synchronized (this.semaphore)
114 		{
115 			this.setCount(this.count
116 					+ new Long(event.getContent().toString()).longValue());
117 			this.setN(this.n + 1);
118 			if (!super.listeners.isEmpty())
119 			{
120 				this.fireEvent(Counter.COUNT_EVENT, this.count);
121 				this.fireEvent(Counter.N_EVENT, this.n);
122 			}
123 		}
124 	}
125 
126 	/***
127 	 * @see java.lang.Object#toString()
128 	 */
129 	public String toString()
130 	{
131 		return this.description;
132 	}
133 
134 	/***
135 	 * initializes the counter
136 	 */
137 	public void initialize()
138 	{
139 		synchronized (this.semaphore)
140 		{
141 			this.setN(0);
142 			this.setCount(0);
143 		}
144 	}
145 
146 	/***
147 	 * is the counter initialized?
148 	 * 
149 	 * @return returns whether the counter is initialized
150 	 */
151 	public boolean isInitialized()
152 	{
153 		return this.n != Long.MIN_VALUE;
154 	}
155 
156 	/***
157 	 * sets the count
158 	 * 
159 	 * @param count the value
160 	 * 
161 	 * @uml.property name="count"
162 	 */
163 	private void setCount(final long count)
164 	{
165 		this.count = count;
166 		this.fireEvent(new Event(COUNT_EVENT, this, new Long(this.count)));
167 	}
168 
169 	/***
170 	 * sets n
171 	 * 
172 	 * @param n the number of measurements
173 	 * 
174 	 * @uml.property name="n"
175 	 */
176 	private void setN(final long n)
177 	{
178 		this.n = n;
179 		this.fireEvent(new Event(N_EVENT, this, new Long(this.n)));
180 	}
181 
182 	/***
183 	 * returns the description of the counter
184 	 * 
185 	 * @return String the description
186 	 * 
187 	 * @uml.property name="description"
188 	 */
189 	public String getDescription()
190 	{
191 		return this.description;
192 	}
193 
194 	/***
195 	 * @see nl.tudelft.simulation.jstats.statistics.StatisticsObject #getTable()
196 	 */
197 	public TableModel getTable()
198 	{
199 		String[] columnNames = {"field", "value"};
200 		EventType[] eventTypes = {null, Counter.N_EVENT, Counter.COUNT_EVENT};
201 		StatisticsTableModel result = new StatisticsTableModel(columnNames,
202 				eventTypes, 3);
203 		this.addListener(result, Counter.N_EVENT, false);
204 		this.addListener(result, Counter.COUNT_EVENT, false);
205 
206 		result.setValueAt("name", 0, 0);
207 		result.setValueAt("n", 1, 0);
208 		result.setValueAt("count", 2, 0);
209 		result.setValueAt(this.description, 0, 1);
210 		result.setValueAt(new Long(this.n), 1, 1);
211 		result.setValueAt(new Long(this.count), 2, 1);
212 		return result;
213 	}
214 }