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 *      http://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</code> wrapper.
021 * <p>
022 * Note that as MutableDouble does not extend Double, it is not treated by String.format as a Double parameter. 
023 * 
024 * @see Double
025 * @since 2.1
026 * @version $Id: MutableDouble.java 1669791 2015-03-28 15:22:59Z britter $
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        super();
045    }
046
047    /**
048     * Constructs a new MutableDouble with the specified value.
049     * 
050     * @param value  the initial value to store
051     */
052    public MutableDouble(final double value) {
053        super();
054        this.value = value;
055    }
056
057    /**
058     * Constructs a new MutableDouble with the specified value.
059     * 
060     * @param value  the initial value to store, not null
061     * @throws NullPointerException if the object is null
062     */
063    public MutableDouble(final Number value) {
064        super();
065        this.value = value.doubleValue();
066    }
067
068    /**
069     * Constructs a new MutableDouble parsing the given string.
070     * 
071     * @param value  the string to parse, not null
072     * @throws NumberFormatException if the string cannot be parsed into a double
073     * @since 2.5
074     */
075    public MutableDouble(final String value) throws NumberFormatException {
076        super();
077        this.value = Double.parseDouble(value);
078    }
079
080    //-----------------------------------------------------------------------
081    /**
082     * Gets the value as a Double instance.
083     * 
084     * @return the value as a Double, never null
085     */
086    @Override
087    public Double getValue() {
088        return Double.valueOf(this.value);
089    }
090
091    /**
092     * Sets the value.
093     * 
094     * @param value  the value to set
095     */
096    public void setValue(final double value) {
097        this.value = value;
098    }
099
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        this.value = value.doubleValue();
109    }
110
111    //-----------------------------------------------------------------------
112    /**
113     * Checks whether the double value is the special NaN value.
114     * 
115     * @return true if NaN
116     */
117    public boolean isNaN() {
118        return Double.isNaN(value);
119    }
120
121    /**
122     * Checks whether the double value is infinite.
123     * 
124     * @return true if infinite
125     */
126    public boolean isInfinite() {
127        return Double.isInfinite(value);
128    }
129
130    //-----------------------------------------------------------------------
131    /**
132     * Increments the value.
133     *
134     * @since Commons Lang 2.2
135     */
136    public void increment() {
137        value++;
138    }
139
140    /**
141     * Decrements the value.
142     *
143     * @since Commons Lang 2.2
144     */
145    public void decrement() {
146        value--;
147    }
148
149    //-----------------------------------------------------------------------
150    /**
151     * Adds a value to the value of this instance.
152     * 
153     * @param operand  the value to add
154     * @since Commons Lang 2.2
155     */
156    public void add(final double operand) {
157        this.value += operand;
158    }
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        this.value += operand.doubleValue();
169    }
170
171    /**
172     * Subtracts a value from the value of this instance.
173     * 
174     * @param operand  the value to subtract, not null
175     * @since Commons Lang 2.2
176     */
177    public void subtract(final double operand) {
178        this.value -= operand;
179    }
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        this.value -= operand.doubleValue();
190    }
191
192    //-----------------------------------------------------------------------
193    // shortValue and byteValue rely on Number implementation
194    /**
195     * Returns the value of this MutableDouble 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        return (int) value;
202    }
203
204    /**
205     * Returns the value of this MutableDouble 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        return (long) value;
212    }
213
214    /**
215     * Returns the value of this MutableDouble 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        return (float) value;
222    }
223
224    /**
225     * Returns the value of this MutableDouble 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        return value;
232    }
233
234    //-----------------------------------------------------------------------
235    /**
236     * Gets this mutable as an instance of Double.
237     *
238     * @return a Double instance containing the value from this mutable, never null
239     */
240    public Double toDouble() {
241        return Double.valueOf(doubleValue());
242    }
243
244    //-----------------------------------------------------------------------
245    /**
246     * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
247     * is not <code>null</code> and is a <code>Double</code> object that represents a double that has the identical
248     * bit pattern to the bit pattern of the double represented by this object. For this purpose, two
249     * <code>double</code> values are considered to be the same if and only if the method
250     * {@link Double#doubleToLongBits(double)}returns the same long value when applied to each.
251     * <p>
252     * Note that in most cases, for two instances of class <code>Double</code>,<code>d1</code> and <code>d2</code>,
253     * the value of <code>d1.equals(d2)</code> is <code>true</code> if and only if <blockquote>
254     * 
255     * <pre>
256     *   d1.doubleValue()&nbsp;== d2.doubleValue()
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>d1</code> and <code>d2</code> both represent <code>Double.NaN</code>, then the
264     * <code>equals</code> method returns <code>true</code>, even though <code>Double.NaN==Double.NaN</code> has
265     * the value <code>false</code>.
266     * <li>If <code>d1</code> represents <code>+0.0</code> while <code>d2</code> represents <code>-0.0</code>,
267     * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
268     * <code>+0.0==-0.0</code> has the value <code>true</code>. This allows hashtables to operate properly.
269     * </ul>
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     */
274    @Override
275    public boolean equals(final Object obj) {
276        return obj instanceof MutableDouble
277            && Double.doubleToLongBits(((MutableDouble) obj).value) == Double.doubleToLongBits(value);
278    }
279
280    /**
281     * Returns a suitable hash code for this mutable.
282     * 
283     * @return a suitable hash code
284     */
285    @Override
286    public int hashCode() {
287        final long bits = Double.doubleToLongBits(value);
288        return (int) (bits ^ bits >>> 32);
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 MutableDouble other) {
300        return Double.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        return String.valueOf(value);
312    }
313
314}