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.lang.mutable;
018:
019: import org.apache.commons.lang.math.NumberUtils;
020:
021: /**
022: * A mutable <code>double</code> wrapper.
023: *
024: * @see Double
025: * @since 2.1
026: * @version $Id: MutableDouble.java 437554 2006-08-28 06:21:41Z bayard $
027: */
028: public class MutableDouble extends Number implements Comparable,
029: Mutable {
030:
031: /**
032: * Required for serialization support.
033: *
034: * @see java.io.Serializable
035: */
036: private static final long serialVersionUID = 1587163916L;
037:
038: /** The mutable value. */
039: private double value;
040:
041: /**
042: * Constructs a new MutableDouble with the default value of zero.
043: */
044: public MutableDouble() {
045: super ();
046: }
047:
048: /**
049: * Constructs a new MutableDouble with the specified value.
050: *
051: * @param value
052: * a value.
053: */
054: public MutableDouble(double value) {
055: super ();
056: this .value = value;
057: }
058:
059: /**
060: * Constructs a new MutableDouble with the specified value.
061: *
062: * @param value
063: * a value.
064: * @throws NullPointerException
065: * if the object is null
066: */
067: public MutableDouble(Number value) {
068: super ();
069: this .value = value.doubleValue();
070: }
071:
072: //-----------------------------------------------------------------------
073: /**
074: * Gets the value as a Double instance.
075: *
076: * @return the value as a Double
077: */
078: public Object getValue() {
079: return new Double(this .value);
080: }
081:
082: /**
083: * Sets the value.
084: *
085: * @param value
086: * the value to set
087: */
088: public void setValue(double value) {
089: this .value = value;
090: }
091:
092: /**
093: * Sets the value from any Number instance.
094: *
095: * @param value
096: * the value to set
097: * @throws NullPointerException
098: * if the object is null
099: * @throws ClassCastException
100: * if the type is not a {@link Number}
101: */
102: public void setValue(Object value) {
103: setValue(((Number) value).doubleValue());
104: }
105:
106: //-----------------------------------------------------------------------
107: // shortValue and bytValue rely on Number implementation
108: /**
109: * Returns the value of this MutableDouble as a int.
110: *
111: * @return the numeric value represented by this object after conversion to type int.
112: */
113: public int intValue() {
114: return (int) value;
115: }
116:
117: /**
118: * Returns the value of this MutableDouble as a long.
119: *
120: * @return the numeric value represented by this object after conversion to type long.
121: */
122: public long longValue() {
123: return (long) value;
124: }
125:
126: /**
127: * Returns the value of this MutableDouble as a float.
128: *
129: * @return the numeric value represented by this object after conversion to type float.
130: */
131: public float floatValue() {
132: return (float) value;
133: }
134:
135: /**
136: * Returns the value of this MutableDouble as a double.
137: *
138: * @return the numeric value represented by this object after conversion to type double.
139: */
140: public double doubleValue() {
141: return value;
142: }
143:
144: /**
145: * Checks whether the double value is the special NaN value.
146: *
147: * @return true if NaN
148: */
149: public boolean isNaN() {
150: return Double.isNaN(value);
151: }
152:
153: /**
154: * Checks whether the double value is infinite.
155: *
156: * @return true if infinite
157: */
158: public boolean isInfinite() {
159: return Double.isInfinite(value);
160: }
161:
162: //-----------------------------------------------------------------------
163: /**
164: * Gets this mutable as an instance of Double.
165: *
166: * @return a Double instance containing the value from this mutable
167: */
168: public Double toDouble() {
169: return new Double(doubleValue());
170: }
171:
172: //-----------------------------------------------------------------------
173: /**
174: * Increments the value.
175: *
176: * @since Commons Lang 2.2
177: */
178: public void increment() {
179: value++;
180: }
181:
182: /**
183: * Decrements the value.
184: *
185: * @since Commons Lang 2.2
186: */
187: public void decrement() {
188: value--;
189: }
190:
191: //-----------------------------------------------------------------------
192: /**
193: * Adds a value.
194: *
195: * @param operand
196: * the value to add
197: *
198: * @since Commons Lang 2.2
199: */
200: public void add(double operand) {
201: this .value += operand;
202: }
203:
204: /**
205: * Adds a value.
206: *
207: * @param operand
208: * the value to add
209: * @throws NullPointerException
210: * if the object is null
211: *
212: * @since Commons Lang 2.2
213: */
214: public void add(Number operand) {
215: this .value += operand.doubleValue();
216: }
217:
218: /**
219: * Subtracts a value.
220: *
221: * @param operand
222: * the value to add
223: *
224: * @since Commons Lang 2.2
225: */
226: public void subtract(double operand) {
227: this .value -= operand;
228: }
229:
230: /**
231: * Subtracts a value.
232: *
233: * @param operand
234: * the value to add
235: * @throws NullPointerException
236: * if the object is null
237: *
238: * @since Commons Lang 2.2
239: */
240: public void subtract(Number operand) {
241: this .value -= operand.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() == 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
272: * the object to compare with.
273: * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
274: */
275: public boolean equals(Object obj) {
276: return (obj instanceof MutableDouble)
277: && (Double
278: .doubleToLongBits(((MutableDouble) obj).value) == Double
279: .doubleToLongBits(value));
280: }
281:
282: /**
283: * Returns a suitable hashcode for this mutable.
284: *
285: * @return a suitable hashcode
286: */
287: public int hashCode() {
288: long bits = Double.doubleToLongBits(value);
289: return (int) (bits ^ (bits >>> 32));
290: }
291:
292: /**
293: * Compares this mutable to another in ascending order.
294: *
295: * @param obj
296: * the mutable to compare to
297: * @return negative if this is less, zero if equal, positive if greater
298: * @throws ClassCastException if the argument is not a MutableDouble
299: */
300: public int compareTo(Object obj) {
301: MutableDouble other = (MutableDouble) obj;
302: double anotherVal = other.value;
303: return NumberUtils.compare(value, anotherVal);
304: }
305:
306: /**
307: * Returns the String value of this mutable.
308: *
309: * @return the mutable value as a string
310: */
311: public String toString() {
312: return String.valueOf(value);
313: }
314:
315: }
|