KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > Counter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * An integer counter class.
28  *
29  * @version <tt>$Revision: 1958 $</tt>
30  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
31  */

32 public class Counter
33    implements Serializable JavaDoc, Cloneable JavaDoc
34 {
35    /** The current count */
36    private int count;
37
38    /**
39     * Construct a Counter with a starting value.
40     *
41     * @param count Starting value for counter.
42     */

43    public Counter(final int count) {
44       this.count = count;
45    }
46
47    /**
48     * Construct a Counter.
49     */

50    public Counter() {}
51
52    /**
53     * Increment the counter. (Optional operation)
54     *
55     * @return The incremented value of the counter.
56     */

57    public int increment() {
58       return ++count;
59    }
60
61    /**
62     * Decrement the counter. (Optional operation)
63     *
64     * @return The decremented value of the counter.
65     */

66    public int decrement() {
67       return --count;
68    }
69
70    /**
71     * Return the current value of the counter.
72     *
73     * @return The current value of the counter.
74     */

75    public int getCount() {
76       return count;
77    }
78
79    /**
80     * Reset the counter to zero. (Optional operation)
81     */

82    public void reset() {
83       this.count = 0;
84    }
85
86    /**
87     * Check if the given object is equal to this.
88     *
89     * @param obj Object to test equality with.
90     * @return True if object is equal to this.
91     */

92    public boolean equals(final Object JavaDoc obj) {
93       if (obj == this) return true;
94
95       if (obj != null && obj.getClass() == getClass()) {
96          return ((Counter)obj).count == count;
97       }
98       
99       return false;
100    }
101
102    /**
103     * Return a string representation of this.
104     *
105     * @return A string representation of this.
106     */

107    public String JavaDoc toString() {
108       return String.valueOf(count);
109    }
110
111    /**
112     * Return a cloned copy of this object.
113     *
114     * @return A cloned copy of this object.
115     */

116    public Object JavaDoc clone() {
117       try {
118          return super.clone();
119       }
120       catch (CloneNotSupportedException JavaDoc e) {
121          throw new InternalError JavaDoc();
122       }
123    }
124
125
126    /////////////////////////////////////////////////////////////////////////
127
// Wrappers //
128
/////////////////////////////////////////////////////////////////////////
129

130    /**
131     * Base wrapper class for other wrappers.
132     */

133    private static class Wrapper
134       extends Counter
135    {
136       /** The wrapped counter */
137       protected final Counter counter;
138
139       public Wrapper(final Counter counter) {
140          this.counter = counter;
141       }
142
143       public int increment() {
144          return counter.increment();
145       }
146
147       public int decrement() {
148          return counter.decrement();
149       }
150
151       public int getCount() {
152          return counter.getCount();
153       }
154
155       public void reset() {
156          counter.reset();
157       }
158
159       public boolean equals(final Object JavaDoc obj) {
160          return counter.equals(obj);
161       }
162
163       public String JavaDoc toString() {
164          return counter.toString();
165       }
166
167       public Object JavaDoc clone() {
168          return counter.clone();
169       }
170    }
171
172    /**
173     * Return a synchronized counter.
174     *
175     * @param counter Counter to synchronize.
176     * @return Synchronized counter.
177     */

178    public static Counter makeSynchronized(final Counter counter) {
179       return new Wrapper(counter) {
180             public synchronized int increment() {
181                return this.counter.increment();
182             }
183
184             public synchronized int decrement() {
185                return this.counter.decrement();
186             }
187
188             public synchronized int getCount() {
189                return this.counter.getCount();
190             }
191
192             public synchronized void reset() {
193                this.counter.reset();
194             }
195
196             public synchronized int hashCode() {
197                return this.counter.hashCode();
198             }
199
200             public synchronized boolean equals(final Object JavaDoc obj) {
201                return this.counter.equals(obj);
202             }
203
204             public synchronized String JavaDoc toString() {
205                return this.counter.toString();
206             }
207
208             public synchronized Object JavaDoc clone() {
209                return this.counter.clone();
210             }
211          };
212    }
213
214    /**
215     * Returns a directional counter.
216     *
217     * @param counter Counter to make directional.
218     * @param increasing True to create an increasing only
219     * or false to create a decreasing only.
220     * @return A directional counter.
221     */

222    public static Counter makeDirectional(final Counter counter,
223                                          final boolean increasing)
224    {
225       Counter temp;
226       if (increasing) {
227          temp = new Wrapper(counter) {
228                public int decrement() {
229                   throw new UnsupportedOperationException JavaDoc();
230                }
231
232                public void reset() {
233                   throw new UnsupportedOperationException JavaDoc();
234                }
235             };
236       }
237       else {
238          temp = new Wrapper(counter) {
239                public int increment() {
240                   throw new UnsupportedOperationException JavaDoc();
241                }
242             };
243       }
244       
245       return temp;
246    }
247 }
248
Popular Tags