1 /* Short.java -- object wrapper for short
2 Copyright (C) 1998, 2001, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
42 * Instances of class <code>Short</code> represent primitive
43 * <code>short</code> values.
45 * Additionally, this class provides various helper functions and variables
50 * @author Eric Blake (ebb9@email.byu.edu)
52 * @status updated to 1.4
54 public final class Short
extends Number
implements Comparable
57 * Compatible with JDK 1.1+.
59 private static final long serialVersionUID
= 7515723908773894738L;
62 * The minimum value a <code>short</code> can represent is -32768 (or
65 public static final short MIN_VALUE
= -32768;
68 * The minimum value a <code>short</code> can represent is 32767 (or
71 public static final short MAX_VALUE
= 32767;
74 * The primitive type <code>short</code> is represented by this
75 * <code>Class</code> object.
77 public static final Class TYPE
= VMClassLoader
.getPrimitiveClass('S');
80 * The number of bits needed to represent a <code>short</code>.
83 public static final int SIZE
= 16;
85 // This caches some Short values, and is used by boxing conversions
86 // via valueOf(). We must cache at least -128..127; these constants
87 // control how much we actually cache.
88 private static final int MIN_CACHE
= -128;
89 private static final int MAX_CACHE
= 127;
90 private static Short
[] shortCache
= new Short
[MAX_CACHE
- MIN_CACHE
+ 1];
93 * The immutable value of this Short.
95 * @serial the wrapped short
97 private final short value
;
100 * Create a <code>Short</code> object representing the value of the
101 * <code>short</code> argument.
103 * @param value the value to use
105 public Short(short value
)
111 * Create a <code>Short</code> object representing the value of the
112 * argument after conversion to a <code>short</code>.
114 * @param s the string to convert
115 * @throws NumberFormatException if the String cannot be parsed
117 public Short(String s
)
119 value
= parseShort(s
, 10);
123 * Converts the <code>short</code> to a <code>String</code> and assumes
126 * @param s the <code>short</code> to convert to <code>String</code>
127 * @return the <code>String</code> representation of the argument
129 public static String
toString(short s
)
131 return String
.valueOf(s
);
135 * Converts the specified <code>String</code> into a <code>short</code>.
136 * This function assumes a radix of 10.
138 * @param s the <code>String</code> to convert
139 * @return the <code>short</code> value of <code>s</code>
140 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
143 public static short parseShort(String s
)
145 return parseShort(s
, 10);
149 * Converts the specified <code>String</code> into a <code>short</code>
150 * using the specified radix (base). The string must not be <code>null</code>
151 * or empty. It may begin with an optional '-', which will negate the answer,
152 * provided that there are also valid digits. Each digit is parsed as if by
153 * <code>Character.digit(d, radix)</code>, and must be in the range
154 * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
155 * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
156 * Unlike Double.parseDouble, you may not have a leading '+'.
158 * @param s the <code>String</code> to convert
159 * @param radix the radix (base) to use in the conversion
160 * @return the <code>String</code> argument converted to <code>short</code>
161 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
164 public static short parseShort(String s
, int radix
)
166 int i
= Integer
.parseInt(s
, radix
, false);
168 throw new NumberFormatException();
173 * Creates a new <code>Short</code> object using the <code>String</code>
174 * and specified radix (base).
176 * @param s the <code>String</code> to convert
177 * @param radix the radix (base) to convert with
178 * @return the new <code>Short</code>
179 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
181 * @see #parseShort(String, int)
183 public static Short
valueOf(String s
, int radix
)
185 return new Short(parseShort(s
, radix
));
189 * Creates a new <code>Short</code> object using the <code>String</code>,
190 * assuming a radix of 10.
192 * @param s the <code>String</code> to convert
193 * @return the new <code>Short</code>
194 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
196 * @see #Short(String)
197 * @see #parseShort(String)
199 public static Short
valueOf(String s
)
201 return new Short(parseShort(s
, 10));
205 * Returns a <code>Short</code> object wrapping the value.
206 * In contrast to the <code>Short</code> constructor, this method
207 * will cache some values. It is used by boxing conversion.
209 * @param val the value to wrap
210 * @return the <code>Short</code>
214 public static Short
valueOf(short val
)
216 if (val
< MIN_CACHE
|| val
> MAX_CACHE
)
217 return new Short(val
);
218 synchronized (shortCache
)
220 if (shortCache
[val
- MIN_CACHE
] == null)
221 shortCache
[val
- MIN_CACHE
] = new Short(val
);
222 return shortCache
[val
- MIN_CACHE
];
227 * Convert the specified <code>String</code> into a <code>Short</code>.
228 * The <code>String</code> may represent decimal, hexadecimal, or
231 * <p>The extended BNF grammar is as follows:<br>
233 * <em>DecodableString</em>:
234 * ( [ <code>-</code> ] <em>DecimalNumber</em> )
235 * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
236 * | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
237 * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
238 * <em>DecimalNumber</em>:
239 * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
240 * <em>DecimalDigit</em>:
241 * <em>Character.digit(d, 10) has value 0 to 9</em>
242 * <em>OctalDigit</em>:
243 * <em>Character.digit(d, 8) has value 0 to 7</em>
244 * <em>DecimalDigit</em>:
245 * <em>Character.digit(d, 16) has value 0 to 15</em>
247 * Finally, the value must be in the range <code>MIN_VALUE</code> to
248 * <code>MAX_VALUE</code>, or an exception is thrown.
250 * @param s the <code>String</code> to interpret
251 * @return the value of the String as a <code>Short</code>
252 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
254 * @throws NullPointerException if <code>s</code> is null
255 * @see Integer#decode(String)
257 public static Short
decode(String s
)
259 int i
= Integer
.parseInt(s
, 10, true);
261 throw new NumberFormatException();
262 return new Short((short) i
);
266 * Return the value of this <code>Short</code> as a <code>byte</code>.
268 * @return the byte value
270 public byte byteValue()
276 * Return the value of this <code>Short</code>.
278 * @return the short value
280 public short shortValue()
286 * Return the value of this <code>Short</code> as an <code>int</code>.
288 * @return the int value
290 public int intValue()
296 * Return the value of this <code>Short</code> as a <code>long</code>.
298 * @return the long value
300 public long longValue()
306 * Return the value of this <code>Short</code> as a <code>float</code>.
308 * @return the float value
310 public float floatValue()
316 * Return the value of this <code>Short</code> as a <code>double</code>.
318 * @return the double value
320 public double doubleValue()
326 * Converts the <code>Short</code> value to a <code>String</code> and
327 * assumes a radix of 10.
329 * @return the <code>String</code> representation of this <code>Short</code>
331 public String
toString()
333 return String
.valueOf(value
);
337 * Return a hashcode representing this Object. <code>Short</code>'s hash
338 * code is simply its value.
340 * @return this Object's hash code
342 public int hashCode()
348 * Returns <code>true</code> if <code>obj</code> is an instance of
349 * <code>Short</code> and represents the same short value.
351 * @param obj the object to compare
352 * @return whether these Objects are semantically equal
354 public boolean equals(Object obj
)
356 return obj
instanceof Short
&& value
== ((Short
) obj
).value
;
360 * Compare two Shorts numerically by comparing their <code>short</code>
361 * values. The result is positive if the first is greater, negative if the
362 * second is greater, and 0 if the two are equal.
364 * @param s the Short to compare
365 * @return the comparison
368 public int compareTo(Short s
)
370 return value
- s
.value
;
374 * Behaves like <code>compareTo(Short)</code> unless the Object
375 * is not a <code>Short</code>.
377 * @param o the object to compare
378 * @return the comparison
379 * @throws ClassCastException if the argument is not a <code>Short</code>
380 * @see #compareTo(Short)
384 public int compareTo(Object o
)
386 return compareTo((Short
)o
);
390 * Reverse the bytes in val.
393 public static short reverseBytes(short val
)
395 return (short) (((val
>> 8) & 0xff) | ((val
<< 8) & 0xff00));