KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > mutable > MutableFloat


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>float</code> wrapper.
9  */

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

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

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

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

43     public void setValue(float 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.floatValue();
52     }
53
54     // ---------------------------------------------------------------- object
55

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

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

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

77     public boolean equals(Object JavaDoc obj) {
78         if (obj != null) {
79             if (obj instanceof Float JavaDoc) {
80                 return Float.floatToIntBits(value) == Float.floatToIntBits(((Float JavaDoc) obj).floatValue());
81             }
82             if (obj instanceof MutableFloat) {
83                 return Float.floatToIntBits(value) == Float.floatToIntBits(((MutableFloat) obj).value);
84             }
85         }
86         return false;
87     }
88
89     // ---------------------------------------------------------------- number
90

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

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

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

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

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

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

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

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

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