KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 public class MuDouble
19    extends MuNumber
20 {
21    /** Double value */
22    private double value = 0;
23
24    /**
25     * Construct a new mutable double.
26     */

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

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

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

53    public double set(double f) {
54       double 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 double 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(double assumed, double 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 double.
84     *
85     * @param b Mutable double to swap values with.
86     * @return The new value.
87     */

88    public double swap(MuDouble b) {
89       if (b == this) return value;
90
91       double 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 double add(double 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 double subtract(double 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 double multiply(double 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 double divide(double 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 double negate() {
144       value = ((double)-value);
145       return value;
146    }
147
148    /**
149     * Compares this object with the specified double 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(double 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 MuDouble.
169     */

170    public int compareTo(Object JavaDoc obj) {
171       return compareTo((MuDouble)obj);
172    }
173
174    /**
175     * Convert this mutable double 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 double.
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
196     * @return 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, ((MuDouble)obj).doubleValue());
203       }
204
205       return false;
206    }
207
208    /**
209     * Return a cloned copy of this mutable double.
210     *
211     * @return Cloaned mutable double.
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
// Number Support //
224
/////////////////////////////////////////////////////////////////////////
225

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

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

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

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

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

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

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

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

292    public void setValue(Object JavaDoc obj) {
293       if (obj instanceof Number JavaDoc) {
294          value = ((Number JavaDoc)obj).doubleValue();
295       }
296       else {
297          throw new NotCoercibleException("can not convert to 'double': " + obj);
298       }
299    }
300
301    /**
302     * Get the value of this mutable double.
303     *
304     * @return <code>java.lang.Double</code> value
305     */

306    public Object JavaDoc getValue() {
307       return new Double JavaDoc(value);
308    }
309 }
310
Popular Tags