KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 public class MuFloat
19    extends MuNumber
20 {
21    /** Float value */
22    private float value; // = 0;
23

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

27    public MuFloat() {}
28
29    /**
30     * Construct a new mutable float.
31     *
32     * @param f <code>float</code> value.
33     */

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

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

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

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

88    public float swap(MuFloat b) {
89       if (b == this) return value;
90
91       float temp = value;
92       value = b.value;
93       b.value = temp;
94
95       return value;
96    }
97
98    /**
99     * Add the specified amount.
100     *
101     * @param amount Amount to add.
102     * @return The new value.
103     */

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

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

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

134    public float divide(float factor) {
135       return value /= factor;
136    }
137
138    /**
139     * Set the value to the negative of its current value.
140     *
141     * @return The new value.
142     */

143    public float negate() {
144       value = ((float)-value);
145       return value;
146    }
147
148    /**
149     * Compares this object with the specified float for order.
150     *
151     * @param other Value to compare with.
152     * @return A negative integer, zero, or a positive integer as
153     * this object is less than, equal to, or greater than
154     * the specified object.
155     */

156    public int compareTo(float other) {
157       return (value < other) ? -1 : Primitives.equals(value, other) ? 0 : 1;
158    }
159
160    /**
161     * Compares this object with the specified object for order.
162     *
163     * @param other Value to compare with.
164     * @return A negative integer, zero, or a positive integer as
165     * this object is less than, equal to, or greater than
166     * the specified object.
167     *
168     * @throws ClassCastException Object is not a MuFloat.
169     */

170    public int compareTo(Object JavaDoc obj) {
171       return compareTo((MuFloat)obj);
172    }
173
174    /**
175     * Convert this mutable float to a string.
176     *
177     * @return String value.
178     */

179    public String JavaDoc toString() {
180       return String.valueOf(value);
181    }
182
183    /**
184     * Get the hash code for this mutable float.
185     *
186     * @return Hash code.
187     */

188    public int hashCode() {
189       return HashCode.generate(value);
190    }
191
192    /**
193     * Test the equality of this mutable double with another object.
194     *
195     * @param obj Object to test equality with.
196     * @return True if object is equal.
197     */

198    public boolean equals(Object JavaDoc obj) {
199       if (obj == this) return true;
200
201       if (obj != null && obj.getClass() == getClass()) {
202          return Primitives.equals(value, ((MuFloat)obj).floatValue());
203       }
204
205       return false;
206    }
207
208    /**
209     * Return a cloned copy of this mutable float.
210     *
211     * @return Cloaned mutable float.
212     */

213    public Object JavaDoc clone() {
214       try {
215          return super.clone();
216       }
217       catch (CloneNotSupportedException JavaDoc e) {
218          throw new InternalError JavaDoc();
219       }
220    }
221
222
223    /////////////////////////////////////////////////////////////////////////
224
// Number Support //
225
/////////////////////////////////////////////////////////////////////////
226

227    /**
228     * Return the <code>byte</code> value of this object.
229     *
230     * @return <code>byte</code> value.
231     */

232    public byte byteValue() {
233       return (byte)value;
234    }
235
236    /**
237     * Return the <code>short</code> value of this object.
238     *
239     * @return <code>short</code> value.
240     */

241    public short shortValue() {
242       return (short)value;
243    }
244
245    /**
246     * Return the <code>int</code> value of this object.
247     *
248     * @return <code>int</code> value.
249     */

250    public int intValue() {
251       return (int)value;
252    }
253
254    /**
255     * Return the <code>long</code> value of this object.
256     *
257     * @return <code>long</code> value.
258     */

259    public long longValue() {
260       return (long)value;
261    }
262
263    /**
264     * Return the <code>float</code> value of this object.
265     *
266     * @return <code>float</code> value.
267     */

268    public float floatValue() {
269       return (float)value;
270    }
271
272    /**
273     * Return the <code>double</code> value of this object.
274     *
275     * @return <code>double</code> value.
276     */

277    public double doubleValue() {
278       return (double)value;
279    }
280
281
282    /////////////////////////////////////////////////////////////////////////
283
// Mutable Support //
284
/////////////////////////////////////////////////////////////////////////
285

286    /**
287     * Set the value of this mutable float.
288     *
289     * @param obj Object to convert to a <code>float</code> value.
290     *
291     * @throws NotCoercibleException Can not convert to <code>float</code>.
292     */

293    public void setValue(Object JavaDoc obj) {
294       if (obj instanceof Number JavaDoc) {
295          value = ((Number JavaDoc)obj).floatValue();
296       }
297       else if (obj instanceof String JavaDoc) {
298          try {
299             value = Float.parseFloat(String.valueOf(obj));
300          }
301          catch (Exception JavaDoc e) {
302             throw new NotCoercibleException("can not convert to 'float': " + obj);
303          }
304       }
305       else {
306          throw new NotCoercibleException("can not convert to 'float': " + obj);
307       }
308    }
309
310    /**
311     * Get the float value of this mutable float.
312     *
313     * @return <code>java.lang.Float</code> value.
314     */

315    public Object JavaDoc getValue() {
316       return new Float JavaDoc(value);
317    }
318 }
319
Popular Tags