1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.lang;
19
20/**
21 * The wrapper for the primitive type {@code short}.
22 *
23 * @see java.lang.Number
24 * @since 1.1
25 */
26@FindBugsSuppressWarnings("DM_NUMBER_CTOR")
27public final class Short extends Number implements Comparable<Short> {
28
29    private static final long serialVersionUID = 7515723908773894738L;
30
31    /**
32     * The value which the receiver represents.
33     */
34    private final short value;
35
36    /**
37     * Constant for the maximum {@code short} value, 2<sup>15</sup>-1.
38     */
39    public static final short MAX_VALUE = (short) 0x7FFF;
40
41    /**
42     * Constant for the minimum {@code short} value, -2<sup>15</sup>.
43     */
44    public static final short MIN_VALUE = (short) 0x8000;
45
46    /**
47     * Constant for the number of bits needed to represent a {@code short} in
48     * two's complement form.
49     *
50     * @since 1.5
51     */
52    public static final int SIZE = 16;
53
54    /**
55     * The {@link Class} object that represents the primitive type {@code
56     * short}.
57     */
58    @SuppressWarnings("unchecked")
59    public static final Class<Short> TYPE
60            = (Class<Short>) short[].class.getComponentType();
61    // Note: Short.TYPE can't be set to "short.class", since *that* is
62    // defined to be "java.lang.Short.TYPE";
63
64    /**
65     * Constructs a new {@code Short} from the specified string.
66     *
67     * @param string
68     *            the string representation of a short value.
69     * @throws NumberFormatException
70     *             if {@code string} cannot be parsed as a short value.
71     * @see #parseShort(String)
72     */
73    public Short(String string) throws NumberFormatException {
74        this(parseShort(string));
75    }
76
77    /**
78     * Constructs a new {@code Short} with the specified primitive short value.
79     *
80     * @param value
81     *            the primitive short value to store in the new instance.
82     */
83    public Short(short value) {
84        this.value = value;
85    }
86
87    @Override
88    public byte byteValue() {
89        return (byte) value;
90    }
91
92    /**
93     * Compares this object to the specified short object to determine their
94     * relative order.
95     *
96     * @param object
97     *            the short object to compare this object to.
98     * @return a negative value if the value of this short is less than the
99     *         value of {@code object}; 0 if the value of this short and the
100     *         value of {@code object} are equal; a positive value if the value
101     *         of this short is greater than the value of {@code object}.
102     * @throws NullPointerException
103     *             if {@code object} is null.
104     * @see java.lang.Comparable
105     * @since 1.2
106     */
107    public int compareTo(Short object) {
108        return compare(value, object.value);
109    }
110
111    /**
112     * Compares two {@code short} values.
113     * @return 0 if lhs = rhs, less than 0 if lhs &lt; rhs, and greater than 0 if lhs &gt; rhs.
114     * @since 1.7
115     * @hide 1.7
116     */
117    public static int compare(long lhs, long rhs) {
118        return lhs > rhs ? 1 : (lhs < rhs ? -1 : 0);
119    }
120
121    /**
122     * Parses the specified string and returns a {@code Short} instance if the
123     * string can be decoded into a short value. The string may be an optional
124     * minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal
125     * ("0..."), or decimal ("...") representation of a short.
126     *
127     * @param string
128     *            a string representation of a short value.
129     * @return a {@code Short} containing the value represented by
130     *         {@code string}.
131     * @throws NumberFormatException
132     *             if {@code string} cannot be parsed as a short value.
133     */
134    public static Short decode(String string) throws NumberFormatException {
135        int intValue = Integer.decode(string).intValue();
136        short result = (short) intValue;
137        if (result == intValue) {
138            return valueOf(result);
139        }
140        throw new NumberFormatException("Value out of range for short: \"" + string + "\"");
141    }
142
143    @Override
144    public double doubleValue() {
145        return value;
146    }
147
148    /**
149     * Compares this instance with the specified object and indicates if they
150     * are equal. In order to be equal, {@code object} must be an instance of
151     * {@code Short} and have the same short value as this object.
152     *
153     * @param object
154     *            the object to compare this short with.
155     * @return {@code true} if the specified object is equal to this
156     *         {@code Short}; {@code false} otherwise.
157     */
158    @Override
159    public boolean equals(Object object) {
160        return (object instanceof Short) && (((Short) object).value == value);
161    }
162
163    @Override
164    public float floatValue() {
165        return value;
166    }
167
168    @Override
169    public int hashCode() {
170        return value;
171    }
172
173    @Override
174    public int intValue() {
175        return value;
176    }
177
178    @Override
179    public long longValue() {
180        return value;
181    }
182
183    /**
184     * Parses the specified string as a signed decimal short value. The ASCII
185     * character \u002d ('-') is recognized as the minus sign.
186     *
187     * @param string
188     *            the string representation of a short value.
189     * @return the primitive short value represented by {@code string}.
190     * @throws NumberFormatException
191     *             if {@code string} cannot be parsed as a short value.
192     */
193    public static short parseShort(String string) throws NumberFormatException {
194        return parseShort(string, 10);
195    }
196
197    /**
198     * Parses the specified string as a signed short value using the specified
199     * radix. The ASCII character \u002d ('-') is recognized as the minus sign.
200     *
201     * @param string
202     *            the string representation of a short value.
203     * @param radix
204     *            the radix to use when parsing.
205     * @return the primitive short value represented by {@code string} using
206     *         {@code radix}.
207     * @throws NumberFormatException
208     *             if {@code string} cannot be parsed as a short value, or
209     *             {@code radix < Character.MIN_RADIX ||
210     *             radix > Character.MAX_RADIX}.
211     */
212    public static short parseShort(String string, int radix) throws NumberFormatException {
213        int intValue = Integer.parseInt(string, radix);
214        short result = (short) intValue;
215        if (result == intValue) {
216            return result;
217        }
218        throw new NumberFormatException("Value out of range for short: \"" + string + "\"");
219    }
220
221    /**
222     * Gets the primitive value of this short.
223     *
224     * @return this object's primitive value.
225     */
226    @Override
227    public short shortValue() {
228        return value;
229    }
230
231    @Override
232    public String toString() {
233        return Integer.toString(value);
234    }
235
236    /**
237     * Returns a string containing a concise, human-readable description of the
238     * specified short value with radix 10.
239     *
240     * @param value
241     *             the short to convert to a string.
242     * @return a printable representation of {@code value}.
243     */
244    public static String toString(short value) {
245        return Integer.toString(value);
246    }
247
248    /**
249     * Parses the specified string as a signed decimal short value.
250     *
251     * @param string
252     *            the string representation of a short value.
253     * @return a {@code Short} instance containing the short value represented
254     *         by {@code string}.
255     * @throws NumberFormatException
256     *             if {@code string} cannot be parsed as a short value.
257     * @see #parseShort(String)
258     */
259    public static Short valueOf(String string) throws NumberFormatException {
260        return valueOf(parseShort(string));
261    }
262
263    /**
264     * Parses the specified string as a signed short value using the specified
265     * radix.
266     *
267     * @param string
268     *            the string representation of a short value.
269     * @param radix
270     *            the radix to use when parsing.
271     * @return a {@code Short} instance containing the short value represented
272     *         by {@code string} using {@code radix}.
273     * @throws NumberFormatException
274     *             if {@code string} cannot be parsed as a short value, or
275     *             {@code radix < Character.MIN_RADIX ||
276     *             radix > Character.MAX_RADIX}.
277     * @see #parseShort(String, int)
278     */
279    public static Short valueOf(String string, int radix) throws NumberFormatException {
280        return valueOf(parseShort(string, radix));
281    }
282
283    /**
284     * Reverses the bytes of the specified short.
285     *
286     * @param s
287     *            the short value for which to reverse bytes.
288     * @return the reversed value.
289     * @since 1.5
290     */
291    public static short reverseBytes(short s) {
292        return (short) ((s << 8) | ((s >>> 8) & 0xFF));
293    }
294
295    /**
296     * Returns a {@code Short} instance for the specified short value.
297     * <p>
298     * If it is not necessary to get a new {@code Short} instance, it is
299     * recommended to use this method instead of the constructor, since it
300     * maintains a cache of instances which may result in better performance.
301     *
302     * @param s
303     *            the short value to store in the instance.
304     * @return a {@code Short} instance containing {@code s}.
305     * @since 1.5
306     */
307    public static Short valueOf(short s) {
308        return s < -128 || s >= 128 ? new Short(s) : SMALL_VALUES[s + 128];
309    }
310
311    /**
312     * A cache of instances used by {@link Short#valueOf(short)} and auto-boxing.
313     */
314    private static final Short[] SMALL_VALUES = new Short[256];
315
316    static {
317        for (int i = -128; i < 128; i++) {
318            SMALL_VALUES[i + 128] = new Short((short) i);
319        }
320    }
321}
322