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 * https://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 */ 017package org.apache.commons.lang3.mutable; 018 019/** 020 * A mutable {@code double} wrapper. 021 * <p> 022 * Note that as MutableDouble does not extend Double, it is not treated by String.format as a Double parameter. 023 * </p> 024 * 025 * @see Double 026 * @since 2.1 027 */ 028public class MutableDouble extends Number implements Comparable<MutableDouble>, Mutable<Number> { 029 030 /** 031 * Required for serialization support. 032 * 033 * @see java.io.Serializable 034 */ 035 private static final long serialVersionUID = 1587163916L; 036 037 /** The mutable value. */ 038 private double value; 039 040 /** 041 * Constructs a new MutableDouble with the default value of zero. 042 */ 043 public MutableDouble() { 044 } 045 046 /** 047 * Constructs a new MutableDouble with the specified value. 048 * 049 * @param value the initial value to store 050 */ 051 public MutableDouble(final double value) { 052 this.value = value; 053 } 054 055 /** 056 * Constructs a new MutableDouble 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 MutableDouble(final Number value) { 062 this.value = value.doubleValue(); 063 } 064 065 /** 066 * Constructs a new MutableDouble parsing the given string. 067 * 068 * @param value the string to parse, not null 069 * @throws NumberFormatException if the string cannot be parsed into a double, see {@link Double#parseDouble(String)}. 070 * @since 2.5 071 */ 072 public MutableDouble(final String value) { 073 this.value = Double.parseDouble(value); 074 } 075 076 /** 077 * Adds a value to the value of this instance. 078 * 079 * @param operand the value to add 080 * @since 2.2 081 */ 082 public void add(final double operand) { 083 this.value += operand; 084 } 085 086 /** 087 * Adds a value to the value of this instance. 088 * 089 * @param operand the value to add, not null 090 * @throws NullPointerException if the object is null 091 * @since 2.2 092 */ 093 public void add(final Number operand) { 094 this.value += operand.doubleValue(); 095 } 096 097 /** 098 * Increments this instance's value by {@code operand}; this method returns the value associated with the instance 099 * immediately after the addition operation. This method is not thread safe. 100 * 101 * @param operand the quantity to add, not null 102 * @return the value associated with this instance after adding the operand 103 * @since 3.5 104 */ 105 public double addAndGet(final double operand) { 106 this.value += operand; 107 return value; 108 } 109 110 /** 111 * Increments this instance's value by {@code operand}; this method returns the value associated with the instance 112 * immediately after the addition operation. This method is not thread safe. 113 * 114 * @param operand the quantity to add, not null 115 * @throws NullPointerException if {@code operand} is null 116 * @return the value associated with this instance after adding the operand 117 * @since 3.5 118 */ 119 public double addAndGet(final Number operand) { 120 this.value += operand.doubleValue(); 121 return value; 122 } 123 124 /** 125 * Compares this mutable to another in ascending order. 126 * 127 * @param other the other mutable to compare to, not null 128 * @return negative if this is less, zero if equal, positive if greater 129 */ 130 @Override 131 public int compareTo(final MutableDouble other) { 132 return Double.compare(this.value, other.value); 133 } 134 135 /** 136 * Decrements the value. 137 * 138 * @since 2.2 139 */ 140 public void decrement() { 141 value--; 142 } 143 144 /** 145 * Decrements this instance's value by 1; this method returns the value associated with the instance 146 * immediately after the decrement operation. This method is not thread safe. 147 * 148 * @return the value associated with the instance after it is decremented 149 * @since 3.5 150 */ 151 public double decrementAndGet() { 152 value--; 153 return value; 154 } 155 156 /** 157 * Returns the value of this MutableDouble as a double. 158 * 159 * @return the numeric value represented by this object after conversion to type double. 160 */ 161 @Override 162 public double doubleValue() { 163 return value; 164 } 165 166 /** 167 * Compares this object against the specified object. The result is {@code true} if and only if the argument is not {@code null} and is a {@link Double} 168 * object that represents a double that has the identical bit pattern to the bit pattern of the double represented by this object. For this purpose, two 169 * {@code double} values are considered to be the same if and only if the method {@link Double#doubleToLongBits(double)}returns the same long value when 170 * applied to each. 171 * <p> 172 * Note that in most cases, for two instances of class {@link Double},{@code d1} and {@code d2}, the value of {@code d1.equals(d2)} is {@code true} if and 173 * only if: 174 * </p> 175 * <pre> 176 * d1.doubleValue() == d2.doubleValue() 177 * </pre> 178 * <p> 179 * also has the value {@code true}. However, there are two exceptions: 180 * </p> 181 * <ul> 182 * <li>If {@code d1} and {@code d2} both represent {@code Double.NaN}, then the {@code equals} method returns {@code true}, even though 183 * {@code Double.NaN == Double.NaN} has the value {@code false}. 184 * <li>If {@code d1} represents {@code +0.0} while {@code d2} represents {@code -0.0}, or vice versa, the {@code equal} test has the value {@code false}, 185 * even though {@code +0.0 == -0.0} has the value {@code true}. This allows hashtables to operate properly. 186 * </ul> 187 * 188 * @param obj the object to compare with, null returns false. 189 * @return {@code true} if the objects are the same; {@code false} otherwise. 190 */ 191 @Override 192 public boolean equals(final Object obj) { 193 return obj instanceof MutableDouble 194 && Double.doubleToLongBits(((MutableDouble) obj).value) == Double.doubleToLongBits(value); 195 } 196 197 /** 198 * Returns the value of this MutableDouble as a float. 199 * 200 * @return the numeric value represented by this object after conversion to type float. 201 */ 202 @Override 203 public float floatValue() { 204 return (float) value; 205 } 206 207 /** 208 * Increments this instance's value by {@code operand}; this method returns the value associated with the instance 209 * immediately prior to the addition operation. This method is not thread safe. 210 * 211 * @param operand the quantity to add, not null 212 * @return the value associated with this instance immediately before the operand was added 213 * @since 3.5 214 */ 215 public double getAndAdd(final double operand) { 216 final double last = value; 217 this.value += operand; 218 return last; 219 } 220 221 /** 222 * Increments this instance's value by {@code operand}; this method returns the value associated with the instance 223 * immediately prior to the addition operation. This method is not thread safe. 224 * 225 * @param operand the quantity to add, not null 226 * @throws NullPointerException if {@code operand} is null 227 * @return the value associated with this instance immediately before the operand was added 228 * @since 3.5 229 */ 230 public double getAndAdd(final Number operand) { 231 final double last = value; 232 this.value += operand.doubleValue(); 233 return last; 234 } 235 236 /** 237 * Decrements this instance's value by 1; this method returns the value associated with the instance 238 * immediately prior to the decrement operation. This method is not thread safe. 239 * 240 * @return the value associated with the instance before it was decremented 241 * @since 3.5 242 */ 243 public double getAndDecrement() { 244 final double last = value; 245 value--; 246 return last; 247 } 248 249 /** 250 * Increments this instance's value by 1; this method returns the value associated with the instance 251 * immediately prior to the increment operation. This method is not thread safe. 252 * 253 * @return the value associated with the instance before it was incremented 254 * @since 3.5 255 */ 256 public double getAndIncrement() { 257 final double last = value; 258 value++; 259 return last; 260 } 261 262 /** 263 * Gets the value as a Double instance. 264 * 265 * @return the value as a Double, never null. 266 * @deprecated Use {@link #get()}. 267 */ 268 @Deprecated 269 @Override 270 public Double getValue() { 271 return Double.valueOf(this.value); 272 } 273 274 /** 275 * Returns a suitable hash code for this mutable. 276 * 277 * @return a suitable hash code 278 */ 279 @Override 280 public int hashCode() { 281 final long bits = Double.doubleToLongBits(value); 282 return (int) (bits ^ bits >>> 32); 283 } 284 285 /** 286 * Increments the value. 287 * 288 * @since 2.2 289 */ 290 public void increment() { 291 value++; 292 } 293 294 /** 295 * Increments this instance's value by 1; this method returns the value associated with the instance 296 * immediately after the increment operation. This method is not thread safe. 297 * 298 * @return the value associated with the instance after it is incremented 299 * @since 3.5 300 */ 301 public double incrementAndGet() { 302 value++; 303 return value; 304 } 305 306 // shortValue and byteValue rely on Number implementation 307 /** 308 * Returns the value of this MutableDouble as an int. 309 * 310 * @return the numeric value represented by this object after conversion to type int. 311 */ 312 @Override 313 public int intValue() { 314 return (int) value; 315 } 316 317 /** 318 * Checks whether the double value is infinite. 319 * 320 * @return true if infinite 321 */ 322 public boolean isInfinite() { 323 return Double.isInfinite(value); 324 } 325 326 /** 327 * Checks whether the double value is the special NaN value. 328 * 329 * @return true if NaN 330 */ 331 public boolean isNaN() { 332 return Double.isNaN(value); 333 } 334 335 /** 336 * Returns the value of this MutableDouble as a long. 337 * 338 * @return the numeric value represented by this object after conversion to type long. 339 */ 340 @Override 341 public long longValue() { 342 return (long) value; 343 } 344 345 /** 346 * Sets the value. 347 * 348 * @param value the value to set 349 */ 350 public void setValue(final double value) { 351 this.value = value; 352 } 353 354 /** 355 * Sets the value from any Number instance. 356 * 357 * @param value the value to set, not null 358 * @throws NullPointerException if the object is null 359 */ 360 @Override 361 public void setValue(final Number value) { 362 this.value = value.doubleValue(); 363 } 364 365 /** 366 * Subtracts a value from the value of this instance. 367 * 368 * @param operand the value to subtract, not null 369 * @since 2.2 370 */ 371 public void subtract(final double operand) { 372 this.value -= operand; 373 } 374 375 /** 376 * Subtracts a value from the value of this instance. 377 * 378 * @param operand the value to subtract, not null 379 * @throws NullPointerException if the object is null 380 * @since 2.2 381 */ 382 public void subtract(final Number operand) { 383 this.value -= operand.doubleValue(); 384 } 385 386 /** 387 * Gets this mutable as an instance of Double. 388 * 389 * @return a Double instance containing the value from this mutable, never null 390 */ 391 public Double toDouble() { 392 return Double.valueOf(doubleValue()); 393 } 394 395 /** 396 * Returns the String value of this mutable. 397 * 398 * @return the mutable value as a string 399 */ 400 @Override 401 public String toString() { 402 return String.valueOf(value); 403 } 404 405}