Coverage Report - org.apache.commons.lang3.mutable.MutableFloat
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableFloat
97%
39/40
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  
  */
 27  0
 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  3
         super();
 44  3
     }
 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  31
         super();
 53  31
         this.value = value;
 54  31
     }
 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  3
         super();
 64  3
         this.value = value.floatValue();
 65  2
     }
 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  1
         super();
 76  1
         this.value = Float.parseFloat(value);
 77  1
     }
 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  4
         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  1
         this.value = value;
 97  1
     }
 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  3
         this.value = value.floatValue();
 108  2
     }
 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  1
         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  2
         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  1
         value++;
 137  1
     }
 138  
 
 139  
     /**
 140  
      * Decrements the value.
 141  
      *
 142  
      * @since Commons Lang 2.2
 143  
      */
 144  
     public void decrement() {
 145  1
         value--;
 146  1
     }
 147  
 
 148  
     //-----------------------------------------------------------------------
 149  
     /**
 150  
      * Adds a value to the value of this instance.
 151  
      * 
 152  
      * @param operand  the value to add, not null
 153  
      * @since Commons Lang 2.2
 154  
      */
 155  
     public void add(final float operand) {
 156  1
         this.value += operand;
 157  1
     }
 158  
 
 159  
     /**
 160  
      * Adds a value to the value of this instance.
 161  
      * 
 162  
      * @param operand  the value to add, not null
 163  
      * @throws NullPointerException if the object is null
 164  
      * @since Commons Lang 2.2
 165  
      */
 166  
     public void add(final Number operand) {
 167  1
         this.value += operand.floatValue();
 168  1
     }
 169  
 
 170  
     /**
 171  
      * Subtracts a value from the value of this instance.
 172  
      * 
 173  
      * @param operand  the value to subtract
 174  
      * @since Commons Lang 2.2
 175  
      */
 176  
     public void subtract(final float operand) {
 177  1
         this.value -= operand;
 178  1
     }
 179  
 
 180  
     /**
 181  
      * Subtracts a value from the value of this instance.
 182  
      * 
 183  
      * @param operand  the value to subtract, not null
 184  
      * @throws NullPointerException if the object is null
 185  
      * @since Commons Lang 2.2
 186  
      */
 187  
     public void subtract(final Number operand) {
 188  1
         this.value -= operand.floatValue();
 189  1
     }
 190  
 
 191  
     //-----------------------------------------------------------------------
 192  
     // shortValue and byteValue rely on Number implementation
 193  
     /**
 194  
      * Returns the value of this MutableFloat as an int.
 195  
      *
 196  
      * @return the numeric value represented by this object after conversion to type int.
 197  
      */
 198  
     @Override
 199  
     public int intValue() {
 200  6
         return (int) value;
 201  
     }
 202  
 
 203  
     /**
 204  
      * Returns the value of this MutableFloat as a long.
 205  
      *
 206  
      * @return the numeric value represented by this object after conversion to type long.
 207  
      */
 208  
     @Override
 209  
     public long longValue() {
 210  3
         return (long) value;
 211  
     }
 212  
 
 213  
     /**
 214  
      * Returns the value of this MutableFloat as a float.
 215  
      *
 216  
      * @return the numeric value represented by this object after conversion to type float.
 217  
      */
 218  
     @Override
 219  
     public float floatValue() {
 220  17
         return value;
 221  
     }
 222  
 
 223  
     /**
 224  
      * Returns the value of this MutableFloat as a double.
 225  
      *
 226  
      * @return the numeric value represented by this object after conversion to type double.
 227  
      */
 228  
     @Override
 229  
     public double doubleValue() {
 230  1
         return 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, never null
 238  
      */
 239  
     public Float toFloat() {
 240  2
         return Float.valueOf(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  the object to compare with, null returns false
 272  
      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
 273  
      * @see java.lang.Float#floatToIntBits(float)
 274  
      */
 275  
     @Override
 276  
     public boolean equals(final Object obj) {
 277  10
         return obj instanceof MutableFloat
 278  
             && Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value);
 279  
     }
 280  
 
 281  
     /**
 282  
      * Returns a suitable hash code for this mutable.
 283  
      * 
 284  
      * @return a suitable hash code
 285  
      */
 286  
     @Override
 287  
     public int hashCode() {
 288  7
         return Float.floatToIntBits(value);
 289  
     }
 290  
 
 291  
     //-----------------------------------------------------------------------
 292  
     /**
 293  
      * Compares this mutable to another in ascending order.
 294  
      * 
 295  
      * @param other  the other mutable to compare to, not null
 296  
      * @return negative if this is less, zero if equal, positive if greater
 297  
      */
 298  
     @Override
 299  
     public int compareTo(final MutableFloat other) {
 300  4
         return Float.compare(this.value, other.value);
 301  
     }
 302  
 
 303  
     //-----------------------------------------------------------------------
 304  
     /**
 305  
      * Returns the String value of this mutable.
 306  
      * 
 307  
      * @return the mutable value as a string
 308  
      */
 309  
     @Override
 310  
     public String toString() {
 311  3
         return String.valueOf(value);
 312  
     }
 313  
 
 314  
 }