001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.lang.mutable;
018
019 import org.apache.commons.lang.math.NumberUtils;
020
021 /**
022 * A mutable <code>float</code> wrapper.
023 *
024 * @see Float
025 * @since 2.1
026 * @version $Id: MutableFloat.java 618693 2008-02-05 16:33:29Z sebb $
027 */
028 public class MutableFloat extends Number implements Comparable, Mutable {
029
030 /**
031 * Required for serialization support.
032 *
033 * @see java.io.Serializable
034 */
035 private static final long serialVersionUID = 5787169186L;
036
037 /** The mutable value. */
038 private float value;
039
040 /**
041 * Constructs a new MutableFloat with the default value of zero.
042 */
043 public MutableFloat() {
044 super();
045 }
046
047 /**
048 * Constructs a new MutableFloat with the specified value.
049 *
050 * @param value
051 * a value.
052 */
053 public MutableFloat(float value) {
054 super();
055 this.value = value;
056 }
057
058 /**
059 * Constructs a new MutableFloat with the specified value.
060 *
061 * @param value
062 * a value.
063 * @throws NullPointerException
064 * if the object is null
065 */
066 public MutableFloat(Number value) {
067 super();
068 this.value = value.floatValue();
069 }
070
071 //-----------------------------------------------------------------------
072 /**
073 * Gets the value as a Float instance.
074 *
075 * @return the value as a Float
076 */
077 public Object getValue() {
078 return new Float(this.value);
079 }
080
081 /**
082 * Sets the value.
083 *
084 * @param value
085 * the value to set
086 */
087 public void setValue(float value) {
088 this.value = value;
089 }
090
091 /**
092 * Sets the value from any Number instance.
093 *
094 * @param value
095 * the value to set
096 * @throws NullPointerException
097 * if the object is null
098 * @throws ClassCastException
099 * if the type is not a {@link Number}
100 */
101 public void setValue(Object value) {
102 setValue(((Number) value).floatValue());
103 }
104
105 //-----------------------------------------------------------------------
106 /**
107 * Increments the value.
108 *
109 * @since Commons Lang 2.2
110 */
111 public void increment() {
112 value++;
113 }
114
115 /**
116 * Decrements the value.
117 *
118 * @since Commons Lang 2.2
119 */
120 public void decrement() {
121 value--;
122 }
123
124 //-----------------------------------------------------------------------
125 /**
126 * Adds a value.
127 *
128 * @param operand
129 * the value to add
130 *
131 * @since Commons Lang 2.2
132 */
133 public void add(float operand) {
134 this.value += operand;
135 }
136
137 /**
138 * Adds a value.
139 *
140 * @param operand
141 * the value to add
142 * @throws NullPointerException
143 * if the object is null
144 *
145 * @since Commons Lang 2.2
146 */
147 public void add(Number operand) {
148 this.value += operand.floatValue();
149 }
150
151 /**
152 * Subtracts a value.
153 *
154 * @param operand
155 * the value to add
156 *
157 * @since Commons Lang 2.2
158 */
159 public void subtract(float operand) {
160 this.value -= operand;
161 }
162
163 /**
164 * Subtracts a value.
165 *
166 * @param operand
167 * the value to add
168 * @throws NullPointerException
169 * if the object is null
170 *
171 * @since Commons Lang 2.2
172 */
173 public void subtract(Number operand) {
174 this.value -= operand.floatValue();
175 }
176
177 //-----------------------------------------------------------------------
178 // shortValue and bytValue rely on Number implementation
179 /**
180 * Returns the value of this MutableFloat as a int.
181 *
182 * @return the numeric value represented by this object after conversion to type int.
183 */
184 public int intValue() {
185 return (int) value;
186 }
187
188 /**
189 * Returns the value of this MutableFloat as a long.
190 *
191 * @return the numeric value represented by this object after conversion to type long.
192 */
193 public long longValue() {
194 return (long) value;
195 }
196
197 /**
198 * Returns the value of this MutableFloat as a float.
199 *
200 * @return the numeric value represented by this object after conversion to type float.
201 */
202 public float floatValue() {
203 return value;
204 }
205
206 /**
207 * Returns the value of this MutableFloat as a double.
208 *
209 * @return the numeric value represented by this object after conversion to type double.
210 */
211 public double doubleValue() {
212 return value;
213 }
214
215 /**
216 * Checks whether the float value is the special NaN value.
217 *
218 * @return true if NaN
219 */
220 public boolean isNaN() {
221 return Float.isNaN(value);
222 }
223
224 /**
225 * Checks whether the float value is infinite.
226 *
227 * @return true if infinite
228 */
229 public boolean isInfinite() {
230 return Float.isInfinite(value);
231 }
232
233 //-----------------------------------------------------------------------
234 /**
235 * Gets this mutable as an instance of Float.
236 *
237 * @return a Float instance containing the value from this mutable
238 */
239 public Float toFloat() {
240 return new Float(floatValue());
241 }
242
243 //-----------------------------------------------------------------------
244 /**
245 * Compares this object against some other object. The result is <code>true</code> if and only if the argument is
246 * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the
247 * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this
248 * purpose, two float values are considered to be the same if and only if the method
249 * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
250 * <p>
251 * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>,
252 * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote>
253 *
254 * <pre>
255 * f1.floatValue() == f2.floatValue()
256 * </pre>
257 *
258 * </blockquote>
259 * <p>
260 * also has the value <code>true</code>. However, there are two exceptions:
261 * <ul>
262 * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the
263 * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has
264 * the value <code>false</code>.
265 * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>,
266 * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
267 * <code>0.0f==-0.0f</code> has the value <code>true</code>.
268 * </ul>
269 * This definition allows hashtables to operate properly.
270 *
271 * @param obj
272 * the object to be compared
273 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
274 * @see java.lang.Float#floatToIntBits(float)
275 */
276 public boolean equals(Object obj) {
277 return (obj instanceof MutableFloat)
278 && (Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value));
279 }
280
281 //-----------------------------------------------------------------------
282 /**
283 * Returns a suitable hashcode for this mutable.
284 *
285 * @return a suitable hashcode
286 */
287 public int hashCode() {
288 return Float.floatToIntBits(value);
289 }
290
291 /**
292 * Compares this mutable to another in ascending order.
293 *
294 * @param obj
295 * the mutable to compare to
296 * @return negative if this is less, zero if equal, positive if greater
297 */
298 public int compareTo(Object obj) {
299 MutableFloat other = (MutableFloat) obj;
300 float anotherVal = other.value;
301 return NumberUtils.compare(value, anotherVal);
302 }
303
304 /**
305 * Returns the String value of this mutable.
306 *
307 * @return the mutable value as a string
308 */
309 public String toString() {
310 return String.valueOf(value);
311 }
312
313 }