KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > mutable > MutableInteger


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

3 package jodd.mutable;
4
5 /**
6  * A mutable <code>int</code> wrapper.
7  */

8 public final class MutableInteger extends Number JavaDoc implements Comparable JavaDoc {
9
10
11     public MutableInteger() {
12         super();
13     }
14
15     public MutableInteger(int value) {
16         super();
17         this.value = value;
18     }
19
20     public MutableInteger(String JavaDoc value) {
21         this(Integer.parseInt(value));
22     }
23
24     // ---------------------------------------------------------------- value
25

26     /**
27      * The mutable value.
28      */

29     public int value;
30
31     /**
32      * Returns mutable value.
33      */

34     public int getValue() {
35         return value;
36     }
37
38     /**
39      * Sets mutable value.
40      */

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

48     public void setValue(Number JavaDoc value) {
49         this.value = value.intValue();
50     }
51
52     // ---------------------------------------------------------------- object
53

54     /**
55      * Stringify the value.
56      */

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

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

75     public boolean equals(Object JavaDoc obj) {
76         if (obj != null) {
77             if (obj instanceof Integer JavaDoc) {
78                 return value == ((Integer JavaDoc) obj).intValue();
79             }
80             if (obj instanceof MutableInteger) {
81                 return value == ((MutableInteger) obj).value;
82             }
83         }
84         return false;
85     }
86
87     // ---------------------------------------------------------------- number
88

89     /**
90      * Returns the value as a int.
91      */

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

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

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

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

119     /**
120      * Comares value of two same instances.
121      */

122     public int compareTo(Object JavaDoc obj) {
123         MutableInteger other = (MutableInteger) obj;
124         return value < other.value ? -1 : (value == other.value ? 0 : 1);
125     }
126 }
Popular Tags