1 /**
2 * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of Konstanz nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 package org.perfidix.meter;
28
29 /**
30 * Simple meter to count given ticks. The ticks are not resetted afterwards, the
31 *
32 * @author Alexander Onea, neue Couch
33 * @author Sebastian Graf, University of Konstanz
34 */
35 public final class CountingMeter extends AbstractMeter {
36
37 /**
38 * Constant to store the default name.
39 */
40 private static final String DEFAULTNAME = "CountingMeter";
41
42 /**
43 * Constant to store the default unit.
44 */
45 private static final String DEFAULTUNIT = "ticks";
46
47 /**
48 * Constant to store the default unit description.
49 */
50 private static final String DEFAULTUNITDESC = "Simple ticks for counting";
51
52 /**
53 * Counter for ticks.
54 */
55 private transient long counter;
56
57 /**
58 * Name for this counting meter.
59 */
60 private transient final String name;
61
62 /**
63 * Unit for this counting meter.
64 */
65 private transient final String unit;
66
67 /**
68 * Short description for the unit.
69 */
70 private transient final String unitDescription;
71
72 /**
73 * Constructor, generates a simple CountingMeter.
74 */
75 public CountingMeter() {
76 this(DEFAULTNAME, DEFAULTUNIT, DEFAULTUNITDESC);
77 }
78
79 /**
80 * Constructor with a given name.
81 *
82 * @param paramName
83 * the name of this CountingMeter
84 */
85 public CountingMeter(final String paramName) {
86 this(paramName, DEFAULTUNIT, DEFAULTUNITDESC);
87 }
88
89 /**
90 * Constructor with a given name and a given unit.
91 *
92 * @param paramName
93 * the name of this CountingMeter
94 * @param paramUnit
95 * the unit of this CountingMeter
96 */
97 public CountingMeter(final String paramName, final String paramUnit) {
98 this(paramName, paramUnit, DEFAULTUNITDESC);
99 }
100
101 /**
102 * Constructor with a given name and a given unit and a given unit
103 * description.
104 *
105 * @param paramName
106 * the name of this CountingMeter
107 * @param paramUnit
108 * the unit of this CountingMeter
109 * @param paramUnitDesc
110 * the description of this CountingMeter
111 */
112 public CountingMeter(final String paramName, final String paramUnit, final String paramUnitDesc) {
113 super();
114 name = paramName;
115 unit = paramUnit;
116 unitDescription = paramUnitDesc;
117 counter = 0;
118 }
119
120 /**
121 * Getting the name of this CountingMeter.
122 *
123 * @return the name of this CountingMeter
124 */
125 @Override
126 public String getName() {
127 return name;
128 }
129
130 /**
131 * The meter is ticking one forward.
132 */
133 public void tick() {
134 counter++;
135 }
136
137 /**
138 * Getting the value of this CountingMeter.
139 *
140 * @return the counter's value.
141 */
142 @Override
143 public double getValue() {
144 return counter;
145 }
146
147 /**
148 * Getting the name of this CountingMeter.
149 *
150 * @return the unit of this CountingMeter
151 */
152 @Override
153 public String getUnit() {
154 return unit;
155 }
156
157 /**
158 * Getting the description of this CountingMeter.
159 *
160 * @return the description of this CountingMeter
161 */
162 @Override
163 public String getUnitDescription() {
164 return unitDescription;
165 }
166
167 /** {@inheritDoc} */
168 @Override
169 public int hashCode() {
170 final int prime = 31;
171 int result = prime;
172 if (name == null) {
173 result = prime * result;
174 } else {
175 result = prime * result + name.hashCode();
176 }
177 if (unit == null) {
178 result = prime * result;
179 } else {
180 result = prime * result + unit.hashCode();
181 }
182 if (unitDescription == null) {
183 result = prime * result;
184 } else {
185 result = prime * result + unitDescription.hashCode();
186 }
187 return result;
188 }
189
190 /** {@inheritDoc} */
191 @Override
192 public boolean equals(final Object obj) {
193 boolean returnVal = true;
194 if (this == obj) {
195 returnVal = true;
196 }
197 if (getClass() != obj.getClass()) {
198 returnVal = false;
199 }
200 final CountingMeter other = (CountingMeter)obj;
201 if (name == null) {
202 if (other.name != null) {
203 returnVal = false;
204 }
205 } else {
206 if (!name.equals(other.name)) {
207 returnVal = false;
208 }
209 }
210 if (unit == null) {
211 if (other.unit != null) {
212 returnVal = false;
213 }
214 } else if (!unit.equals(other.unit)) {
215 returnVal = false;
216 }
217 if (unitDescription == null) {
218 if (other.unitDescription == null) {
219 if (!unitDescription.equals(other.unitDescription)) {
220 returnVal = false;
221 }
222 } else {
223 returnVal = false;
224 }
225 }
226 return returnVal;
227 }
228
229 }