View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.mutable;
18  
19  /**
20   * A mutable <code>float</code> wrapper.
21   * <p>
22   * Note that as MutableFloat does not extend Float, it is not treated by String.format as a Float parameter. 
23   * 
24   * @see Float
25   * @since 2.1
26   */
27  public class MutableFloat extends Number implements Comparable<MutableFloat>, Mutable<Number> {
28  
29      /**
30       * Required for serialization support.
31       * 
32       * @see java.io.Serializable
33       */
34      private static final long serialVersionUID = 5787169186L;
35  
36      /** The mutable value. */
37      private float value;
38  
39      /**
40       * Constructs a new MutableFloat with the default value of zero.
41       */
42      public MutableFloat() {
43          super();
44      }
45  
46      /**
47       * Constructs a new MutableFloat with the specified value.
48       * 
49       * @param value  the initial value to store
50       */
51      public MutableFloat(final float value) {
52          super();
53          this.value = value;
54      }
55  
56      /**
57       * Constructs a new MutableFloat with the specified value.
58       * 
59       * @param value  the initial value to store, not null
60       * @throws NullPointerException if the object is null
61       */
62      public MutableFloat(final Number value) {
63          super();
64          this.value = value.floatValue();
65      }
66  
67      /**
68       * Constructs a new MutableFloat parsing the given string.
69       * 
70       * @param value  the string to parse, not null
71       * @throws NumberFormatException if the string cannot be parsed into a float
72       * @since 2.5
73       */
74      public MutableFloat(final String value) throws NumberFormatException {
75          super();
76          this.value = Float.parseFloat(value);
77      }
78  
79      //-----------------------------------------------------------------------
80      /**
81       * Gets the value as a Float instance.
82       * 
83       * @return the value as a Float, never null
84       */
85      @Override
86      public Float getValue() {
87          return Float.valueOf(this.value);
88      }
89  
90      /**
91       * Sets the value.
92       * 
93       * @param value  the value to set
94       */
95      public void setValue(final float value) {
96          this.value = value;
97      }
98  
99      /**
100      * Sets the value from any Number instance.
101      * 
102      * @param value  the value to set, not null
103      * @throws NullPointerException if the object is null
104      */
105     @Override
106     public void setValue(final Number value) {
107         this.value = value.floatValue();
108     }
109 
110     //-----------------------------------------------------------------------
111     /**
112      * Checks whether the float value is the special NaN value.
113      * 
114      * @return true if NaN
115      */
116     public boolean isNaN() {
117         return Float.isNaN(value);
118     }
119 
120     /**
121      * Checks whether the float value is infinite.
122      * 
123      * @return true if infinite
124      */
125     public boolean isInfinite() {
126         return Float.isInfinite(value);
127     }
128 
129     //-----------------------------------------------------------------------
130     /**
131      * Increments the value.
132      *
133      * @since Commons Lang 2.2
134      */
135     public void increment() {
136         value++;
137     }
138 
139     /**
140      * Increments this instance's value by 1; this method returns the value associated with the instance
141      * immediately prior to the increment operation. This method is not thread safe.
142      *
143      * @return the value associated with the instance before it was incremented
144      * @since 3.5
145      */
146     public float getAndIncrement() {
147         float last = value;
148         value++;
149         return last;
150     }
151 
152     /**
153      * Increments this instance's value by 1; this method returns the value associated with the instance
154      * immediately after the increment operation. This method is not thread safe.
155      *
156      * @return the value associated with the instance after it is incremented
157      * @since 3.5
158      */
159     public float incrementAndGet() {
160         value++;
161         return value;
162     }
163 
164     /**
165      * Decrements the value.
166      *
167      * @since Commons Lang 2.2
168      */
169     public void decrement() {
170         value--;
171     }
172 
173     /**
174      * Decrements this instance's value by 1; this method returns the value associated with the instance
175      * immediately prior to the decrement operation. This method is not thread safe.
176      *
177      * @return the value associated with the instance before it was decremented
178      * @since 3.5
179      */
180     public float getAndDecrement() {
181         float last = value;
182         value--;
183         return last;
184     }
185 
186     /**
187      * Decrements this instance's value by 1; this method returns the value associated with the instance
188      * immediately after the decrement operation. This method is not thread safe.
189      *
190      * @return the value associated with the instance after it is decremented
191      * @since 3.5
192      */
193     public float decrementAndGet() {
194         value--;
195         return value;
196     }
197 
198     //-----------------------------------------------------------------------
199     /**
200      * Adds a value to the value of this instance.
201      * 
202      * @param operand  the value to add, not null
203      * @since Commons Lang 2.2
204      */
205     public void add(final float operand) {
206         this.value += operand;
207     }
208 
209     /**
210      * Adds a value to the value of this instance.
211      * 
212      * @param operand  the value to add, not null
213      * @throws NullPointerException if the object is null
214      * @since Commons Lang 2.2
215      */
216     public void add(final Number operand) {
217         this.value += operand.floatValue();
218     }
219 
220     /**
221      * Subtracts a value from the value of this instance.
222      * 
223      * @param operand  the value to subtract
224      * @since Commons Lang 2.2
225      */
226     public void subtract(final float operand) {
227         this.value -= operand;
228     }
229 
230     /**
231      * Subtracts a value from the value of this instance.
232      * 
233      * @param operand  the value to subtract, not null
234      * @throws NullPointerException if the object is null
235      * @since Commons Lang 2.2
236      */
237     public void subtract(final Number operand) {
238         this.value -= operand.floatValue();
239     }
240 
241     /**
242      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
243      * immediately after the addition operation. This method is not thread safe.
244      *
245      * @param operand the quantity to add, not null
246      * @return the value associated with this instance after adding the operand
247      */
248     public float addAndGet(final float operand) {
249         this.value += operand;
250         return value;
251     }
252 
253     /**
254      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
255      * immediately after the addition operation. This method is not thread safe.
256      *
257      * @param operand the quantity to add, not null
258      * @throws NullPointerException if {@code operand} is null
259      * @return the value associated with this instance after adding the operand
260      */
261     public float addAndGet(final Number operand) {
262         this.value += operand.floatValue();
263         return value;
264     }
265 
266     /**
267      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
268      * immediately prior to the addition operation. This method is not thread safe.
269      *
270      * @param operand the quantity to add, not null
271      * @return the value associated with this instance immediately before the operand was added
272      */
273     public float getAndAdd(final float operand) {
274         float last = value;
275         this.value += operand;
276         return last;
277     }
278 
279     /**
280      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
281      * immediately prior to the addition operation. This method is not thread safe.
282      *
283      * @param operand the quantity to add, not null
284      * @throws NullPointerException if {@code operand} is null
285      * @return the value associated with this instance immediately before the operand was added
286      */
287     public float getAndAdd(final Number operand) {
288         float last = value;
289         this.value += operand.floatValue();
290         return last;
291     }
292 
293     //-----------------------------------------------------------------------
294     // shortValue and byteValue rely on Number implementation
295     /**
296      * Returns the value of this MutableFloat as an int.
297      *
298      * @return the numeric value represented by this object after conversion to type int.
299      */
300     @Override
301     public int intValue() {
302         return (int) value;
303     }
304 
305     /**
306      * Returns the value of this MutableFloat as a long.
307      *
308      * @return the numeric value represented by this object after conversion to type long.
309      */
310     @Override
311     public long longValue() {
312         return (long) value;
313     }
314 
315     /**
316      * Returns the value of this MutableFloat as a float.
317      *
318      * @return the numeric value represented by this object after conversion to type float.
319      */
320     @Override
321     public float floatValue() {
322         return value;
323     }
324 
325     /**
326      * Returns the value of this MutableFloat as a double.
327      *
328      * @return the numeric value represented by this object after conversion to type double.
329      */
330     @Override
331     public double doubleValue() {
332         return value;
333     }
334 
335     //-----------------------------------------------------------------------
336     /**
337      * Gets this mutable as an instance of Float.
338      *
339      * @return a Float instance containing the value from this mutable, never null
340      */
341     public Float toFloat() {
342         return Float.valueOf(floatValue());
343     }
344 
345     //-----------------------------------------------------------------------
346     /**
347      * Compares this object against some other object. The result is <code>true</code> if and only if the argument is
348      * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the
349      * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this
350      * purpose, two float values are considered to be the same if and only if the method
351      * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
352      * <p>
353      * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>,
354      * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote>
355      * 
356      * <pre>
357      *   f1.floatValue() == f2.floatValue()
358      * </pre>
359      * 
360      * </blockquote>
361      * <p>
362      * also has the value <code>true</code>. However, there are two exceptions:
363      * <ul>
364      * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the
365      * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has
366      * the value <code>false</code>.
367      * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>,
368      * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
369      * <code>0.0f==-0.0f</code> has the value <code>true</code>.
370      * </ul>
371      * This definition allows hashtables to operate properly.
372      * 
373      * @param obj  the object to compare with, null returns false
374      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
375      * @see java.lang.Float#floatToIntBits(float)
376      */
377     @Override
378     public boolean equals(final Object obj) {
379         return obj instanceof MutableFloat
380             && Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value);
381     }
382 
383     /**
384      * Returns a suitable hash code for this mutable.
385      * 
386      * @return a suitable hash code
387      */
388     @Override
389     public int hashCode() {
390         return Float.floatToIntBits(value);
391     }
392 
393     //-----------------------------------------------------------------------
394     /**
395      * Compares this mutable to another in ascending order.
396      * 
397      * @param other  the other mutable to compare to, not null
398      * @return negative if this is less, zero if equal, positive if greater
399      */
400     @Override
401     public int compareTo(final MutableFloat other) {
402         return Float.compare(this.value, other.value);
403     }
404 
405     //-----------------------------------------------------------------------
406     /**
407      * Returns the String value of this mutable.
408      * 
409      * @return the mutable value as a string
410      */
411     @Override
412     public String toString() {
413         return String.valueOf(value);
414     }
415 
416 }