KickJava   Java API By Example, From Geeks To Geeks.

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


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>float</code> wrapper.
23  *
24  * @see Float
25  * @since 2.1
26  * @version $Id: MutableFloat.java 161243 2005-04-14 04:30:28Z ggregory $
27  */

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

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

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

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

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

83     public void setValue(float 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).floatValue());
99     }
100
101     //-----------------------------------------------------------------------
102
// shortValue and bytValue rely on Number implementation
103
/**
104      * Returns the value of this MutableFloat 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 MutableFloat 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 MutableFloat 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 value;
128     }
129
130     /**
131      * Returns the value of this MutableFloat 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 float value is the special NaN value.
141      *
142      * @return true if NaN
143      */

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

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

189     public boolean equals(Object JavaDoc obj) {
190         return (obj instanceof MutableFloat)
191             && (Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value));
192     }
193
194     //-----------------------------------------------------------------------
195
/**
196      * Returns a suitable hashcode for this mutable.
197      *
198      * @return a suitable hashcode
199      */

200     public int hashCode() {
201         return Float.floatToIntBits(value);
202     }
203
204     /**
205      * Compares this mutable to another in ascending order.
206      *
207      * @param obj
208      * the mutable to compare to
209      * @return negative if this is less, zero if equal, positive if greater
210      */

211     public int compareTo(Object JavaDoc obj) {
212         MutableFloat other = (MutableFloat) obj;
213         float 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