MutableDouble.java

  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.  * A mutable {@code double} wrapper.
  20.  * <p>
  21.  * Note that as MutableDouble does not extend Double, it is not treated by String.format as a Double parameter.
  22.  * </p>
  23.  *
  24.  * @see Double
  25.  * @since 2.1
  26.  */
  27. public class MutableDouble extends Number implements Comparable<MutableDouble>, Mutable<Number> {

  28.     /**
  29.      * Required for serialization support.
  30.      *
  31.      * @see java.io.Serializable
  32.      */
  33.     private static final long serialVersionUID = 1587163916L;

  34.     /** The mutable value. */
  35.     private double value;

  36.     /**
  37.      * Constructs a new MutableDouble with the default value of zero.
  38.      */
  39.     public MutableDouble() {
  40.     }

  41.     /**
  42.      * Constructs a new MutableDouble with the specified value.
  43.      *
  44.      * @param value  the initial value to store
  45.      */
  46.     public MutableDouble(final double value) {
  47.         this.value = value;
  48.     }

  49.     /**
  50.      * Constructs a new MutableDouble with the specified value.
  51.      *
  52.      * @param value  the initial value to store, not null
  53.      * @throws NullPointerException if the object is null
  54.      */
  55.     public MutableDouble(final Number value) {
  56.         this.value = value.doubleValue();
  57.     }

  58.     /**
  59.      * Constructs a new MutableDouble parsing the given string.
  60.      *
  61.      * @param value  the string to parse, not null
  62.      * @throws NumberFormatException if the string cannot be parsed into a double
  63.      * @since 2.5
  64.      */
  65.     public MutableDouble(final String value) {
  66.         this.value = Double.parseDouble(value);
  67.     }

  68.     /**
  69.      * Adds a value to the value of this instance.
  70.      *
  71.      * @param operand  the value to add
  72.      * @since 2.2
  73.      */
  74.     public void add(final double operand) {
  75.         this.value += operand;
  76.     }

  77.     /**
  78.      * Adds a value to the value of this instance.
  79.      *
  80.      * @param operand  the value to add, not null
  81.      * @throws NullPointerException if the object is null
  82.      * @since 2.2
  83.      */
  84.     public void add(final Number operand) {
  85.         this.value += operand.doubleValue();
  86.     }

  87.     /**
  88.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  89.      * immediately after the addition operation. This method is not thread safe.
  90.      *
  91.      * @param operand the quantity to add, not null
  92.      * @return the value associated with this instance after adding the operand
  93.      * @since 3.5
  94.      */
  95.     public double addAndGet(final double operand) {
  96.         this.value += operand;
  97.         return value;
  98.     }

  99.     /**
  100.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  101.      * immediately after the addition operation. This method is not thread safe.
  102.      *
  103.      * @param operand the quantity to add, not null
  104.      * @throws NullPointerException if {@code operand} is null
  105.      * @return the value associated with this instance after adding the operand
  106.      * @since 3.5
  107.      */
  108.     public double addAndGet(final Number operand) {
  109.         this.value += operand.doubleValue();
  110.         return value;
  111.     }

  112.     /**
  113.      * Compares this mutable to another in ascending order.
  114.      *
  115.      * @param other  the other mutable to compare to, not null
  116.      * @return negative if this is less, zero if equal, positive if greater
  117.      */
  118.     @Override
  119.     public int compareTo(final MutableDouble other) {
  120.         return Double.compare(this.value, other.value);
  121.     }

  122.     /**
  123.      * Decrements the value.
  124.      *
  125.      * @since 2.2
  126.      */
  127.     public void decrement() {
  128.         value--;
  129.     }

  130.     /**
  131.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  132.      * immediately after the decrement operation. This method is not thread safe.
  133.      *
  134.      * @return the value associated with the instance after it is decremented
  135.      * @since 3.5
  136.      */
  137.     public double decrementAndGet() {
  138.         value--;
  139.         return value;
  140.     }

  141.     /**
  142.      * Returns the value of this MutableDouble as a double.
  143.      *
  144.      * @return the numeric value represented by this object after conversion to type double.
  145.      */
  146.     @Override
  147.     public double doubleValue() {
  148.         return value;
  149.     }

  150.     /**
  151.      * Compares this object against the specified object. The result is {@code true} if and only if the argument
  152.      * is not {@code null} and is a {@link Double} object that represents a double that has the identical
  153.      * bit pattern to the bit pattern of the double represented by this object. For this purpose, two
  154.      * {@code double} values are considered to be the same if and only if the method
  155.      * {@link Double#doubleToLongBits(double)}returns the same long value when applied to each.
  156.      * <p>
  157.      * Note that in most cases, for two instances of class {@link Double},{@code d1} and {@code d2},
  158.      * the value of {@code d1.equals(d2)} is {@code true} if and only if <blockquote>
  159.      *
  160.      * <pre>
  161.      *   d1.doubleValue()&nbsp;== d2.doubleValue()
  162.      * </pre>
  163.      *
  164.      * </blockquote>
  165.      * <p>
  166.      * also has the value {@code true}. However, there are two exceptions:
  167.      * <ul>
  168.      * <li>If {@code d1} and {@code d2} both represent {@code Double.NaN}, then the
  169.      * {@code equals} method returns {@code true}, even though {@code Double.NaN == Double.NaN} has
  170.      * the value {@code false}.
  171.      * <li>If {@code d1} represents {@code +0.0} while {@code d2} represents {@code -0.0},
  172.      * or vice versa, the {@code equal} test has the value {@code false}, even though
  173.      * {@code +0.0 == -0.0} has the value {@code true}. This allows hashtables to operate properly.
  174.      * </ul>
  175.      *
  176.      * @param obj  the object to compare with, null returns false
  177.      * @return {@code true} if the objects are the same; {@code false} otherwise.
  178.      */
  179.     @Override
  180.     public boolean equals(final Object obj) {
  181.         return obj instanceof MutableDouble
  182.             && Double.doubleToLongBits(((MutableDouble) obj).value) == Double.doubleToLongBits(value);
  183.     }

  184.     /**
  185.      * Returns the value of this MutableDouble as a float.
  186.      *
  187.      * @return the numeric value represented by this object after conversion to type float.
  188.      */
  189.     @Override
  190.     public float floatValue() {
  191.         return (float) value;
  192.     }

  193.     /**
  194.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  195.      * immediately prior to the addition operation. This method is not thread safe.
  196.      *
  197.      * @param operand the quantity to add, not null
  198.      * @return the value associated with this instance immediately before the operand was added
  199.      * @since 3.5
  200.      */
  201.     public double getAndAdd(final double operand) {
  202.         final double last = value;
  203.         this.value += operand;
  204.         return last;
  205.     }

  206.     /**
  207.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  208.      * immediately prior to the addition operation. This method is not thread safe.
  209.      *
  210.      * @param operand the quantity to add, not null
  211.      * @throws NullPointerException if {@code operand} is 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 Number operand) {
  216.         final double last = value;
  217.         this.value += operand.doubleValue();
  218.         return last;
  219.     }

  220.     /**
  221.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  222.      * immediately prior to the decrement operation. This method is not thread safe.
  223.      *
  224.      * @return the value associated with the instance before it was decremented
  225.      * @since 3.5
  226.      */
  227.     public double getAndDecrement() {
  228.         final double last = value;
  229.         value--;
  230.         return last;
  231.     }

  232.     /**
  233.      * Increments this instance's value by 1; this method returns the value associated with the instance
  234.      * immediately prior to the increment operation. This method is not thread safe.
  235.      *
  236.      * @return the value associated with the instance before it was incremented
  237.      * @since 3.5
  238.      */
  239.     public double getAndIncrement() {
  240.         final double last = value;
  241.         value++;
  242.         return last;
  243.     }

  244.     /**
  245.      * Gets the value as a Double instance.
  246.      *
  247.      * @return the value as a Double, never null
  248.      */
  249.     @Override
  250.     public Double getValue() {
  251.         return Double.valueOf(this.value);
  252.     }

  253.     /**
  254.      * Returns a suitable hash code for this mutable.
  255.      *
  256.      * @return a suitable hash code
  257.      */
  258.     @Override
  259.     public int hashCode() {
  260.         final long bits = Double.doubleToLongBits(value);
  261.         return (int) (bits ^ bits >>> 32);
  262.     }

  263.     /**
  264.      * Increments the value.
  265.      *
  266.      * @since 2.2
  267.      */
  268.     public void increment() {
  269.         value++;
  270.     }

  271.     /**
  272.      * Increments this instance's value by 1; this method returns the value associated with the instance
  273.      * immediately after the increment operation. This method is not thread safe.
  274.      *
  275.      * @return the value associated with the instance after it is incremented
  276.      * @since 3.5
  277.      */
  278.     public double incrementAndGet() {
  279.         value++;
  280.         return value;
  281.     }

  282.     // shortValue and byteValue rely on Number implementation
  283.     /**
  284.      * Returns the value of this MutableDouble as an int.
  285.      *
  286.      * @return the numeric value represented by this object after conversion to type int.
  287.      */
  288.     @Override
  289.     public int intValue() {
  290.         return (int) value;
  291.     }

  292.     /**
  293.      * Checks whether the double value is infinite.
  294.      *
  295.      * @return true if infinite
  296.      */
  297.     public boolean isInfinite() {
  298.         return Double.isInfinite(value);
  299.     }

  300.     /**
  301.      * Checks whether the double value is the special NaN value.
  302.      *
  303.      * @return true if NaN
  304.      */
  305.     public boolean isNaN() {
  306.         return Double.isNaN(value);
  307.     }

  308.     /**
  309.      * Returns the value of this MutableDouble as a long.
  310.      *
  311.      * @return the numeric value represented by this object after conversion to type long.
  312.      */
  313.     @Override
  314.     public long longValue() {
  315.         return (long) value;
  316.     }

  317.     /**
  318.      * Sets the value.
  319.      *
  320.      * @param value  the value to set
  321.      */
  322.     public void setValue(final double value) {
  323.         this.value = value;
  324.     }

  325.     /**
  326.      * Sets the value from any Number instance.
  327.      *
  328.      * @param value  the value to set, not null
  329.      * @throws NullPointerException if the object is null
  330.      */
  331.     @Override
  332.     public void setValue(final Number value) {
  333.         this.value = value.doubleValue();
  334.     }

  335.     /**
  336.      * Subtracts a value from the value of this instance.
  337.      *
  338.      * @param operand  the value to subtract, not null
  339.      * @since 2.2
  340.      */
  341.     public void subtract(final double operand) {
  342.         this.value -= operand;
  343.     }

  344.     /**
  345.      * Subtracts a value from the value of this instance.
  346.      *
  347.      * @param operand  the value to subtract, not null
  348.      * @throws NullPointerException if the object is null
  349.      * @since 2.2
  350.      */
  351.     public void subtract(final Number operand) {
  352.         this.value -= operand.doubleValue();
  353.     }

  354.     /**
  355.      * Gets this mutable as an instance of Double.
  356.      *
  357.      * @return a Double instance containing the value from this mutable, never null
  358.      */
  359.     public Double toDouble() {
  360.         return Double.valueOf(doubleValue());
  361.     }

  362.     /**
  363.      * Returns the String value of this mutable.
  364.      *
  365.      * @return the mutable value as a string
  366.      */
  367.     @Override
  368.     public String toString() {
  369.         return String.valueOf(value);
  370.     }

  371. }