KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 public class MuByte
19    extends MuNumber
20 {
21    /** Byte value */
22    private byte value; // = 0;
23

24    /**
25     * Construct a new mutable byte.
26     */

27    public MuByte() {}
28
29    /**
30     * Construct a new mutable byte.
31     *
32     * @param b Byte value.
33     */

34    public MuByte(byte b) {
35       value = b;
36    }
37
38    /**
39     * Construct a new mutable byte.
40     *
41     * @param obj Object to convert to a byte value.
42     */

43    public MuByte(Object JavaDoc obj) {
44       setValue(obj);
45    }
46
47    /**
48     * Set the value.
49     *
50     * @param b Byte value.
51     * @return The previous value.
52     */

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

64    public byte 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(byte assumed, byte b) {
77       boolean success = (assumed == value);
78       if (success) value = b;
79       return success;
80    }
81
82    /**
83     * Swap values with another mutable byte.
84     *
85     * @param b Mutable byte to swap values with.
86     * @return The new value.
87     */

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

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

112    public byte 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 byte add(byte 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 byte subtract(byte 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 byte multiply(byte 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 byte divide(byte 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 byte negate() {
162       value = ((byte)-value);
163       return value;
164    }
165
166    /**
167     * Set the value to its complement.
168     *
169     * @return The new value.
170     */

171    public byte complement() {
172       value = (byte)~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 byte and(byte b) {
183       value = (byte)(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 byte or(byte b) {
194       value = (byte)(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 byte xor(byte b) {
205       value = (byte)(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 byte 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 byte 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 byte shiftLeft(int bits) {
238       value <<= bits;
239       return value;
240    }
241
242    /**
243     * Compares this object with the specified byte 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(byte 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 MuByte.
263     */

264    public int compareTo(Object JavaDoc obj) throws ClassCastException JavaDoc {
265       return compareTo((MuByte)obj);
266    }
267
268    /**
269     * Convert this mutable byte 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 byte.
279     *
280     * @return Hash code.
281     */

282    public int hashCode() {
283       return (int)value;
284    }
285
286    /**
287     * Test the equality of this mutable byte 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 == ((MuByte)obj).byteValue();
297       }
298
299       return false;
300    }
301
302    /**
303     * Return a cloned copy of this mutable byte.
304     *
305     * @return Cloned mutable byte.
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 byte.
382     *
383     * @param obj Object to convert to a <code>byte</code> value.
384     */

385    public void setValue(Object JavaDoc obj) {
386       if (obj instanceof Number JavaDoc) {
387          value = ((Number JavaDoc)obj).byteValue();
388       }
389       else {
390          value = (byte)obj.hashCode();
391       }
392    }
393
394    /**
395     * Get the byte value of this mutable byte.
396     *
397     * @return <code>java.lang.Byte</code> value.
398     */

399    public Object JavaDoc getValue() {
400       return new Byte JavaDoc(value);
401    }
402 }
403
Popular Tags