Coverage Report - org.apache.commons.lang3.mutable.MutableDouble
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableDouble
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>double</code> wrapper.
 21  
  * <p>
 22  
  * Note that as MutableDouble does not extend Double, it is not treated by String.format as a Double parameter. 
 23  
  * 
 24  
  * @see Double
 25  
  * @since 2.1
 26  
  */
 27  0
 public class MutableDouble extends Number implements Comparable<MutableDouble>, Mutable<Number> {
 28  
 
 29  
     /**
 30  
      * Required for serialization support.
 31  
      * 
 32  
      * @see java.io.Serializable
 33  
      */
 34  
     private static final long serialVersionUID = 1587163916L;
 35  
 
 36  
     /** The mutable value. */
 37  
     private double value;
 38  
 
 39  
     /**
 40  
      * Constructs a new MutableDouble with the default value of zero.
 41  
      */
 42  
     public MutableDouble() {
 43  3
         super();
 44  3
     }
 45  
 
 46  
     /**
 47  
      * Constructs a new MutableDouble with the specified value.
 48  
      * 
 49  
      * @param value  the initial value to store
 50  
      */
 51  
     public MutableDouble(final double value) {
 52  31
         super();
 53  31
         this.value = value;
 54  31
     }
 55  
 
 56  
     /**
 57  
      * Constructs a new MutableDouble 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 MutableDouble(final Number value) {
 63  3
         super();
 64  3
         this.value = value.doubleValue();
 65  2
     }
 66  
 
 67  
     /**
 68  
      * Constructs a new MutableDouble 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 double
 72  
      * @since 2.5
 73  
      */
 74  
     public MutableDouble(final String value) throws NumberFormatException {
 75  1
         super();
 76  1
         this.value = Double.parseDouble(value);
 77  1
     }
 78  
 
 79  
     //-----------------------------------------------------------------------
 80  
     /**
 81  
      * Gets the value as a Double instance.
 82  
      * 
 83  
      * @return the value as a Double, never null
 84  
      */
 85  
     @Override
 86  
     public Double getValue() {
 87  4
         return Double.valueOf(this.value);
 88  
     }
 89  
 
 90  
     /**
 91  
      * Sets the value.
 92  
      * 
 93  
      * @param value  the value to set
 94  
      */
 95  
     public void setValue(final double 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.doubleValue();
 108  2
     }
 109  
 
 110  
     //-----------------------------------------------------------------------
 111  
     /**
 112  
      * Checks whether the double value is the special NaN value.
 113  
      * 
 114  
      * @return true if NaN
 115  
      */
 116  
     public boolean isNaN() {
 117  1
         return Double.isNaN(value);
 118  
     }
 119  
 
 120  
     /**
 121  
      * Checks whether the double value is infinite.
 122  
      * 
 123  
      * @return true if infinite
 124  
      */
 125  
     public boolean isInfinite() {
 126  2
         return Double.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
 153  
      * @since Commons Lang 2.2
 154  
      */
 155  
     public void add(final double 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.doubleValue();
 168  1
     }
 169  
 
 170  
     /**
 171  
      * Subtracts a value from the value of this instance.
 172  
      * 
 173  
      * @param operand  the value to subtract, not null
 174  
      * @since Commons Lang 2.2
 175  
      */
 176  
     public void subtract(final double 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.doubleValue();
 189  1
     }
 190  
 
 191  
     //-----------------------------------------------------------------------
 192  
     // shortValue and byteValue rely on Number implementation
 193  
     /**
 194  
      * Returns the value of this MutableDouble 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  5
         return (int) value;
 201  
     }
 202  
 
 203  
     /**
 204  
      * Returns the value of this MutableDouble 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 MutableDouble 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  1
         return (float) value;
 221  
     }
 222  
 
 223  
     /**
 224  
      * Returns the value of this MutableDouble 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  18
         return value;
 231  
     }
 232  
 
 233  
     //-----------------------------------------------------------------------
 234  
     /**
 235  
      * Gets this mutable as an instance of Double.
 236  
      *
 237  
      * @return a Double instance containing the value from this mutable, never null
 238  
      */
 239  
     public Double toDouble() {
 240  2
         return Double.valueOf(doubleValue());
 241  
     }
 242  
 
 243  
     //-----------------------------------------------------------------------
 244  
     /**
 245  
      * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
 246  
      * is not <code>null</code> and is a <code>Double</code> object that represents a double that has the identical
 247  
      * bit pattern to the bit pattern of the double represented by this object. For this purpose, two
 248  
      * <code>double</code> values are considered to be the same if and only if the method
 249  
      * {@link Double#doubleToLongBits(double)}returns the same long value when applied to each.
 250  
      * <p>
 251  
      * Note that in most cases, for two instances of class <code>Double</code>,<code>d1</code> and <code>d2</code>,
 252  
      * the value of <code>d1.equals(d2)</code> is <code>true</code> if and only if <blockquote>
 253  
      * 
 254  
      * <pre>
 255  
      *   d1.doubleValue()&nbsp;== d2.doubleValue()
 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>d1</code> and <code>d2</code> both represent <code>Double.NaN</code>, then the
 263  
      * <code>equals</code> method returns <code>true</code>, even though <code>Double.NaN==Double.NaN</code> has
 264  
      * the value <code>false</code>.
 265  
      * <li>If <code>d1</code> represents <code>+0.0</code> while <code>d2</code> represents <code>-0.0</code>,
 266  
      * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
 267  
      * <code>+0.0==-0.0</code> has the value <code>true</code>. This allows hashtables to operate properly.
 268  
      * </ul>
 269  
      * 
 270  
      * @param obj  the object to compare with, null returns false
 271  
      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
 272  
      */
 273  
     @Override
 274  
     public boolean equals(final Object obj) {
 275  10
         return obj instanceof MutableDouble
 276  
             && Double.doubleToLongBits(((MutableDouble) obj).value) == Double.doubleToLongBits(value);
 277  
     }
 278  
 
 279  
     /**
 280  
      * Returns a suitable hash code for this mutable.
 281  
      * 
 282  
      * @return a suitable hash code
 283  
      */
 284  
     @Override
 285  
     public int hashCode() {
 286  7
         final long bits = Double.doubleToLongBits(value);
 287  7
         return (int) (bits ^ bits >>> 32);
 288  
     }
 289  
 
 290  
     //-----------------------------------------------------------------------
 291  
     /**
 292  
      * Compares this mutable to another in ascending order.
 293  
      * 
 294  
      * @param other  the other mutable to compare to, not null
 295  
      * @return negative if this is less, zero if equal, positive if greater
 296  
      */
 297  
     @Override
 298  
     public int compareTo(final MutableDouble other) {
 299  4
         return Double.compare(this.value, other.value);
 300  
     }
 301  
 
 302  
     //-----------------------------------------------------------------------
 303  
     /**
 304  
      * Returns the String value of this mutable.
 305  
      * 
 306  
      * @return the mutable value as a string
 307  
      */
 308  
     @Override
 309  
     public String toString() {
 310  3
         return String.valueOf(value);
 311  
     }
 312  
 
 313  
 }