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.lang3.mutable;
018    
019    /**
020     * A mutable <code>float</code> wrapper.
021     * 
022     * @see Float
023     * @since 2.1
024     * @version $Id: MutableFloat.java 1096472 2011-04-25 13:28:06Z mbenson $
025     */
026    public class MutableFloat extends Number implements Comparable<MutableFloat>, Mutable<Number> {
027    
028        /**
029         * Required for serialization support.
030         * 
031         * @see java.io.Serializable
032         */
033        private static final long serialVersionUID = 5787169186L;
034    
035        /** The mutable value. */
036        private float value;
037    
038        /**
039         * Constructs a new MutableFloat with the default value of zero.
040         */
041        public MutableFloat() {
042            super();
043        }
044    
045        /**
046         * Constructs a new MutableFloat with the specified value.
047         * 
048         * @param value  the initial value to store
049         */
050        public MutableFloat(float value) {
051            super();
052            this.value = value;
053        }
054    
055        /**
056         * Constructs a new MutableFloat with the specified value.
057         * 
058         * @param value  the initial value to store, not null
059         * @throws NullPointerException if the object is null
060         */
061        public MutableFloat(Number value) {
062            super();
063            this.value = value.floatValue();
064        }
065    
066        /**
067         * Constructs a new MutableFloat parsing the given string.
068         * 
069         * @param value  the string to parse, not null
070         * @throws NumberFormatException if the string cannot be parsed into a float
071         * @since 2.5
072         */
073        public MutableFloat(String value) throws NumberFormatException {
074            super();
075            this.value = Float.parseFloat(value);
076        }
077    
078        //-----------------------------------------------------------------------
079        /**
080         * Gets the value as a Float instance.
081         * 
082         * @return the value as a Float, never null
083         */
084        public Float getValue() {
085            return new Float(this.value);
086        }
087    
088        /**
089         * Sets the value.
090         * 
091         * @param value  the value to set
092         */
093        public void setValue(float value) {
094            this.value = value;
095        }
096    
097        /**
098         * Sets the value from any Number instance.
099         * 
100         * @param value  the value to set, not null
101         * @throws NullPointerException if the object is null
102         */
103        public void setValue(Number value) {
104            this.value = value.floatValue();
105        }
106    
107        //-----------------------------------------------------------------------
108        /**
109         * Checks whether the float value is the special NaN value.
110         * 
111         * @return true if NaN
112         */
113        public boolean isNaN() {
114            return Float.isNaN(value);
115        }
116    
117        /**
118         * Checks whether the float value is infinite.
119         * 
120         * @return true if infinite
121         */
122        public boolean isInfinite() {
123            return Float.isInfinite(value);
124        }
125    
126        //-----------------------------------------------------------------------
127        /**
128         * Increments the value.
129         *
130         * @since Commons Lang 2.2
131         */
132        public void increment() {
133            value++;
134        }
135    
136        /**
137         * Decrements the value.
138         *
139         * @since Commons Lang 2.2
140         */
141        public void decrement() {
142            value--;
143        }
144    
145        //-----------------------------------------------------------------------
146        /**
147         * Adds a value to the value of this instance.
148         * 
149         * @param operand  the value to add, not null
150         * @since Commons Lang 2.2
151         */
152        public void add(float operand) {
153            this.value += operand;
154        }
155    
156        /**
157         * Adds a value to the value of this instance.
158         * 
159         * @param operand  the value to add, not null
160         * @throws NullPointerException if the object is null
161         * @since Commons Lang 2.2
162         */
163        public void add(Number operand) {
164            this.value += operand.floatValue();
165        }
166    
167        /**
168         * Subtracts a value from the value of this instance.
169         * 
170         * @param operand  the value to subtract
171         * @since Commons Lang 2.2
172         */
173        public void subtract(float operand) {
174            this.value -= operand;
175        }
176    
177        /**
178         * Subtracts a value from the value of this instance.
179         * 
180         * @param operand  the value to subtract, not null
181         * @throws NullPointerException if the object is null
182         * @since Commons Lang 2.2
183         */
184        public void subtract(Number operand) {
185            this.value -= operand.floatValue();
186        }
187    
188        //-----------------------------------------------------------------------
189        // shortValue and byteValue rely on Number implementation
190        /**
191         * Returns the value of this MutableFloat as an int.
192         *
193         * @return the numeric value represented by this object after conversion to type int.
194         */
195        @Override
196        public int intValue() {
197            return (int) value;
198        }
199    
200        /**
201         * Returns the value of this MutableFloat as a long.
202         *
203         * @return the numeric value represented by this object after conversion to type long.
204         */
205        @Override
206        public long longValue() {
207            return (long) value;
208        }
209    
210        /**
211         * Returns the value of this MutableFloat as a float.
212         *
213         * @return the numeric value represented by this object after conversion to type float.
214         */
215        @Override
216        public float floatValue() {
217            return value;
218        }
219    
220        /**
221         * Returns the value of this MutableFloat as a double.
222         *
223         * @return the numeric value represented by this object after conversion to type double.
224         */
225        @Override
226        public double doubleValue() {
227            return value;
228        }
229    
230        //-----------------------------------------------------------------------
231        /**
232         * Gets this mutable as an instance of Float.
233         *
234         * @return a Float instance containing the value from this mutable, never null
235         */
236        public Float toFloat() {
237            return Float.valueOf(floatValue());
238        }
239    
240        //-----------------------------------------------------------------------
241        /**
242         * Compares this object against some other object. The result is <code>true</code> if and only if the argument is
243         * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the
244         * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this
245         * purpose, two float values are considered to be the same if and only if the method
246         * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
247         * <p>
248         * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>,
249         * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote>
250         * 
251         * <pre>
252         *   f1.floatValue() == f2.floatValue()
253         * </pre>
254         * 
255         * </blockquote>
256         * <p>
257         * also has the value <code>true</code>. However, there are two exceptions:
258         * <ul>
259         * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the
260         * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has
261         * the value <code>false</code>.
262         * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>,
263         * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
264         * <code>0.0f==-0.0f</code> has the value <code>true</code>.
265         * </ul>
266         * This definition allows hashtables to operate properly.
267         * 
268         * @param obj  the object to compare with, null returns false
269         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
270         * @see java.lang.Float#floatToIntBits(float)
271         */
272        @Override
273        public boolean equals(Object obj) {
274            return (obj instanceof MutableFloat)
275                && (Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value));
276        }
277    
278        /**
279         * Returns a suitable hash code for this mutable.
280         * 
281         * @return a suitable hash code
282         */
283        @Override
284        public int hashCode() {
285            return Float.floatToIntBits(value);
286        }
287    
288        //-----------------------------------------------------------------------
289        /**
290         * Compares this mutable to another in ascending order.
291         * 
292         * @param other  the other mutable to compare to, not null
293         * @return negative if this is less, zero if equal, positive if greater
294         */
295        public int compareTo(MutableFloat other) {
296            float anotherVal = other.value;
297            return Float.compare(value, anotherVal);
298        }
299    
300        //-----------------------------------------------------------------------
301        /**
302         * Returns the String value of this mutable.
303         * 
304         * @return the mutable value as a string
305         */
306        @Override
307        public String toString() {
308            return String.valueOf(value);
309        }
310    
311    }