KickJava   Java API By Example, From Geeks To Geeks.

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


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.util;
11
12 /**
13  * A mutable long integer class.
14  *
15  * @version <tt>$Revision: 1.4 $</tt>
16  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
17  */

18 public class MuLong
19    extends MuNumber
20 {
21    /** Long value */
22    private long value;
23
24    /**
25     * Construct a new mutable long integer.
26     */

27    public MuLong() {}
28
29    /**
30     * Construct a new mutable long integer.
31     *
32     * @param l <code>long</code> value.
33     */

34    public MuLong(long l) {
35       value = l;
36    }
37
38    /**
39     * Construct a new mutable long integer.
40     *
41     * @param obj Object to convert to a <code>long</code> value.
42     */

43    public MuLong(Object JavaDoc obj) {
44       setValue(obj);
45    }
46
47    /**
48     * Set the value to value only if the current value is equal to
49     * the assumed value.
50     *
51     * @param assumed The assumed value.
52     * @param b The new value.
53     * @return True if value was changed.
54     */

55    public boolean commit(long assumed, long b) {
56       boolean success = (assumed == value);
57       if (success) value = b;
58       return success;
59    }
60
61    /**
62     * Swap values with another mutable long.
63     *
64     * @param b Mutable long to swap values with.
65     * @return The new value.
66     */

67    public long swap(MuLong b) {
68       if (b == this) return value;
69
70       long temp = value;
71       value = b.value;
72       b.value = temp;
73
74       return value;
75    }
76
77    /**
78     * Increment the value of this mutable long.
79     *
80     * @return Long value.
81     */

82    public long increment() {
83       return ++value;
84    }
85
86    /**
87     * Decrement the value of this mutable long.
88     *
89     * @return Long value.
90     */

91    public long decrement() {
92       return --value;
93    }
94
95    /**
96     * Add the specified amount.
97     *
98     * @param amount Amount to add.
99     * @return The new value.
100     */

101    public long add(long amount) {
102       return value += amount;
103    }
104
105    /**
106     * Subtract the specified amount.
107     *
108     * @param amount Amount to subtract.
109     * @return The new value.
110     */

111    public long subtract(long amount) {
112       return value -= amount;
113    }
114
115    /**
116     * Multiply by the specified factor.
117     *
118     * @param factor Factor to multiply by.
119     * @return The new value.
120     */

121    public long multiply(long factor) {
122       return value *= factor;
123    }
124
125    /**
126     * Divide by the specified factor.
127     *
128     * @param factor Factor to divide by.
129     * @return The new value.
130     */

131    public long divide(long factor) {
132       return value /= factor;
133    }
134
135    /**
136     * Set the value to the negative of its current value.
137     *
138     * @return The new value.
139     */

140    public long negate() {
141       value = ((long)-value);
142       return value;
143    }
144
145    /**
146     * Set the value to its complement.
147     *
148     * @return The new value.
149     */

150    public long complement() {
151       value = (long)~value;
152       return value;
153    }
154
155    /**
156     * <i>AND</i>s the current value with the specified value.
157     *
158     * @param b Value to <i>and</i> with.
159     * @return The new value.
160     */

161    public long and(long b) {
162       value = (long)(value & b);
163       return value;
164    }
165
166    /**
167     * <i>OR</i>s the current value with the specified value.
168     *
169     * @param b Value to <i>or</i> with.
170     * @return The new value.
171     */

172    public long or(long b) {
173       value = (long)(value | b);
174       return value;
175    }
176
177    /**
178     * <i>XOR</i>s the current value with the specified value.
179     *
180     * @param b Value to <i>xor</i> with.
181     * @return The new value.
182     */

183    public long xor(long b) {
184       value = (long)(value ^ b);
185       return value;
186    }
187
188    /**
189     * Shift the current value to the <i>right</i>.
190     *
191     * @param bits The number of bits to shift.
192     * @return The new value.
193     */

194    public long shiftRight(int bits) {
195       value >>= bits;
196       return value;
197    }
198
199    /**
200     * Shift the current value to the <i>right</i> with a zero extension.
201     *
202     * @param bits The number of bits to shift.
203     * @return The new value.
204     */

205    public long shiftRightZero(int bits) {
206       value >>>= bits;
207       return value;
208    }
209
210    /**
211     * Shift the current value to the <i>left</i>.
212     *
213     * @param bits The number of bits to shift.
214     * @return The new value.
215     */

216    public long shiftLeft(int bits) {
217       value <<= bits;
218       return value;
219    }
220
221    /**
222     * Compares this object with the specified long for order.
223     *
224     * @param other Value to compare with.
225     * @return A negative integer, zero, or a positive integer as
226     * this object is less than, equal to, or greater than
227     * the specified object.
228     */

229    public int compareTo(long other) {
230       return (value < other) ? -1 : (value == other) ? 0 : 1;
231    }
232
233    /**
234     * Compares this object with the specified object for order.
235     *
236     * @param other Value to compare with.
237     * @return A negative integer, zero, or a positive integer as
238     * this object is less than, equal to, or greater than
239     * the specified object.
240     *
241     * @throws ClassCastException Object is not a MuLong.
242     */

243    public int compareTo(Object JavaDoc obj) {
244       return compareTo((MuLong)obj);
245    }
246
247    /**
248     * Convert this mutable long integer to a string.
249     *
250     * @return String value.
251     */

252    public String JavaDoc toString() {
253       return String.valueOf(value);
254    }
255
256    /**
257     * Get the hash code of this mutable long integer.
258     *
259     * @return Hash code.
260     */

261    public int hashCode() {
262       return HashCode.generate(value);
263    }
264
265    /**
266     * Test the equality of this mutable long integer and another object.
267     *
268     * @param obj Object to test equality with.
269     * @return True if object is equal.
270     */

271    public boolean equals(Object JavaDoc obj) {
272       if (obj == this) return true;
273
274       if (obj != null && obj.getClass() == getClass()) {
275          return value == ((MuLong)obj).longValue();
276       }
277
278       return false;
279    }
280
281    /**
282     * Return a cloned copy of this mutable long.
283     *
284     * @return Cloaned mutable long.
285     */

286    public Object JavaDoc clone() {
287       try {
288          return super.clone();
289       }
290       catch (CloneNotSupportedException JavaDoc e) {
291          throw new InternalError JavaDoc();
292       }
293    }
294
295    /////////////////////////////////////////////////////////////////////////
296
// Number Support //
297
/////////////////////////////////////////////////////////////////////////
298

299    /**
300     * Return the <code>byte</code> value of this object.
301     *
302     * @return <code>byte</code> value.
303     */

304    public byte byteValue() {
305       return (byte)value;
306    }
307
308    /**
309     * Return the <code>short</code> value of this object.
310     *
311     * @return <code>short</code> value.
312     */

313    public short shortValue() {
314       return (short)value;
315    }
316
317    /**
318     * Return the <code>int</code> value of this object.
319     *
320     * @return <code>int</code> value.
321     */

322    public int intValue() {
323       return (int)value;
324    }
325
326    /**
327     * Return the <code>long</code> value of this object.
328     *
329     * @return <code>long</code> value.
330     */

331    public long longValue() {
332       return (long)value;
333    }
334
335    /**
336     * Return the <code>float</code> value of this object.
337     *
338     * @return <code>float</code> value.
339     */

340    public float floatValue() {
341       return (float)value;
342    }
343
344    /**
345     * Return the <code>double</code> value of this object.
346     *
347     * @return <code>double</code> value.
348     */

349    public double doubleValue() {
350       return (double)value;
351    }
352
353    /**
354     * Set the value of this mutable long integer.
355     *
356     * @param value The new value.
357     */

358    public void set(long value)
359    {
360       this.value = value;
361    }
362
363
364    /////////////////////////////////////////////////////////////////////////
365
// Mutable Support //
366
/////////////////////////////////////////////////////////////////////////
367

368    /**
369     * Set the value of this mutable long integer.
370     *
371     * @param obj Object to convert to a <code>long</code> value.
372     *
373     * @throws NotCoercibleException Can not convert to <code>long</code>.
374     */

375    public void setValue(Object JavaDoc obj) {
376       if (obj instanceof Number JavaDoc) {
377          value = ((Number JavaDoc)obj).longValue();
378       }
379       else {
380          throw new NotCoercibleException("can not convert to 'long': " + obj);
381       }
382    }
383
384    /**
385     * Return the value of this mutable long integer.
386     *
387     * @return <code>java.lang.Long<code> value.
388     */

389    public Object JavaDoc getValue() {
390       return new Long JavaDoc(value);
391    }
392 }
393
Popular Tags