001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.mutable;
004:
005: import jodd.util.MathUtil;
006:
007: /**
008: * A mutable <code>double</code> wrapper.
009: */
010: public final class MutableDouble extends Number implements Comparable {
011:
012: public MutableDouble() {
013: super ();
014: }
015:
016: public MutableDouble(double value) {
017: super ();
018: this .value = value;
019: }
020:
021: public MutableDouble(String value) {
022: this (Double.parseDouble(value));
023: }
024:
025: // ---------------------------------------------------------------- value
026:
027: /**
028: * The mutable value.
029: */
030: public double value;
031:
032: /**
033: * Returns mutable value.
034: */
035: public double getValue() {
036: return value;
037: }
038:
039: /**
040: * Sets mutable value.
041: */
042: public void setValue(double value) {
043: this .value = value;
044: }
045:
046: /**
047: * Sets mutable value from a Number.
048: */
049: public void setValue(Number value) {
050: this .value = value.doubleValue();
051: }
052:
053: // ---------------------------------------------------------------- object
054:
055: /**
056: * Stringify the value.
057: */
058: @Override
059: public String toString() {
060: return Double.toString(value);
061: }
062:
063: /**
064: * Returns a hashcode for this value.
065: */
066: @Override
067: public int hashCode() {
068: long bits = Double.doubleToLongBits(value);
069: return (int) (bits ^ (bits >>> 32));
070: }
071:
072: /**
073: * Compares this object to the specified object.
074: *
075: * @param obj the object to compare with.
076: * @return <code>true</code> if the objects are the same;
077: * <code>false</code> otherwise.
078: */
079: @Override
080: public boolean equals(Object obj) {
081: if (obj != null) {
082: if (obj instanceof Double) {
083: return Double.doubleToLongBits(value) == Double
084: .doubleToLongBits(((Double) obj).doubleValue());
085: }
086: if (obj instanceof MutableDouble) {
087: return Double.doubleToLongBits(value) == Double
088: .doubleToLongBits(((MutableDouble) obj).value);
089: }
090: }
091: return false;
092: }
093:
094: // ---------------------------------------------------------------- number
095:
096: /**
097: * Returns the value as a int.
098: */
099: @Override
100: public int intValue() {
101: return (int) value;
102: }
103:
104: /**
105: * Returns the value as a long.
106: */
107: @Override
108: public long longValue() {
109: return (long) value;
110: }
111:
112: /**
113: * Returns the value as a float..
114: */
115: @Override
116: public float floatValue() {
117: return (float) value;
118: }
119:
120: /**
121: * Returns the value as a double.
122: */
123: @Override
124: public double doubleValue() {
125: return value;
126: }
127:
128: // ---------------------------------------------------------------- compare
129:
130: /**
131: * Checks whether the value is the special NaN value.
132: */
133: public boolean isNaN() {
134: return Double.isNaN(value);
135: }
136:
137: /**
138: * Checks whether the double value is infinite.
139: */
140: public boolean isInfinite() {
141: return Double.isInfinite(value);
142: }
143:
144: /**
145: * Comares value of two same instances.
146: */
147: public int compareTo(Object obj) {
148: MutableDouble other = (MutableDouble) obj;
149: return MathUtil.compare(value, other.value);
150: }
151: }
|