KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

27    public MuShort() {}
28
29    /**
30     * Construct a new mutable short integer.
31     *
32     * @param s Short value.
33     */

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

43    public MuShort(Object JavaDoc obj) {
44       setValue(obj);
45    }
46
47    /**
48     * Set the value.
49     *
50     * @param s <code>short</code> value.
51     * @return The previous value.
52     */

53    public short set(short s) {
54       short old = value;
55       value = s;
56       return old;
57    }
58
59    /**
60     * Get the current value.
61     *
62     * @return The current value.
63     */

64    public short get() {
65       return value;
66    }
67
68    /**
69     * Set the value to value only if the current value is equal to
70     * the assumed value.
71     *
72     * @param assumed The assumed value.
73     * @param b The new value.
74     * @return True if value was changed.
75     */

76    public boolean commit(short assumed, short b) {
77       boolean success = (assumed == value);
78       if (success) value = b;
79       return success;
80    }
81
82    /**
83     * Swap values with another mutable short.
84     *
85     * @param b Mutable short to swap values with.
86     * @return The new value.
87     */

88    public short swap(MuShort b) {
89       if (b == this) return value;
90
91       short temp = value;
92       value = b.value;
93       b.value = temp;
94
95       return value;
96    }
97
98    /**
99     * Increment the value of this mutable short.
100     *
101     * @return Short value.
102     */

103    public short increment() {
104       return ++value;
105    }
106
107    /**
108     * Decrement the value of this mutable short.
109     *
110     * @return Short value.
111     */

112    public short decrement() {
113       return --value;
114    }
115
116    /**
117     * Add the specified amount.
118     *
119     * @param amount Amount to add.
120     * @return The new value.
121     */

122    public short add(short amount) {
123       return value += amount;
124    }
125
126    /**
127     * Subtract the specified amount.
128     *
129     * @param amount Amount to subtract.
130     * @return The new value.
131     */

132    public short subtract(short amount) {
133       return value -= amount;
134    }
135
136    /**
137     * Multiply by the specified factor.
138     *
139     * @param factor Factor to multiply by.
140     * @return The new value.
141     */

142    public short multiply(short factor) {
143       return value *= factor;
144    }
145
146    /**
147     * Divide by the specified factor.
148     *
149     * @param factor Factor to divide by.
150     * @return The new value.
151     */

152    public short divide(short factor) {
153       return value /= factor;
154    }
155
156    /**
157     * Set the value to the negative of its current value.
158     *
159     * @return The new value.
160     */

161    public short negate() {
162       value = ((short)-value);
163       return value;
164    }
165
166    /**
167     * Set the value to its complement.
168     *
169     * @return The new value.
170     */

171    public short complement() {
172       value = (short)~value;
173       return value;
174    }
175
176    /**
177     * <i>AND</i>s the current value with the specified value.
178     *
179     * @param b Value to <i>and</i> with.
180     * @return The new value.
181     */

182    public short and(short b) {
183       value = (short)(value & b);
184       return value;
185    }
186
187    /**
188     * <i>OR</i>s the current value with the specified value.
189     *
190     * @param b Value to <i>or</i> with.
191     * @return The new value.
192     */

193    public short or(short b) {
194       value = (short)(value | b);
195       return value;
196    }
197
198    /**
199     * <i>XOR</i>s the current value with the specified value.
200     *
201     * @param b Value to <i>xor</i> with.
202     * @return The new value.
203     */

204    public short xor(short b) {
205       value = (short)(value ^ b);
206       return value;
207    }
208
209    /**
210     * Shift the current value to the <i>right</i>.
211     *
212     * @param bits The number of bits to shift.
213     * @return The new value.
214     */

215    public short shiftRight(int bits) {
216       value >>= bits;
217       return value;
218    }
219
220    /**
221     * Shift the current value to the <i>right</i> with a zero extension.
222     *
223     * @param bits The number of bits to shift.
224     * @return The new value.
225     */

226    public short shiftRightZero(int bits) {
227       value >>>= bits;
228       return value;
229    }
230
231    /**
232     * Shift the current value to the <i>left</i>.
233     *
234     * @param bits The number of bits to shift.
235     * @return The new value.
236     */

