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