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     */
116    public static int compare(short lhs, short rhs) {
117        return lhs > rhs ? 1 : (lhs < rhs ? -1 : 0);
118    }
119
120    /**
121     * Parses the specified string and returns a {@code Short} instance if the
122     * string can be decoded into a short value. The string may be an optional
123     * minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal
124     * ("0..."), or decimal ("...") representation of a short.
125     *
126     * @param string
127     *            a string representation of a short value.
128     * @return a {@code Short} containing the value represented by
129     *         {@code string}.
130     * @throws NumberFormatException
131     *             if {@code string} cannot be parsed as a short value.
132     */
133    public static Short decode(String string) throws NumberFormatException {
134        int intValue = Integer.decode(string).intValue();
135        short result = (short) intValue;
136        if (result == intValue) {
137            return valueOf(result);
138        }
139        throw new NumberFormatException("Value out of range for short: \"" + string + "\"");
140    }
141
142    @Override
143    public double doubleValue() {
144        return value;
145    }
146
147    /**
148     * Compares this instance with the specified object and indicates if they
149     * are equal. In order to be equal, {@code object} must be an instance of
150     * {@code Short} and have the same short value as this object.
151     *
152     * @param object
153     *            the object to compare this short with.
154     * @return {@code true} if the specified object is equal to this
155     *         {@code Short}; {@code false} otherwise.
156     */
157    @Override
158    public boolean equals(Object object) {
159        return (object instanceof Short) && (((Short) object).value == value);
160    }
161
162    @Override
163    public float floatValue() {
164        return value;
165    }
166
167    @Override
168    public int hashCode() {
169        return value;
170    }
171
172    @Override
173    public int intValue() {
174        return value;
175    }
176
177    @Override
178    public long longValue() {
179        return value;
180    }
181
182    /**
183     * Parses the specified string as a signed decimal short value. The ASCII
184     * character \u002d ('-') is recognized as the minus sign.
185     *
186     * @param string
187     *            the string representation of a short value.
188     * @return the primitive short value represented by {@code string}.
189     * @throws NumberFormatException
190     *             if {@code string} cannot be parsed as a short value.
191     */
192    public static short parseShort(String string) throws NumberFormatException {
193        return parseShort(string, 10);
194    }
195
196    /**
197     * Parses the specified string as a signed short value using the specified
198     * radix. The ASCII character \u002d ('-') is recognized as the minus sign.
199     *
200     * @param string
201     *            the string representation of a short value.
202     * @param radix
203     *            the radix to use when parsing.
204     * @return the primitive short value represented by {@code string} using
205     *         {@code radix}.
206     * @throws NumberFormatException
207     *             if {@code string} cannot be parsed as a short value, or
208     *             {@code radix < Character.MIN_RADIX ||
209     *             radix > Character.MAX_RADIX}.
210     */
211    public static short parseShort(String string, int radix) throws NumberFormatException {
212        int intValue = Integer.parseInt(string, radix);
213        short result = (short) intValue;
214        if (result == intValue) {
215            return result;
216        }
217        throw new NumberFormatException("Value out of range for short: \"" + string + "\"");
218    }
219
220    /**
221     * Gets the primitive value of this short.
222     *
223     * @return this object's primitive value.
224     */
225    @Override
226    public short shortValue() {
227        return value;
228    }
229
230    @Override
231    public String toString() {
232        return Integer.toString(value);
233    }
234
235    /**
236     * Returns a string containing a concise, human-readable description of the
237     * specified short value with radix 10.
238     *
239     * @param value
240     *             the short to convert to a string.
241     * @return a printable representation of {@code value}.
242     */
243    public static String toString(short value) {
244        return Integer.toString(value);
245    }
246
247    /**
248     * Parses the specified string as a signed decimal short value.
249     *
250     * @param string
251     *            the string representation of a short value.
252     * @return a {@code Short} instance containing the short value represented
253     *         by {@code string}.
254     * @throws NumberFormatException
255     *             if {@code string} cannot be parsed as a short value.
256     * @see #parseShort(String)
257     */
258    public static Short valueOf(String string) throws NumberFormatException {
259        return valueOf(parseShort(string));
260    }
261
262    /**
263     * Parses the specified string as a signed short value using the specified
264     * radix.
265     *
266     * @param string
267     *            the string representation of a short value.
268     * @param radix
269     *            the radix to use when parsing.
270     * @return a {@code Short} instance containing the short value represented
271     *         by {@code string} using {@code radix}.
272     * @throws NumberFormatException
273     *             if {@code string} cannot be parsed as a short value, or
274     *             {@code radix < Character.MIN_RADIX ||
275     *             radix > Character.MAX_RADIX}.
276     * @see #parseShort(String, int)
277     */
278    public static Short valueOf(String string, int radix) throws NumberFormatException {
279        return valueOf(parseShort(string, radix));
280    }
281
282    /**
283     * Reverses the bytes of the specified short.
284     *
285     * @param s
286     *            the short value for which to reverse bytes.
287     * @return the reversed value.
288     * @since 1.5
289     */
290    public static short reverseBytes(short s) {
291        return (short) ((s << 8) | ((s >>> 8) & 0xFF));
292    }
293
294    /**
295     * Returns a {@code Short} instance for the specified short value.
296     * <p>
297     * If it is not necessary to get a new {@code Short} instance, it is
298     * recommended to use this method instead of the constructor, since it
299     * maintains a cache of instances which may result in better performance.
300     *
301     * @param s
302     *            the short value to store in the instance.
303     * @return a {@code Short} instance containing {@code s}.
304     * @since 1.5
305     */
306    public static Short valueOf(short s) {
307        return s < -128 || s >= 128 ? new Short(s) : SMALL_VALUES[s + 128];
308    }
309
310    /**
311     * A cache of instances used by {@link Short#valueOf(short)} and auto-boxing.
312     */
313    private static final Short[] SMALL_VALUES = new Short[256];
314
315    static {
316        for (int i = -128; i < 128; i++) {
317            SMALL_VALUES[i + 128] = new Short((short) i);
318        }
319    }
320}
321