Coverage Report - org.apache.commons.lang3.mutable.MutableFloat
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableFloat
97%
40/41
100%
4/4
1,042
 
 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  
  * @version $Id: MutableFloat.java 1436770 2013-01-22 07:09:45Z ggregory $
 27  
  */
 28  0
 public class MutableFloat extends Number implements Comparable<MutableFloat>, Mutable<Number> {
 29  
 
 30  
     /**
 31  
      * Required for serialization support.
 32  
      * 
 33  
      * @see java.io.Serializable
 34  
      */
 35  
     private static final long serialVersionUID = 5787169186L;
 36  
 
 37  
     /** The mutable value. */
 38  
     private float value;
 39  
 
 40  
     /**
 41  
      * Constructs a new MutableFloat with the default value of zero.
 42  
      */
 43  
     public MutableFloat() {
 44  3
         super();
 45  3
     }
 46  
 
 47  
     /**
 48  
      * Constructs a new MutableFloat with the specified value.
 49  
      * 
 50  
      * @param value  the initial value to store
 51  
      */
 52  
     public MutableFloat(final float value) {
 53  31
         super();
 54  31
         this.value = value;
 55  31
     }
 56  
 
 57  
     /**
 58  
      * Constructs a new MutableFloat with the specified value.
 59  
      * 
 60  
      * @param value  the initial value to store, not null
 61  
      * @throws NullPointerException if the object is null
 62  
      */
 63  
     public MutableFloat(final Number value) {
 64  3
         super();
 65  3
         this.value = value.floatValue();
 66  2
     }
 67  
 
 68  
     /**
 69  
      * Constructs a new MutableFloat parsing the given string.
 70  
      * 
 71  
      * @param value  the string to parse, not null
 72  
      * @throws NumberFormatException if the string cannot be parsed into a float
 73  
      * @since 2.5
 74  
      */
 75  
     public MutableFloat(final String value) throws NumberFormatException {
 76  1
         super();
 77  1
         this.value = Float.parseFloat(value);
 78  1
     }
 79  
 
 80  
     //-----------------------------------------------------------------------
 81  
     /**
 82  
      * Gets the value as a Float instance.
 83  
      * 
 84  
      * @return the value as a Float, never null
 85  
      */
 86  
     @Override
 87  
     public Float getValue() {
 88  4
         return Float.valueOf(this.value);
 89  
     }
 90  
 
 91  
     /**
 92  
      * Sets the value.
 93  
      * 
 94  
      * @param value  the value to set
 95  
      */
 96  
     public void setValue(final float value) {
 97  1
         this.value = value;
 98  1
     }
 99  
 
 100  
     /**
 101  
      * Sets the value from any Number instance.
 102  
      * 
 103  
      * @param value  the value to set, not null
 104  
      * @throws NullPointerException if the object is null
 105  
      */
 106  
     @Override
 107  
     public void setValue(final Number value) {
 108  3
         this.value = value.floatValue();
 109  2
     }
 110  
 
 111  
     //-----------------------------------------------------------------------
 112  
     /**
 113  
      * Checks whether the float value is the special NaN value.
 114  
      * 
 115  
      * @return true if NaN
 116  
      */
 117  
     public boolean isNaN() {
 118  1
         return Float.isNaN(value);
 119  
     }
 120  
 
 121  
     /**
 122  
      * Checks whether the float value is infinite.
 123  
      * 
 124  
      * @return true if infinite
 125  
      */
 126  
     public boolean isInfinite() {
 127  2
         return Float.isInfinite(value);
 128  
     }
 129  
 
 130  
     //-----------------------------------------------------------------------
 131  
     /**
 132  
      * Increments the value.
 133  
      *
 134  
      * @since Commons Lang 2.2
 135  
      */
 136  
     public void increment() {
 137  1
         value++;
 138  1
     }
 139  
 
 140  
     /**
 141  
      * Decrements the value.
 142  
      *
 143  
      * @since Commons Lang 2.2
 144  
      */
 145  
     public void decrement() {
 146  1
         value--;
 147  1
     }
 148  
 
 149  
     //-----------------------------------------------------------------------
 150  
     /**
 151  
      * Adds a value to the value of this instance.
 152  
      * 
 153  
      * @param operand  the value to add, not null
 154  
      * @since Commons Lang 2.2
 155  
      */
 156  
     public void add(final float operand) {
 157  1
         this.value += operand;
 158  1
     }
 159  
 
 160  
     /**
 161  
      * Adds a value to the value of this instance.
 162  
      * 
 163  
      * @param operand  the value to add, not null
 164  
      * @throws NullPointerException if the object is null
 165  
      * @since Commons Lang 2.2
 166  
      */
 167  
     public void add(final Number operand) {
 168  1
         this.value += operand.floatValue();
 169  1
     }
 170  
 
 171  
     /**
 172  
      * Subtracts a value from the value of this instance.
 173  
      * 
 174  
      * @param operand  the value to subtract
 175  
      * @since Commons Lang 2.2
 176  
      */
 177  
     public void subtract(final float operand) {
 178  1
         this.value -= operand;
 179  1
     }
 180  
 
 181  
     /**
 182  
      * Subtracts a value from the value of this instance.
 183  
      * 
 184  
      * @param operand  the value to subtract, not null
 185  
      * @throws NullPointerException if the object is null
 186  
      * @since Commons Lang 2.2
 187  
      */
 188  
     public void subtract(final Number operand) {
 189  1
         this.value -= operand.floatValue();
 190  1
     }
 191  
 
 192  
     //-----------------------------------------------------------------------
 193  
     // shortValue and byteValue rely on Number implementation
 194  
     /**
 195  
      * Returns the value of this MutableFloat as an int.
 196  
      *
 197  
      * @return the numeric value represented by this object after conversion to type int.
 198  
      */
 199  
     @Override
 200  
     public int intValue() {
 201  6
         return (int) value;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Returns the value of this MutableFloat as a long.
 206  
      *
 207  
      * @return the numeric value represented by this object after conversion to type long.
 208  
      */
 209  
     @Override
 210  
     public long longValue() {
 211  3
         return (long) value;
 212  
     }
 213  
 
 214  
     /**
 215  
      * Returns the value of this MutableFloat as a float.
 216  
      *
 217  
      * @return the numeric value represented by this object after conversion to type float.
 218  
      */
 219  
     @Override
 220  
     public float floatValue() {
 221  17
         return value;
 222  
     }
 223  
 
 224  
     /**
 225  
      * Returns the value of this MutableFloat as a double.
 226  
      *
 227  
      * @return the numeric value represented by this object after conversion to type double.
 228  
      */
 229  
     @Override
 230  
     public double doubleValue() {
 231  1
         return value;
 232  
     }
 233  
 
 234  
     //-----------------------------------------------------------------------
 235  
     /**
 236  
      * Gets this mutable as an instance of Float.
 237  
      *
 238  
      * @return a Float instance containing the value from this mutable, never null
 239  
      */
 240  
     public Float toFloat() {
 241  2
         return Float.valueOf(floatValue());
 242  
     }
 243  
 
 244  
     //-----------------------------------------------------------------------
 245  
     /**
 246  
      * Compares this object against some other object. The result is <code>true</code> if and only if the argument is
 247  
      * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the
 248  
      * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this
 249  
      * purpose, two float values are considered to be the same if and only if the method
 250  
      * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
 251  
      * <p>
 252  
      * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>,
 253  
      * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote>
 254  
      * 
 255  
      * <pre>
 256  
      *   f1.floatValue() == f2.floatValue()
 257  
      * </pre>
 258  
      * 
 259  
      * </blockquote>
 260  
      * <p>
 261  
      * also has the value <code>true</code>. However, there are two exceptions:
 262  
      * <ul>
 263  
      * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the
 264  
      * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has
 265  
      * the value <code>false</code>.
 266  
      * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>,
 267  
      * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
 268  
      * <code>0.0f==-0.0f</code> has the value <code>true</code>.
 269  
      * </ul>
 270  
      * This definition allows hashtables to operate properly.
 271  
      * 
 272  
      * @param obj  the object to compare with, null returns false
 273  
      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
 274  
      * @see java.lang.Float#floatToIntBits(float)
 275  
      */
 276  
     @Override
 277  
     public boolean equals(final Object obj) {
 278  10
         return obj instanceof MutableFloat
 279  
             && Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value);
 280  
     }
 281  
 
 282  
     /**
 283  
      * Returns a suitable hash code for this mutable.
 284  
      * 
 285  
      * @return a suitable hash code
 286  
      */
 287  
     @Override
 288  
     public int hashCode() {
 289  7
         return Float.floatToIntBits(value);
 290  
     }
 291  
 
 292  
     //-----------------------------------------------------------------------
 293  
     /**
 294  
      * Compares this mutable to another in ascending order.
 295  
      * 
 296  
      * @param other  the other mutable to compare to, not null
 297  
      * @return negative if this is less, zero if equal, positive if greater
 298  
      */
 299  
     @Override
 300  
     public int compareTo(final MutableFloat other) {
 301  4
         final float anotherVal = other.value;
 302  3
         return Float.compare(value, anotherVal);
 303  
     }
 304  
 
 305  
     //-----------------------------------------------------------------------
 306  
     /**
 307  
      * Returns the String value of this mutable.
 308  
      * 
 309  
      * @return the mutable value as a string
 310  
      */
 311  
     @Override
 312  
     public String toString() {
 313  3
         return String.valueOf(value);
 314  
     }
 315  
 
 316  
 }