libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / lang / Short.java
blobec87f933e9383754408d0b5c8069e8815d3db7e2
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package java.lang;
41 /**
42 * Instances of class <code>Short</code> represent primitive
43 * <code>short</code> values.
45 * Additionally, this class provides various helper functions and variables
46 * related to shorts.
48 * @author Paul Fisher
49 * @author John Keiser
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)
53 * @since 1.1
54 * @status updated to 1.5
56 public final class Short extends Number implements Comparable<Short>
58 /**
59 * Compatible with JDK 1.1+.
61 private static final long serialVersionUID = 7515723908773894738L;
63 /**
64 * The minimum value a <code>short</code> can represent is -32768 (or
65 * -2<sup>15</sup>).
67 public static final short MIN_VALUE = -32768;
69 /**
70 * The minimum value a <code>short</code> can represent is 32767 (or
71 * 2<sup>15</sup>).
73 public static final short MAX_VALUE = 32767;
75 /**
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');
81 /**
82 * The number of bits needed to represent a <code>short</code>.
83 * @since 1.5
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];
93 static
95 for (short i=MIN_CACHE; i <= MAX_CACHE; i++)
96 shortCache[i - MIN_CACHE] = new Short(i);
99 /**
100 * The immutable value of this Short.
102 * @serial the wrapped short
104 private final short value;
107 * Create a <code>Short</code> object representing the value of the
108 * <code>short</code> argument.
110 * @param value the value to use
112 public Short(short value)
114 this.value = value;
118 * Create a <code>Short</code> object representing the value of the
119 * argument after conversion to a <code>short</code>.
121 * @param s the string to convert
122 * @throws NumberFormatException if the String cannot be parsed
124 public Short(String s)
126 value = parseShort(s, 10);
130 * Converts the <code>short</code> to a <code>String</code> and assumes
131 * a radix of 10.
133 * @param s the <code>short</code> to convert to <code>String</code>
134 * @return the <code>String</code> representation of the argument
136 public static String toString(short s)
138 return String.valueOf(s);
142 * Converts the specified <code>String</code> into a <code>short</code>.
143 * This function assumes a radix of 10.
145 * @param s the <code>String</code> to convert
146 * @return the <code>short</code> value of <code>s</code>
147 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
148 * <code>short</code>
150 public static short parseShort(String s)
152 return parseShort(s, 10);
156 * Converts the specified <code>String</code> into a <code>short</code>
157 * using the specified radix (base). The string must not be <code>null</code>
158 * or empty. It may begin with an optional '-', which will negate the answer,
159 * provided that there are also valid digits. Each digit is parsed as if by
160 * <code>Character.digit(d, radix)</code>, and must be in the range
161 * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
162 * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
163 * Unlike Double.parseDouble, you may not have a leading '+'.
165 * @param s the <code>String</code> to convert
166 * @param radix the radix (base) to use in the conversion
167 * @return the <code>String</code> argument converted to <code>short</code>
168 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
169 * <code>short</code>
171 public static short parseShort(String s, int radix)
173 int i = Integer.parseInt(s, radix, false);
174 if ((short) i != i)
175 throw new NumberFormatException();
176 return (short) i;
180 * Creates a new <code>Short</code> object using the <code>String</code>
181 * and specified radix (base).
183 * @param s the <code>String</code> to convert
184 * @param radix the radix (base) to convert with
185 * @return the new <code>Short</code>
186 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
187 * <code>short</code>
188 * @see #parseShort(String, int)
190 public static Short valueOf(String s, int radix)
192 return valueOf(parseShort(s, radix));
196 * Creates a new <code>Short</code> object using the <code>String</code>,
197 * assuming a radix of 10.
199 * @param s the <code>String</code> to convert
200 * @return the new <code>Short</code>
201 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
202 * <code>short</code>
203 * @see #Short(String)
204 * @see #parseShort(String)
206 public static Short valueOf(String s)
208 return valueOf(parseShort(s, 10));
212 * Returns a <code>Short</code> object wrapping the value.
213 * In contrast to the <code>Short</code> constructor, this method
214 * will cache some values. It is used by boxing conversion.
216 * @param val the value to wrap
217 * @return the <code>Short</code>
218 * @since 1.5
220 public static Short valueOf(short val)
222 if (val < MIN_CACHE || val > MAX_CACHE)
223 return new Short(val);
224 else
225 return shortCache[val - MIN_CACHE];
229 * Convert the specified <code>String</code> into a <code>Short</code>.
230 * The <code>String</code> may represent decimal, hexadecimal, or
231 * octal numbers.
233 * <p>The extended BNF grammar is as follows:<br>
234 * <pre>
235 * <em>DecodableString</em>:
236 * ( [ <code>-</code> ] <em>DecimalNumber</em> )
237 * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
238 * | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
239 * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
240 * <em>DecimalNumber</em>:
241 * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
242 * <em>DecimalDigit</em>:
243 * <em>Character.digit(d, 10) has value 0 to 9</em>
244 * <em>OctalDigit</em>:
245 * <em>Character.digit(d, 8) has value 0 to 7</em>
246 * <em>DecimalDigit</em>:
247 * <em>Character.digit(d, 16) has value 0 to 15</em>
248 * </pre>
249 * Finally, the value must be in the range <code>MIN_VALUE</code> to
250 * <code>MAX_VALUE</code>, or an exception is thrown.
252 * @param s the <code>String</code> to interpret
253 * @return the value of the String as a <code>Short</code>
254 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
255 * <code>short</code>
256 * @throws NullPointerException if <code>s</code> is null
257 * @see Integer#decode(String)
259 public static Short decode(String s)
261 int i = Integer.parseInt(s, 10, true);
262 if ((short) i != i)
263 throw new NumberFormatException();
264 return valueOf((short) i);
268 * Return the value of this <code>Short</code> as a <code>byte</code>.
270 * @return the byte value
272 public byte byteValue()
274 return (byte) value;
278 * Return the value of this <code>Short</code>.
280 * @return the short value
282 public short shortValue()
284 return value;
288 * Return the value of this <code>Short</code> as an <code>int</code>.
290 * @return the int value
292 public int intValue()
294 return value;
298 * Return the value of this <code>Short</code> as a <code>long</code>.
300 * @return the long value
302 public long longValue()
304 return value;
308 * Return the value of this <code>Short</code> as a <code>float</code>.
310 * @return the float value
312 public float floatValue()
314 return value;
318 * Return the value of this <code>Short</code> as a <code>double</code>.
320 * @return the double value
322 public double doubleValue()
324 return value;
328 * Converts the <code>Short</code> value to a <code>String</code> and
329 * assumes a radix of 10.
331 * @return the <code>String</code> representation of this <code>Short</code>
333 public String toString()
335 return String.valueOf(value);
339 * Return a hashcode representing this Object. <code>Short</code>'s hash
340 * code is simply its value.
342 * @return this Object's hash code
344 public int hashCode()
346 return value;
350 * Returns <code>true</code> if <code>obj</code> is an instance of
351 * <code>Short</code> and represents the same short value.
353 * @param obj the object to compare
354 * @return whether these Objects are semantically equal
356 public boolean equals(Object obj)
358 return obj instanceof Short && value == ((Short) obj).value;
362 * Compare two Shorts numerically by comparing their <code>short</code>
363 * values. The result is positive if the first is greater, negative if the
364 * second is greater, and 0 if the two are equal.
366 * @param s the Short to compare
367 * @return the comparison
368 * @since 1.2
370 public int compareTo(Short s)
372 return value - s.value;
376 * Reverse the bytes in val.
377 * @since 1.5
379 public static short reverseBytes(short val)
381 return (short) (((val >> 8) & 0xff) | ((val << 8) & 0xff00));