KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > mutable > MutableLong


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

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

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

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

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

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

41     public void setValue(long 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.longValue();
50     }
51
52     // ---------------------------------------------------------------- object
53

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

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

64     public int hashCode() {
65         return (int) (value ^ (value >>> 32));
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 Long JavaDoc) {
78                 return value == ((Long JavaDoc) obj).longValue();
79             }
80             if (obj instanceof MutableLong) {
81                 return value == ((MutableLong) 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 (int) 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         MutableLong other = (MutableLong) obj;
124         return value < other.value ? -1 : (value == other.value ? 0 : 1);
125     }
126 }
Popular Tags