KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > mutable > MutableDouble


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.lang.mutable;
18
19 import org.apache.commons.lang.math.NumberUtils;
20
21 /**
22  * A mutable <code>double</code> wrapper.
23  *
24  * @see Double
25  * @since 2.1
26  * @version $Id: MutableDouble.java 161243 2005-04-14 04:30:28Z ggregory $
27  */

28 public class MutableDouble extends Number JavaDoc implements Comparable JavaDoc, Mutable {
29
30     /** Serialization lock. */
31     private static final long serialVersionUID = 1587163916L;
32
33     /** The mutable value. */
34     private double value;
35
36     /**
37      * Constructs a new MutableDouble with the default value of zero.
38      */

39     public MutableDouble() {
40         super();
41     }
42
43     /**
44      * Constructs a new MutableDouble with the specified value.
45      *
46      * @param value
47      * a value.
48      */

49     public MutableDouble(double value) {
50         super();
51         this.value = value;
52     }
53
54     /**
55      * Constructs a new MutableDouble with the specified value.
56      *
57      * @param value
58      * a value.
59      * @throws NullPointerException
60      * if the object is null
61      */

62     public MutableDouble(Number JavaDoc value) {
63         super();
64         this.value = value.doubleValue();
65     }
66
67     //-----------------------------------------------------------------------
68
/**
69      * Gets the value as a Double instance.
70      *
71      * @return the value as a Double
72      */

73     public Object JavaDoc getValue() {
74         return new Double JavaDoc(this.value);
75     }
76
77     /**
78      * Sets the value.
79      *
80      * @param value
81      * the value to set
82      */

83     public void setValue(double value) {
84         this.value = value;
85     }
86
87     /**
88      * Sets the value from any Number instance.
89      *
90      * @param value
91      * the value to set
92      * @throws NullPointerException
93      * if the object is null
94      * @throws ClassCastException
95      * if the type is not a {@link Number}
96      */

97     public void setValue(Object JavaDoc value) {
98         setValue(((Number JavaDoc) value).doubleValue());
99     }
100
101     //-----------------------------------------------------------------------
102
// shortValue and bytValue rely on Number implementation
103
/**
104      * Returns the value of this MutableDouble as a int.
105      *
106      * @return the numeric value represented by this object after conversion to type int.
107      */

108     public int intValue() {
109         return (int) value;
110     }
111
112     /**
113      * Returns the value of this MutableDouble as a long.
114      *
115      * @return the numeric value represented by this object after conversion to type long.
116      */

117     public long longValue() {
118         return (long) value;
119     }
120
121     /**
122      * Returns the value of this MutableDouble as a float.
123      *
124      * @return the numeric value represented by this object after conversion to type float.
125      */

126     public float floatValue() {
127         return (float) value;
128     }
129
130     /**
131      * Returns the value of this MutableDouble as a double.
132      *
133      * @return the numeric value represented by this object after conversion to type double.
134      */

135     public double doubleValue() {
136         return value;
137     }
138
139     /**
140      * Checks whether the double value is the special NaN value.
141      *
142      * @return true if NaN
143      */

144     public boolean isNaN() {
145         return Double.isNaN(value);
146     }
147
148     /**
149      * Checks whether the double value is infinite.
150      *
151      * @return true if infinite
152      */

153     public boolean isInfinite() {
154         return Double.isInfinite(value);
155     }
156
157     //-----------------------------------------------------------------------
158
/**
159      * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
160      * is not <code>null</code> and is a <code>Double</code> object that represents a double that has the identical
161      * bit pattern to the bit pattern of the double represented by this object. For this purpose, two
162      * <code>double</code> values are considered to be the same if and only if the method
163      * {@link Double#doubleToLongBits(double)}returns the same long value when applied to each.
164      * <p>
165      * Note that in most cases, for two instances of class <code>Double</code>,<code>d1</code> and <code>d2</code>,
166      * the value of <code>d1.equals(d2)</code> is <code>true</code> if and only if <blockquote>
167      *
168      * <pre>
169      * d1.doubleValue()&nbsp;== d2.doubleValue()
170      * </pre>
171      *
172      * </blockquote>
173      * <p>
174      * also has the value <code>true</code>. However, there are two exceptions:
175      * <ul>
176      * <li>If <code>d1</code> and <code>d2</code> both represent <code>Double.NaN</code>, then the
177      * <code>equals</code> method returns <code>true</code>, even though <code>Double.NaN==Double.NaN</code> has
178      * the value <code>false</code>.
179      * <li>If <code>d1</code> represents <code>+0.0</code> while <code>d2</code> represents <code>-0.0</code>,
180      * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
181      * <code>+0.0==-0.0</code> has the value <code>true</code>. This allows hashtables to operate properly.
182      * </ul>
183      *
184      * @param obj
185      * the object to compare with.
186      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
187      */

188     public boolean equals(Object JavaDoc obj) {
189         return (obj instanceof MutableDouble)
190             && (Double.doubleToLongBits(((MutableDouble) obj).value) == Double.doubleToLongBits(value));
191     }
192
193     /**
194      * Returns a suitable hashcode for this mutable.
195      *
196      * @return a suitable hashcode
197      */

198     public int hashCode() {
199         long bits = Double.doubleToLongBits(value);
200         return (int) (bits ^ (bits >>> 32));
201     }
202
203     /**
204      * Compares this mutable to another in ascending order.
205      *
206      * @param obj
207      * the mutable to compare to
208      * @return negative if this is less, zero if equal, positive if greater
209      * @throws ClassCastException if the argument is not a MutableDouble
210      */

211     public int compareTo(Object JavaDoc obj) {
212         MutableDouble other = (MutableDouble) obj;
213         double anotherVal = other.value;
214         return NumberUtils.compare(value, anotherVal);
215     }
216
217     /**
218      * Returns the String value of this mutable.
219      *
220      * @return the mutable value as a string
221      */

222     public String JavaDoc toString() {
223         return String.valueOf(value);
224     }
225
226 }
227
Popular Tags