KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > mutable > MutableLong


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.lang.mutable;
18
19 /**
20  * A mutable <code>long</code> wrapper.
21  *
22  * @see Long
23  * @since 2.1
24  * @version $Id: MutableLong.java 161243 2005-04-14 04:30:28Z ggregory $
25  */

26 public class MutableLong extends Number JavaDoc implements Comparable JavaDoc, Mutable {
27
28     /** Serialization lock. */
29     private static final long serialVersionUID = 62986528375L;
30
31     /** The mutable value. */
32     private long value;
33
34     /**
35      * Constructs a new MutableLong with the default value of zero.
36      */

37     public MutableLong() {
38         super();
39     }
40
41     /**
42      * Constructs a new MutableLong with the specified value.
43      *
44      * @param value
45      * a value.
46      */

47     public MutableLong(long value) {
48         super();
49         this.value = value;
50     }
51
52     /**
53      * Constructs a new MutableLong with the specified value.
54      *
55      * @param value
56      * a value.
57      * @throws NullPointerException
58      * if the object is null
59      */

60     public MutableLong(Number JavaDoc value) {
61         super();
62         this.value = value.longValue();
63     }
64
65     //-----------------------------------------------------------------------
66
/**
67      * Gets the value as a Long instance.
68      *
69      * @return the value as a Long
70      */

71     public Object JavaDoc getValue() {
72         return new Long JavaDoc(this.value);
73     }
74
75     /**
76      * Sets the value.
77      *
78      * @param value
79      * the value to set
80      */

81     public void setValue(long value) {
82         this.value = value;
83     }
84
85     /**
86      * Sets the value from any Number instance.
87      *
88      * @param value
89      * the value to set
90      * @throws NullPointerException
91      * if the object is null
92      * @throws ClassCastException
93      * if the type is not a {@link Number}
94      */

95     public void setValue(Object JavaDoc value) {
96         setValue(((Number JavaDoc) value).longValue());
97     }
98
99     //-----------------------------------------------------------------------
100
// shortValue and bytValue rely on Number implementation
101
/**
102      * Returns the value of this MutableLong as a int.
103      *
104      * @return the numeric value represented by this object after conversion to type int.
105      */

106     public int intValue() {
107         return (int) value;
108     }
109
110     /**
111      * Returns the value of this MutableLong as a long.
112      *
113      * @return the numeric value represented by this object after conversion to type long.
114      */

115     public long longValue() {
116         return value;
117     }
118
119     /**
120      * Returns the value of this MutableLong as a float.
121      *
122      * @return the numeric value represented by this object after conversion to type float.
123      */

124     public float floatValue() {
125         return value;
126     }
127
128     /**
129      * Returns the value of this MutableLong as a double.
130      *
131      * @return the numeric value represented by this object after conversion to type double.
132      */

133     public double doubleValue() {
134         return value;
135     }
136
137     //-----------------------------------------------------------------------
138
/**
139      * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
140      * is not <code>null</code> and is a <code>MutableLong</code> object that contains the same <code>long</code>
141      * value as this object.
142      *
143      * @param obj
144      * the object to compare with.
145      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
146      */

147     public boolean equals(Object JavaDoc obj) {
148         if (obj instanceof MutableLong) {
149             return value == ((MutableLong) obj).longValue();
150         }
151         return false;
152     }
153
154     /**
155      * Returns a suitable hashcode for this mutable.
156      *
157      * @return a suitable hashcode
158      */

159     public int hashCode() {
160         return (int) (value ^ (value >>> 32));
161     }
162
163     /**
164      * Compares this mutable to another in ascending order.
165      *
166      * @param obj
167      * the mutable to compare to
168      * @return negative if this is less, zero if equal, positive if greater
169      * @throws ClassCastException if the argument is not a MutableLong
170      */

171     public int compareTo(Object JavaDoc obj) {
172         MutableLong other = (MutableLong) obj;
173         long anotherVal = other.value;
174         return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
175     }
176
177     /**
178      * Returns the String value of this mutable.
179      *
180      * @return the mutable value as a string
181      */

182     public String JavaDoc toString() {
183         return String.valueOf(value);
184     }
185
186 }
187
Popular Tags