KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > mutable > MutableDouble


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.mutable;
4
5 import jodd.util.MathUtil;
6
7 /**
8  * A mutable <code>double</code> wrapper.
9  */

10 public final class MutableDouble extends Number JavaDoc implements Comparable JavaDoc {
11
12
13     public MutableDouble() {
14         super();
15     }
16
17     public MutableDouble(double value) {
18         super();
19         this.value = value;
20     }
21
22     public MutableDouble(String JavaDoc value) {
23         this(Double.parseDouble(value));
24     }
25
26     // ---------------------------------------------------------------- value
27

28     /**
29      * The mutable value.
30      */

31     public double value;
32
33     /**
34      * Returns mutable value.
35      */

36     public double getValue() {
37         return value;
38     }
39
40     /**
41      * Sets mutable value.
42      */

43     public void setValue(double value) {
44         this.value = value;
45     }
46
47     /**
48      * Sets mutable value from a Number.
49      */

50     public void setValue(Number JavaDoc value) {
51         this.value = value.doubleValue();
52     }
53
54     // ---------------------------------------------------------------- object
55

56     /**
57      * Stringify the value.
58      */

59     public String JavaDoc toString() {
60         return Double.toString(value);
61     }
62
63     /**
64      * Returns a hashcode for this value.
65      */

66     public int hashCode() {
67         long bits = Double.doubleToLongBits(value);
68         return (int) (bits ^ (bits >>> 32));
69     }
70
71     /**
72      * Compares this object to the specified object.
73      *
74      * @param obj the object to compare with.
75      * @return <code>true</code> if the objects are the same;
76      * <code>false</code> otherwise.
77      */

78     public boolean equals(Object JavaDoc obj) {
79         if (obj != null) {
80             if (obj instanceof Double JavaDoc) {
81                 return Double.doubleToLongBits(value) == Double.doubleToLongBits(((Double JavaDoc) obj).doubleValue());
82             }
83             if (obj instanceof MutableDouble) {
84                 return Double.doubleToLongBits(value) == Double.doubleToLongBits(((MutableDouble) obj).value);
85             }
86         }
87         return false;
88     }
89
90     // ---------------------------------------------------------------- number
91

92     /**
93      * Returns the value as a int.
94      */

95     public int intValue() {
96         return (int) value;
97     }
98
99     /**
100      * Returns the value as a long.
101      */

102     public long longValue() {
103         return (long) value;
104     }
105
106     /**
107      * Returns the value as a float..
108      */

109     public float floatValue() {
110         return (float) value;
111     }
112
113     /**
114      * Returns the value as a double.
115      */

116     public double doubleValue() {
117         return value;
118     }
119
120     // ---------------------------------------------------------------- compare
121

122     /**
123      * Checks whether the value is the special NaN value.
124      */

125     public boolean isNaN() {
126         return Double.isNaN(value);
127     }
128
129     /**
130      * Checks whether the double value is infinite.
131      */

132     public boolean isInfinite() {
133         return Double.isInfinite(value);
134     }
135
136     /**
137      * Comares value of two same instances.
138      */

139     public int compareTo(Object JavaDoc obj) {
140         MutableDouble other = (MutableDouble) obj;
141         return MathUtil.compare(value, other.value);
142     }
143 }
Popular Tags