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     */
017    package org.apache.commons.lang3.mutable;
018    
019    /**
020     * A mutable <code>double</code> wrapper.
021     * 
022     * @see Double
023     * @since 2.1
024     * @version $Id: MutableDouble.java 1096472 2011-04-25 13:28:06Z mbenson $
025     */
026    public class MutableDouble extends Number implements Comparable<MutableDouble>, Mutable<Number> {
027    
028        /**
029         * Required for serialization support.
030         * 
031         * @see java.io.Serializable
032         */
033        private static final long serialVersionUID = 1587163916L;
034    
035        /** The mutable value. */
036        private double value;
037    
038        /**
039         * Constructs a new MutableDouble with the default value of zero.
040         */
041        public MutableDouble() {
042            super();
043        }
044    
045        /**
046         * Constructs a new MutableDouble with the specified value.
047         * 
048         * @param value  the initial value to store
049         */
050        public MutableDouble(double value) {
051            super();
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(Number value) {
062            super();
063            this.value = value.doubleValue();
064        }
065    
066        /**
067         * Constructs a new MutableDouble parsing the given string.
068         * 
069         * @param value  the string to parse, not null
070         * @throws NumberFormatException if the string cannot be parsed into a double
071         * @since 2.5
072         */
073        public MutableDouble(String value) throws NumberFormatException {
074            super();
075            this.value = Double.parseDouble(value);
076        }
077    
078        //-----------------------------------------------------------------------
079        /**
080         * Gets the value as a Double instance.
081         * 
082         * @return the value as a Double, never null
083         */
084        public Double getValue() {
085            return new Double(this.value);
086        }
087    
088        /**
089         * Sets the value.
090         * 
091         * @param value  the value to set
092         */
093        public void setValue(double value) {
094            this.value = value;
095        }
096    
097        /**
098         * Sets the value from any Number instance.
099         * 
100         * @param value  the value to set, not null
101         * @throws NullPointerException if the object is null
102         */
103        public void setValue(Number value) {
104            this.value = value.doubleValue();
105        }
106    
107        //-----------------------------------------------------------------------
108        /**
109         * Checks whether the double value is the special NaN value.
110         * 
111         * @return true if NaN
112         */
113        public boolean isNaN() {
114            return Double.isNaN(value);
115        }
116    
117        /**
118         * Checks whether the double value is infinite.
119         * 
120         * @return true if infinite
121         */
122        public boolean isInfinite() {
123            return Double.isInfinite(value);
124        }
125    
126        //-----------------------------------------------------------------------
127        /**
128         * Increments the value.
129         *
130         * @since Commons Lang 2.2
131         */
132        public void increment() {
133            value++;
134        }
135    
136        /**
137         * Decrements the value.
138         *
139         * @since Commons Lang 2.2
140         */
141        public void decrement() {
142            value--;
143        }
144    
145        //-----------------------------------------------------------------------
146        /**
147         * Adds a value to the value of this instance.
148         * 
149         * @param operand  the value to add
150         * @since Commons Lang 2.2
151         */
152        public void add(double operand) {
153            this.value += operand;
154        }
155    
156        /**
157         * Adds a value to the value of this instance.
158         * 
159         * @param operand  the value to add, not null
160         * @throws NullPointerException if the object is null
161         * @since Commons Lang 2.2
162         */
163        public void add(Number operand) {
164            this.value += operand.doubleValue();
165        }
166    
167        /**
168         * Subtracts a value from the value of this instance.
169         * 
170         * @param operand  the value to subtract, not null
171         * @since Commons Lang 2.2
172         */
173        public void subtract(double operand) {
174            this.value -= operand;
175        }
176    
177        /**
178         * Subtracts a value from the value of this instance.
179         * 
180         * @param operand  the value to subtract, not null
181         * @throws NullPointerException if the object is null
182         * @since Commons Lang 2.2
183         */
184        public void subtract(Number operand) {
185            this.value -= operand.doubleValue();
186        }
187    
188        //-----------------------------------------------------------------------
189        // shortValue and byteValue rely on Number implementation
190        /**
191         * Returns the value of this MutableDouble as an int.
192         *
193         * @return the numeric value represented by this object after conversion to type int.
194         */
195        @Override
196        public int intValue() {
197            return (int) value;
198        }
199    
200        /**
201         * Returns the value of this MutableDouble as a long.
202         *
203         * @return the numeric value represented by this object after conversion to type long.
204         */
205        @Override
206        public long longValue() {
207            return (long) value;
208        }
209    
210        /**
211         * Returns the value of this MutableDouble as a float.
212         *
213         * @return the numeric value represented by this object after conversion to type float.
214         */
215        @Override
216        public float floatValue() {
217            return (float) value;
218        }
219    
220        /**
221         * Returns the value of this MutableDouble as a double.
222         *
223         * @return the numeric value represented by this object after conversion to type double.
224         */
225        @Override
226        public double doubleValue() {
227            return value;
228        }
229    
230        //-----------------------------------------------------------------------
231        /**
232         * Gets this mutable as an instance of Double.
233         *
234         * @return a Double instance containing the value from this mutable, never null
235         */
236        public Double toDouble() {
237            return Double.valueOf(doubleValue());
238        }
239    
240        //-----------------------------------------------------------------------
241        /**
242         * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
243         * is not <code>null</code> and is a <code>Double</code> object that represents a double that has the identical
244         * bit pattern to the bit pattern of the double represented by this object. For this purpose, two
245         * <code>double</code> values are considered to be the same if and only if the method
246         * {@link Double#doubleToLongBits(double)}returns the same long value when applied to each.
247         * <p>
248         * Note that in most cases, for two instances of class <code>Double</code>,<code>d1</code> and <code>d2</code>,
249         * the value of <code>d1.equals(d2)</code> is <code>true</code> if and only if <blockquote>
250         * 
251         * <pre>
252         *   d1.doubleValue()&nbsp;== d2.doubleValue()
253         * </pre>
254         * 
255         * </blockquote>
256         * <p>
257         * also has the value <code>true</code>. However, there are two exceptions:
258         * <ul>
259         * <li>If <code>d1</code> and <code>d2</code> both represent <code>Double.NaN</code>, then the
260         * <code>equals</code> method returns <code>true</code>, even though <code>Double.NaN==Double.NaN</code> has
261         * the value <code>false</code>.
262         * <li>If <code>d1</code> represents <code>+0.0</code> while <code>d2</code> represents <code>-0.0</code>,
263         * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
264         * <code>+0.0==-0.0</code> has the value <code>true</code>. This allows hashtables to operate properly.
265         * </ul>
266         * 
267         * @param obj  the object to compare with, null returns false
268         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
269         */
270        @Override
271        public boolean equals(Object obj) {
272            return (obj instanceof MutableDouble)
273                && (Double.doubleToLongBits(((MutableDouble) obj).value) == Double.doubleToLongBits(value));
274        }
275    
276        /**
277         * Returns a suitable hash code for this mutable.
278         * 
279         * @return a suitable hash code
280         */
281        @Override
282        public int hashCode() {
283            long bits = Double.doubleToLongBits(value);
284            return (int) (bits ^ (bits >>> 32));
285        }
286    
287        //-----------------------------------------------------------------------
288        /**
289         * Compares this mutable to another in ascending order.
290         * 
291         * @param other  the other mutable to compare to, not null
292         * @return negative if this is less, zero if equal, positive if greater
293         */
294        public int compareTo(MutableDouble other) {
295            double anotherVal = other.value;
296            return Double.compare(value, anotherVal);
297        }
298    
299        //-----------------------------------------------------------------------
300        /**
301         * Returns the String value of this mutable.
302         * 
303         * @return the mutable value as a string
304         */
305        @Override
306        public String toString() {
307            return String.valueOf(value);
308        }
309    
310    }