1 /* Short.java -- object wrapper for short
2 Copyright (C) 1998, 2001, 2002, 2004, 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)
51 * @author Tom Tromey (tromey@redhat.com)
52 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
54 * @status updated to 1.5
56 public final class Short extends Number implements Comparable<Short>
59 * Compatible with JDK 1.1+.
61 private static final long serialVersionUID = 7515723908773894738L;
64 * The minimum value a <code>short</code> can represent is -32768 (or
67 public static final short MIN_VALUE = -32768;
70 * The minimum value a <code>short</code> can represent is 32767 (or
73 public static final short MAX_VALUE = 32767;
76 * The primitive type <code>short</code> is represented by this
77 * <code>Class</code> object.
79 public static final Class<Short> TYPE = (Class<Short>) VMClassLoader.getPrimitiveClass('S');
82 * The number of bits needed to represent a <code>short</code>.
85 public static final int SIZE = 16;
87 // This caches some Short values, and is used by boxing conversions
88 // via valueOf(). We must cache at least -128..127; these constants
89 // control how much we actually cache.
90 private static final int MIN_CACHE = -128;
91 private static final int MAX_CACHE = 127;
92 private static Short[] shortCache = new Short[MAX_CACHE - MIN_CACHE + 1];
95 * The immutable value of this Short.
97 * @serial the wrapped short
99 private final short value;
102 * Create a <code>Short</code> object representing the value of the
103 * <code>short</code> argument.
105 * @param value the value to use
107 public Short(short value)
113 * Create a <code>Short</code> object representing the value of the
114 * argument after conversion to a <code>short</code>.
116 * @param s the string to convert
117 * @throws NumberFormatException if the String cannot be parsed
119 public Short(String s)
121 value = parseShort(s, 10);
125 * Converts the <code>short</code> to a <code>String</code> and assumes
128 * @param s the <code>short</code> to convert to <code>String</code>
129 * @return the <code>String</code> representation of the argument
131 public static String toString(short s)
133 return String.valueOf(s);
137 * Converts the specified <code>String</code> into a <code>short</code>.
138 * This function assumes a radix of 10.
140 * @param s the <code>String</code> to convert
141 * @return the <code>short</code> value of <code>s</code>
142 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
145 public static short parseShort(String s)
147 return parseShort(s, 10);
151 * Converts the specified <code>String</code> into a <code>short</code>
152 * using the specified radix (base). The string must not be <code>null</code>
153 * or empty. It may begin with an optional '-', which will negate the answer,
154 * provided that there are also valid digits. Each digit is parsed as if by
155 * <code>Character.digit(d, radix)</code>, and must be in the range
156 * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
157 * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
158 * Unlike Double.parseDouble, you may not have a leading '+'.
160 * @param s the <code>String</code> to convert
161 * @param radix the radix (base) to use in the conversion
162 * @return the <code>String</code> argument converted to <code>short</code>
163 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
166 public static short parseShort(String s, int radix)
168 int i = Integer.parseInt(s, radix, false);
170 throw new NumberFormatException();
175 * Creates a new <code>Short</code> object using the <code>String</code>
176 * and specified radix (base).
178 * @param s the <code>String</code> to convert
179 * @param radix the radix (base) to convert with
180 * @return the new <code>Short</code>
181 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
183 * @see #parseShort(String, int)
185 public static Short valueOf(String s, int radix)
187 return new Short(parseShort(s, radix));
191 * Creates a new <code>Short</code> object using the <code>String</code>,
192 * assuming a radix of 10.
194 * @param s the <code>String</code> to convert
195 * @return the new <code>Short</code>
196 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
198 * @see #Short(String)
199 * @see #parseShort(String)
201 public static Short valueOf(String s)
203 return new Short(parseShort(s, 10));
207 * Returns a <code>Short</code> object wrapping the value.
208 * In contrast to the <code>Short</code> constructor, this method
209 * will cache some values. It is used by boxing conversion.
211 * @param val the value to wrap
212 * @return the <code>Short</code>
215 public static Short valueOf(short val)
217 if (val < MIN_CACHE || val > MAX_CACHE)
218 return new Short(val);
219 synchronized (shortCache)
221 if (shortCache[val - MIN_CACHE] == null)
222 shortCache[val - MIN_CACHE] = new Short(val);
223 return shortCache[val - MIN_CACHE];
228 * Convert the specified <code>String</code> into a <code>Short</code>.
229 * The <code>String</code> may represent decimal, hexadecimal, or
232 * <p>The extended BNF grammar is as follows:<br>
234 * <em>DecodableString</em>:
235 * ( [ <code>-</code> ] <em>DecimalNumber</em> )
236 * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
237 * | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
238 * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
239 * <em>DecimalNumber</em>:
240 * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
241 * <em>DecimalDigit</em>:
242 * <em>Character.digit(d, 10) has value 0 to 9</em>
243 * <em>OctalDigit</em>:
244 * <em>Character.digit(d, 8) has value 0 to 7</em>
245 * <em>DecimalDigit</em>:
246 * <em>Character.digit(d, 16) has value 0 to 15</em>
248 * Finally, the value must be in the range <code>MIN_VALUE</code> to
249 * <code>MAX_VALUE</code>, or an exception is thrown.
251 * @param s the <code>String</code> to interpret
252 * @return the value of the String as a <code>Short</code>
253 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
255 * @throws NullPointerException if <code>s</code> is null
256 * @see Integer#decode(String)
258 public static Short decode(String s)
260 int i = Integer.parseInt(s, 10, true);
262 throw new NumberFormatException();
263 return new Short((short) i);
267 * Return the value of this <code>Short</code> as a <code>byte</code>.
269 * @return the byte value
271 public byte byteValue()
277 * Return the value of this <code>Short</code>.
279 * @return the short value
281 public short shortValue()
287 * Return the value of this <code>Short</code> as an <code>int</code>.
289 * @return the int value
291 public int intValue()
297 * Return the value of this <code>Short</code> as a <code>long</code>.
299 * @return the long value
301 public long longValue()
307 * Return the value of this <code>Short</code> as a <code>float</code>.
309 * @return the float value
311 public float floatValue()
317 * Return the value of this <code>Short</code> as a <code>double</code>.
319 * @return the double value
321 public double doubleValue()
327 * Converts the <code>Short</code> value to a <code>String</code> and
328 * assumes a radix of 10.
330 * @return the <code>String</code> representation of this <code>Short</code>
332 public String toString()
334 return String.valueOf(value);
338 * Return a hashcode representing this Object. <code>Short</code>'s hash
339 * code is simply its value.
341 * @return this Object's hash code
343 public int hashCode()
349 * Returns <code>true</code> if <code>obj</code> is an instance of
350 * <code>Short</code> and represents the same short value.
352 * @param obj the object to compare
353 * @return whether these Objects are semantically equal
355 public boolean equals(Object obj)
357 return obj instanceof Short && value == ((Short) obj).value;
361 * Compare two Shorts numerically by comparing their <code>short</code>
362 * values. The result is positive if the first is greater, negative if the
363 * second is greater, and 0 if the two are equal.
365 * @param s the Short to compare
366 * @return the comparison
369 public int compareTo(Short s)
371 return value - s.value;
375 * Reverse the bytes in val.
378 public static short reverseBytes(short val)
380 return (short) (((val >> 8) & 0xff) | ((val << 8) & 0xff00));