KickJava   Java API By Example, From Geeks To Geeks.

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


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>int</code> wrapper.
21  *
22  * @see Integer
23  * @since 2.1
24  * @version $Id: MutableInt.java 161243 2005-04-14 04:30:28Z ggregory $
25  */

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

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

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

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

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

81     public void setValue(int 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).intValue());
97     }
98
99     //-----------------------------------------------------------------------
100
// shortValue and bytValue rely on Number implementation
101
/**
102      * Returns the value of this MutableInt 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 value;
108     }
109
110     /**
111      * Returns the value of this MutableInt 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 MutableInt 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 MutableInt 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 to the specified object. The result is <code>true</code> if and only if the argument is
140      * not <code>null</code> and is an <code>MutableInt</code> object that contains the same <code>int</code> value
141      * 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 MutableInt) {
149             return value == ((MutableInt) obj).intValue();
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 value;
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 MutableInt
170      */

171     public int compareTo(Object JavaDoc obj) {
172         MutableInt other = (MutableInt) obj;
173         int 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