KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A long 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 LongCounter
33    implements Serializable JavaDoc, Cloneable JavaDoc
34 {
35    /** The current count */
36    private long count;
37
38    /**
39     * Construct a LongCounter with a starting value.
40     *
41     * @param count Starting value for counter.
42     */

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

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

57    public long 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 long 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 long 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 ((LongCounter)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 LongCounter
135    {
136       /** The wrapped counter */
137       protected final LongCounter counter;
138
139       public Wrapper(final LongCounter counter) {
140          this.counter = counter;
141       }
142
143       public long increment() {
144          return counter.increment();
145       }
146
147       public long decrement() {
148          return counter.decrement();
149       }
150
151       public long 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 LongCounter to synchronize.
176     * @return Synchronized counter.
177     */

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

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