001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang;
019:
020: /**
021: * <p>
022: * Short is the wrapper for the primitive type <code>short</code>.
023: * </p>
024: *
025: * @see java.lang.Number
026: * @since 1.1
027: */
028: public final class Short extends Number implements Comparable<Short> {
029:
030: private static final long serialVersionUID = 7515723908773894738L;
031:
032: /**
033: * The value which the receiver represents.
034: */
035: private final short value;
036:
037: /**
038: * <p>
039: * Constant for the maximum <code>short</code> value, 2<sup>15</sup>-1.
040: * </p>
041: */
042: public static final short MAX_VALUE = (short) 0x7FFF;
043:
044: /**
045: * <p>
046: * Constant for the minimum <code>short</code> value, -2<sup>15</sup>.
047: * </p>
048: */
049: public static final short MIN_VALUE = (short) 0x8000;
050:
051: /**
052: * <p>
053: * Constant for the number of bits to represent a <code>short</code> in
054: * two's compliment form.
055: * </p>
056: *
057: * @since 1.5
058: */
059: public static final int SIZE = 16;
060:
061: /**
062: * The java.lang.Class that represents this class.
063: */
064: @SuppressWarnings("unchecked")
065: public static final Class<Short> TYPE = (Class<Short>) new short[0]
066: .getClass().getComponentType();
067:
068: // Note: This can't be set to "short.class", since *that* is
069: // defined to be "java.lang.Short.TYPE";
070:
071: /**
072: * Constructs a new instance of this class given a string.
073: *
074: * @param string
075: * a string representation of a short quantity.
076: * @exception NumberFormatException
077: * if the argument could not be parsed as a short quantity.
078: */
079: public Short(String string) throws NumberFormatException {
080: this (parseShort(string));
081: }
082:
083: /**
084: * Constructs a new instance of the receiver which represents the short
085: * valued argument.
086: *
087: * @param value
088: * the short to store in the new instance.
089: */
090: public Short(short value) {
091: this .value = value;
092: }
093:
094: /**
095: * Answers the byte value which the receiver represents
096: *
097: * @return byte the value of the receiver.
098: */
099: @Override
100: public byte byteValue() {
101: return (byte) value;
102: }
103:
104: /**
105: * <p>
106: * Compares this <code>Short</code> to the <code>Short</code>
107: * passed. If this instance's value is equal to the value of the instance
108: * passed, then 0 is returned. If this instance's value is less than the
109: * value of the instance passed, then a negative value is returned. If this
110: * instance's value is greater than the value of the instance passed, then a
111: * positive value is returned.
112: * </p>
113: *
114: * @param object The instance to compare to.
115: * @throws NullPointerException if <code>object</code> is
116: * <code>null</code>.
117: * @since 1.2
118: */
119: public int compareTo(Short object) {
120: return value > object.value ? 1 : (value < object.value ? -1
121: : 0);
122: }
123:
124: /**
125: * Parses the string argument as if it was a short value and returns the
126: * result. Throws NumberFormatException if the string does not represent an
127: * int quantity. The string may be a hexadecimal ("0x..."), octal ("0..."),
128: * or decimal ("...") representation of a byte.
129: *
130: * @param string
131: * a string representation of a short quantity.
132: * @return Short the value represented by the argument
133: * @exception NumberFormatException
134: * if the argument could not be parsed as a short quantity.
135: */
136: public static Short decode(String string)
137: throws NumberFormatException {
138: int intValue = Integer.decode(string).intValue();
139: short result = (short) intValue;
140: if (result == intValue) {
141: return valueOf(result);
142: }
143: throw new NumberFormatException();
144: }
145:
146: /**
147: * Answers the double value which the receiver represents
148: *
149: * @return double the value of the receiver.
150: */
151: @Override
152: public double doubleValue() {
153: return value;
154: }
155:
156: /**
157: * Compares the argument to the receiver, and answers true if they represent
158: * the <em>same</em> object using a class specific comparison.
159: * <p>
160: * In this case, the argument must also be a Short, and the receiver and
161: * argument must represent the same short value.
162: *
163: * @param object
164: * the object to compare with this object
165: * @return <code>true</code> if the object is the same as this object
166: * <code>false</code> if it is different from this object
167: * @see #hashCode
168: */
169: @Override
170: public boolean equals(Object object) {
171: return (object instanceof Short)
172: && (value == ((Short) object).value);
173: }
174:
175: /**
176: * Answers the float value which the receiver represents
177: *
178: * @return float the value of the receiver.
179: */
180: @Override
181: public float floatValue() {
182: return value;
183: }
184:
185: /**
186: * Answers an integer hash code for the receiver. Any two objects which
187: * answer <code>true</code> when passed to <code>equals</code> must
188: * answer the same value for this method.
189: *
190: * @return the receiver's hash
191: *
192: * @see #equals
193: */
194: @Override
195: public int hashCode() {
196: return value;
197: }
198:
199: /**
200: * Answers the int value which the receiver represents
201: *
202: * @return int the value of the receiver.
203: */
204: @Override
205: public int intValue() {
206: return value;
207: }
208:
209: /**
210: * Answers the long value which the receiver represents
211: *
212: * @return long the value of the receiver.
213: */
214: @Override
215: public long longValue() {
216: return value;
217: }
218:
219: /**
220: * Parses the string argument as if it was a short value and returns the
221: * result. Throws NumberFormatException if the string does not represent an
222: * short quantity.
223: *
224: * @param string
225: * a string representation of a short quantity.
226: * @return short the value represented by the argument
227: * @exception NumberFormatException
228: * if the argument could not be parsed as a short quantity.
229: */
230: public static short parseShort(String string)
231: throws NumberFormatException {
232: return parseShort(string, 10);
233: }
234:
235: /**
236: * Parses the string argument as if it was a short value and returns the
237: * result. Throws NumberFormatException if the string does not represent a
238: * single short quantity. The second argument specifies the radix to use
239: * when parsing the value.
240: *
241: * @param string
242: * a string representation of a short quantity.
243: * @param radix
244: * the radix to use when parsing.
245: * @return short the value represented by the argument
246: * @exception NumberFormatException
247: * if the argument could not be parsed as a short quantity.
248: */
249: public static short parseShort(String string, int radix)
250: throws NumberFormatException {
251: int intValue = Integer.parseInt(string, radix);
252: short result = (short) intValue;
253: if (result == intValue) {
254: return result;
255: }
256: throw new NumberFormatException();
257: }
258:
259: /**
260: * Answers the short value which the receiver represents
261: *
262: * @return short the value of the receiver.
263: */
264: @Override
265: public short shortValue() {
266: return value;
267: }
268:
269: /**
270: * Answers a string containing a concise, human-readable description of the
271: * receiver.
272: *
273: * @return a printable representation for the receiver.
274: */
275: @Override
276: public String toString() {
277: return Integer.toString(value);
278: }
279:
280: /**
281: * Answers a string containing a concise, human-readable description of the
282: * argument.
283: *
284: * @param value
285: * short the short to convert.
286: * @return String a printable representation for the short.
287: */
288: public static String toString(short value) {
289: return Integer.toString(value);
290: }
291:
292: /**
293: * Parses the string argument as if it was a short value and returns a Short
294: * representing the result. Throws NumberFormatException if the string does
295: * not represent a single short quantity.
296: *
297: * @param string
298: * a string representation of a short quantity.
299: * @return Short the value represented by the argument
300: * @exception NumberFormatException
301: * if the argument could not be parsed as a short quantity.
302: */
303: public static Short valueOf(String string)
304: throws NumberFormatException {
305: return valueOf(parseShort(string));
306: }
307:
308: /**
309: * Parses the string argument as if it was a short value and returns a Short
310: * representing the result. Throws NumberFormatException if the string does
311: * not represent a short quantity. The second argument specifies the radix
312: * to use when parsing the value.
313: *
314: * @param string
315: * a string representation of a short quantity.
316: * @param radix
317: * the radix to use when parsing.
318: * @return Short the value represented by the argument
319: * @exception NumberFormatException
320: * if the argument could not be parsed as a short quantity.
321: */
322: public static Short valueOf(String string, int radix)
323: throws NumberFormatException {
324: return valueOf(parseShort(string, radix));
325: }
326:
327: /**
328: * <p>
329: * Reverses the bytes of a <code>short</code>.
330: * </p>
331: *
332: * @param s The <code>short</code> to reverse.
333: * @return The reversed value.
334: * @since 1.5
335: */
336: public static short reverseBytes(short s) {
337: int high = (s >> 8) & 0xFF;
338: int low = (s & 0xFF) << 8;
339: return (short) (low | high);
340: }
341:
342: /**
343: * <p>
344: * Returns a <code>Short</code> instance for the <code>short</code>
345: * value passed. This method is preferred over the constructor, as this
346: * method may maintain a cache of instances.
347: * </p>
348: *
349: * @param s The short value.
350: * @return A <code>Short</code> instance.
351: * @since 1.5
352: */
353: public static Short valueOf(short s) {
354: if (s < -128 || s > 127) {
355: return new Short(s);
356: }
357: return valueOfCache.CACHE[s + 128];
358: }
359:
360: static class valueOfCache {
361: /**
362: * <p>
363: * A cache of instances used by {@link Short#valueOf(short)} and auto-boxing.
364: * </p>
365: */
366: private static final Short[] CACHE = new Short[256];
367:
368: static {
369: for (int i = -128; i <= 127; i++) {
370: CACHE[i + 128] = new Short((short) i);
371: }
372: }
373: }
374: }
|