KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

81     public void setValue(short 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).shortValue());
97     }
98
99     //-----------------------------------------------------------------------
100
// bytValue relies on Number implementation
101
/**
102      * Returns the value of this MutableShort as a short.
103      *
104      * @return the numeric value represented by this object after conversion to type short.
105      */

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

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

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

133     public float floatValue() {
134         return value;
135     }
136
137     /**
138      * Returns the value of this MutableShort as a double.
139      *
140      * @return the numeric value represented by this object after conversion to type double.
141      */

142     public double doubleValue() {
143         return value;
144     }
145
146     //-----------------------------------------------------------------------
147
/**
148      * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
149      * is not <code>null</code> and is a <code>MutableShort</code> object that contains the same <code>short</code>
150      * value as this object.
151      *
152      * @param obj
153      * the object to compare with.
154      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
155      */

156     public boolean equals(Object JavaDoc obj) {
157         if (obj instanceof MutableShort) {
158             return value == ((MutableShort) obj).shortValue();
159         }
160         return false;
161     }
162
163     /**
164      * Returns a suitable hashcode for this mutable.
165      *
166      * @return a suitable hashcode
167      */

168     public int hashCode() {
169         return value;
170     }
171
172     /**
173      * Compares this mutable to another in ascending order.
174      *
175      * @param obj
176      * the mutable to compare to
177      * @return negative if this is less, zero if equal, positive if greater
178      * @throws ClassCastException if the argument is not a MutableShort
179      */

180     public int compareTo(Object JavaDoc obj) {
181         MutableShort other = (MutableShort) obj;
182         short anotherVal = other.value;
183         return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
184     }
185
186     /**
187      * Returns the String value of this mutable.
188      *
189      * @return the mutable value as a string
190      */

191     public String JavaDoc toString() {
192         return String.valueOf(value);
193     }
194
195 }
196
Popular Tags