237    public short shiftLeft(int bits) {
238       value <<= bits;
239       return value;
240    }
241
242    /**
243     * Compares this object with the specified short for order.
244     *
245     * @param other Value to compare with.
246     * @return A negative integer, zero, or a positive integer as
247     * this object is less than, equal to, or greater than
248     * the specified object.
249     */

250    public int compareTo(short other) {
251       return (value < other) ? -1 : (value == other) ? 0 : 1;
252    }
253
254    /**
255     * Compares this object with the specified object for order.
256     *
257     * @param other Value to compare with.
258     * @return A negative integer, zero, or a positive integer as
259     * this object is less than, equal to, or greater than
260     * the specified object.
261     *
262     * @throws ClassCastException Object is not a MuShort.
263     */

264    public int compareTo(Object JavaDoc obj) {
265       return compareTo((MuShort)obj);
266    }
267
268    /**
269     * Convert this mutable short integer to a string.
270     *
271     * @return String value.
272     */

273    public String JavaDoc toString() {
274       return String.valueOf(value);
275    }
276
277    /**
278     * Get the hash code for this mutable short integer.
279     *
280     * @return Hash code.
281     */

282    public int hashCode() {
283       return (int)value;
284    }
285
286    /**
287     * Test the equality of this mutable short integer and another object.
288     *
289     * @param obj Object to test equality with.
290     * @return True if object is equal.
291     */

292    public boolean equals(Object JavaDoc obj) {
293       if (obj == this) return true;
294
295       if (obj != null && obj.getClass() == getClass()) {
296          return value == ((MuShort)obj).shortValue();
297       }
298
299       return false;
300    }
301
302    /**
303     * Return a cloned copy of this mutable short.
304     *
305     * @return Cloaned mutable short.
306     */

307    public Object JavaDoc clone() {
308       try {
309          return super.clone();
310       }
311       catch (CloneNotSupportedException JavaDoc e) {
312          throw new InternalError JavaDoc();
313       }
314    }
315
316
317    /////////////////////////////////////////////////////////////////////////
318
// Number Support //
319
/////////////////////////////////////////////////////////////////////////
320

321    /**
322     * Return the <code>byte</code> value of this object.
323     *
324     * @return <code>byte</code> value.
325     */

326    public byte byteValue() {
327       return (byte)value;
328    }
329
330    /**
331     * Return the <code>short</code> value of this object.
332     *
333     * @return <code>short</code> value.
334     */

335    public short shortValue() {
336       return (short)value;
337    }
338
339    /**
340     * Return the <code>int</code> value of this object.
341     *
342     * @return <code>int</code> value.
343     */

344    public int intValue() {
345       return (int)value;
346    }
347
348    /**
349     * Return the <code>long</code> value of this object.
350     *
351     * @return <code>long</code> value.
352     */

353    public long longValue() {
354       return (long)value;
355    }
356
357    /**
358     * Return the <code>float</code> value of this object.
359     *
360     * @return <code>float</code> value.
361     */

362    public float floatValue() {
363       return (float)value;
364    }
365
366    /**
367     * Return the <code>double</code> value of this object.
368     *
369     * @return <code>double</code> value.
370     */

371    public double doubleValue() {
372       return (double)value;
373    }
374
375
376    /////////////////////////////////////////////////////////////////////////
377
// Mutable Support //
378
/////////////////////////////////////////////////////////////////////////
379

380    /**
381     * Set the value of this mutable short integer.
382     *
383     * @param obj Object to convert to a <code>short</code> value.
384     *
385     * @throws NotCoercibleException Can not convert to <code>short</code>.
386     */

387    public void setValue(Object JavaDoc obj) {
388       if (obj instanceof Number JavaDoc) {
389          value = ((Number JavaDoc)obj).shortValue();
390       }
391       else {
392          throw new NotCoercibleException("can not convert to 'short': " + obj);
393       }
394    }
395
396    /**
397     * Return the value of this mutable short integer.
398     *
399     * @return <code>java.lang.Short</code> value.
400     */

401    public Object JavaDoc getValue() {
402       return new Short JavaDoc(value);
403    }
404 }
405
Popular Tags