KickJava   Java API By Example, From Geeks To Geeks.

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


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 integer class.
14  *
15  * @version <tt>$Revision: 1.1.28.1 $</tt>
16  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
17  */

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

27    public MuInteger() {}
28
29    /**
30     * Construct a new mutable integer.
31     *
32     * @param i Integer value.
33     */

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

43    public MuInteger(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 i The new value.
53     * @return True if value was changed.
54     */

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

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

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

91    public int 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 int add(int 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 int subtract(int 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 int multiply(int 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 int divide(int 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 int negate() {
141       value = ((int)-value);
142       return value;
143    }
144
145    /**
146     * Set the value to its complement.
147     *
148     * @return The new value.
149     */

150    public int complement() {
151       value = (int)~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 int and(int b) {
162       value = (int)(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 int or(int b) {
173       value = (int)(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 int xor(int b) {
184       value = (int)(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 int 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 int 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 int shiftLeft(int bits) {
217       value <<= bits;
218       return value;
219    }
220
221    /**
222     * Compares this object with the specified int 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(int 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 MuInteger.
242     */

243    public int compareTo(Object JavaDoc obj) {
244       return compareTo(((MuInteger)obj).value);
245    }
246
247    /**
248     * Convert this mutable 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 for this mutable integer.
258     *
259     * @return Hash code.
260     */

261    public int hashCode() {
262       return value;
263    }
264
265    /**
266     * Test the equality of this mutable integer and another object.
267     *
268     * @param obj Qbject 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 == ((MuInteger)obj).intValue();
276       }
277
278       return false;
279    }
280
281    /**
282     * Return a cloned copy of this mutable integer.
283     *
284     * @return Cloned mutable integer.
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    /////////////////////////////////////////////////////////////////////////
355
// Mutable Support //
356
/////////////////////////////////////////////////////////////////////////
357

358    /**
359     * Set the value of this mutable integer.
360     *
361     * @param value Object to convert to an integer value.
362     *
363     * @throws NotCoercibleException Can not convert to <code>int</code>.
364     */

365    public void setValue(Object JavaDoc obj) {
366       if (obj instanceof Number JavaDoc) {
367          value = ((Number JavaDoc)obj).intValue();
368       }
369       else {
370          throw new NotCoercibleException("can not convert to 'int': " + obj);
371       }
372    }
373
374    /**
375     * Get the value of this mutable integer.
376     *
377     * @return <code>java.lang.Integer</code> value.
378     */

379    public Object JavaDoc getValue() {
380       return new Integer JavaDoc(value);
381    }
382 }
383
Popular Tags