001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.lang;
017:
018: /**
019: * Wraps a primitive <code>short</code> as an object.
020: */
021: public final class Short extends Number implements Comparable<Short> {
022:
023: public static final short MIN_VALUE = (short) 0x8000;
024: public static final short MAX_VALUE = (short) 0x7fff;
025: public static final int SIZE = 16;
026:
027: // Box values according to JLS - between -128 and 127
028: private static Short[] boxedValues = new Short[256];
029:
030: public static Short decode(String s) throws NumberFormatException {
031: return new Short((short) __decodeAndValidateLong(s, MIN_VALUE,
032: MAX_VALUE));
033: }
034:
035: /**
036: * @skip Here for shared implementation with Arrays.hashCode
037: */
038: public static int hashCode(short s) {
039: return s;
040: }
041:
042: public static short parseShort(String s)
043: throws NumberFormatException {
044: return parseShort(s, 10);
045: }
046:
047: public static short parseShort(String s, int radix)
048: throws NumberFormatException {
049: return (short) __parseAndValidateLong(s, radix, MIN_VALUE,
050: MAX_VALUE);
051: }
052:
053: public static short reverseBytes(short s) {
054: return (short) (((s & 0xff) << 8) | ((s & 0xff00) >> 8));
055: }
056:
057: public static String toString(short b) {
058: return String.valueOf(b);
059: }
060:
061: public static Short valueOf(short s) {
062: if (s > -129 && s < 128) {
063: int rebase = s + 128;
064: if (boxedValues[rebase] == null) {
065: boxedValues[rebase] = new Short(s);
066: }
067: return boxedValues[rebase];
068: }
069: return new Short(s);
070: }
071:
072: public static Short valueOf(String s) throws NumberFormatException {
073: return new Short(Short.parseShort(s));
074: }
075:
076: public static Short valueOf(String s, int radix)
077: throws NumberFormatException {
078: return new Short(Short.parseShort(s, radix));
079: }
080:
081: private final transient short value;
082:
083: public Short(short value) {
084: this .value = value;
085: }
086:
087: public Short(String s) {
088: value = parseShort(s);
089: }
090:
091: @Override
092: public byte byteValue() {
093: return (byte) value;
094: }
095:
096: public int compareTo(Short b) {
097: if (value < b.value) {
098: return -1;
099: } else if (value > b.value) {
100: return 1;
101: } else {
102: return 0;
103: }
104: }
105:
106: @Override
107: public double doubleValue() {
108: return value;
109: }
110:
111: @Override
112: public boolean equals(Object o) {
113: return (o instanceof Short) && (((Short) o).value == value);
114: }
115:
116: @Override
117: public float floatValue() {
118: return value;
119: }
120:
121: @Override
122: public int hashCode() {
123: return hashCode(value);
124: }
125:
126: @Override
127: public int intValue() {
128: return value;
129: }
130:
131: @Override
132: public long longValue() {
133: return value;
134: }
135:
136: @Override
137: public short shortValue() {
138: return value;
139: }
140:
141: @Override
142: public String toString() {
143: return toString(value);
144: }
145: }
|