FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / lang / Short.java
blob54e36a5f65cc7d09acc8a78485958767dc894e5f
1 /* Short.java -- object wrapper for short
2 Copyright (C) 1998, 2001, 2002 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 * @since 1.1
52 * @status updated to 1.4
54 public final class Short extends Number implements Comparable
56 /**
57 * Compatible with JDK 1.1+.
59 private static final long serialVersionUID = 7515723908773894738L;
61 /**
62 * The minimum value a <code>short</code> can represent is -32768 (or
63 * -2<sup>15</sup).
65 public static final short MIN_VALUE = -32768;
67 /**
68 * The minimum value a <code>short</code> can represent is 32767 (or
69 * 2<sup>15</sup).
71 public static final short MAX_VALUE = 32767;
73 /**
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');
79 /**
80 * The immutable value of this Short.
82 * @serial the wrapped short
84 private final short value;
86 /**
87 * Create a <code>Short</code> object representing the value of the
88 * <code>short</code> argument.
90 * @param value the value to use
92 public Short(short value)
94 this.value = value;
97 /**
98 * Create a <code>Short</code> object representing the value of the
99 * argument after conversion to a <code>short</code>.
101 * @param s the string to convert
102 * @throws NumberFormatException if the String cannot be parsed
104 public Short(String s)
106 value = parseShort(s, 10);
110 * Converts the <code>short</code> to a <code>String</code> and assumes
111 * a radix of 10.
113 * @param s the <code>short</code> to convert to <code>String</code>
114 * @return the <code>String</code> representation of the argument
116 public static String toString(short s)
118 return String.valueOf(s);
122 * Converts the specified <code>String</code> into a <code>short</code>.
123 * This function assumes a radix of 10.
125 * @param s the <code>String</code> to convert
126 * @return the <code>short</code> value of <code>s</code>
127 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
128 * <code>short</code>
130 public static short parseShort(String s)
132 return parseShort(s, 10);
136 * Converts the specified <code>String</code> into a <code>short</code>
137 * using the specified radix (base). The string must not be <code>null</code>
138 * or empty. It may begin with an optional '-', which will negate the answer,
139 * provided that there are also valid digits. Each digit is parsed as if by
140 * <code>Character.digit(d, radix)</code>, and must be in the range
141 * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
142 * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
143 * Unlike Double.parseDouble, you may not have a leading '+'.
145 * @param s the <code>String</code> to convert
146 * @param radix the radix (base) to use in the conversion
147 * @return the <code>String</code> argument converted to </code>short</code>
148 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
149 * <code>short</code>
151 public static short parseShort(String s, int radix)
153 int i = Integer.parseInt(s, radix, false);
154 if ((short) i != i)
155 throw new NumberFormatException();
156 return (short) i;
160 * Creates a new <code>Short</code> object using the <code>String</code>
161 * and specified radix (base).
163 * @param s the <code>String</code> to convert
164 * @param radix the radix (base) to convert with
165 * @return the new <code>Short</code>
166 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
167 * <code>short</code>
168 * @see #parseShort(String, int)
170 public static Short valueOf(String s, int radix)
172 return new Short(parseShort(s, radix));
176 * Creates a new <code>Short</code> object using the <code>String</code>,
177 * assuming a radix of 10.
179 * @param s the <code>String</code> to convert
180 * @return the new <code>Short</code>
181 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
182 * <code>short</code>
183 * @see #Short(String)
184 * @see #parseShort(String)
186 public static Short valueOf(String s)
188 return new Short(parseShort(s, 10));
192 * Convert the specified <code>String</code> into a <code>Short</code>.
193 * The <code>String</code> may represent decimal, hexadecimal, or
194 * octal numbers.
196 * <p>The extended BNF grammar is as follows:<br>
197 * <pre>
198 * <em>DecodableString</em>:
199 * ( [ <code>-</code> ] <em>DecimalNumber</em> )
200 * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
201 * | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
202 * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
203 * <em>DecimalNumber</em>:
204 * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
205 * <em>DecimalDigit</em>:
206 * <em>Character.digit(d, 10) has value 0 to 9</em>
207 * <em>OctalDigit</em>:
208 * <em>Character.digit(d, 8) has value 0 to 7</em>
209 * <em>DecimalDigit</em>:
210 * <em>Character.digit(d, 16) has value 0 to 15</em>
211 * </pre>
212 * Finally, the value must be in the range <code>MIN_VALUE</code> to
213 * <code>MAX_VALUE</code>, or an exception is thrown.
215 * @param s the <code>String</code> to interpret
216 * @return the value of the String as a <code>Short</code>
217 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
218 * <code>short</code>
219 * @throws NullPointerException if <code>s</code> is null
220 * @see Integer#decode(String)
222 public static Short decode(String s)
224 int i = Integer.parseInt(s, 10, true);
225 if ((short) i != i)
226 throw new NumberFormatException();
227 return new Short((short) i);
231 * Return the value of this <code>Short</code> as a <code>byte</code>.
233 * @return the byte value
235 public byte byteValue()
237 return (byte) value;
241 * Return the value of this <code>Short</code>.
243 * @return the short value
245 public short shortValue()
247 return value;
251 * Return the value of this <code>Short</code> as an <code>int</code>.
253 * @return the int value
255 public int intValue()
257 return value;
261 * Return the value of this <code>Short</code> as a <code>long</code>.
263 * @return the long value
265 public long longValue()
267 return value;
271 * Return the value of this <code>Short</code> as a <code>float</code>.
273 * @return the float value
275 public float floatValue()
277 return value;
281 * Return the value of this <code>Short</code> as a <code>double</code>.
283 * @return the double value
285 public double doubleValue()
287 return value;
291 * Converts the <code>Short</code> value to a <code>String</code> and
292 * assumes a radix of 10.
294 * @return the <code>String</code> representation of this <code>Short</code>
296 public String toString()
298 return String.valueOf(value);
302 * Return a hashcode representing this Object. <code>Short</code>'s hash
303 * code is simply its value.
305 * @return this Object's hash code
307 public int hashCode()
309 return value;
313 * Returns <code>true</code> if <code>obj</code> is an instance of
314 * <code>Short</code> and represents the same short value.
316 * @param obj the object to compare
317 * @return whether these Objects are semantically equal
319 public boolean equals(Object obj)
321 return obj instanceof Short && value == ((Short) obj).value;
325 * Compare two Shorts numerically by comparing their <code>short</code>
326 * values. The result is positive if the first is greater, negative if the
327 * second is greater, and 0 if the two are equal.
329 * @param s the Short to compare
330 * @return the comparison
331 * @since 1.2
333 public int compareTo(Short s)
335 return value - s.value;
339 * Behaves like <code>compareTo(Short)</code> unless the Object
340 * is not a <code>Short</code>.
342 * @param o the object to compare
343 * @return the comparison
344 * @throws ClassCastException if the argument is not a <code>Short</code>
345 * @see #compareTo(Short)
346 * @see Comparable
347 * @since 1.2
349 public int compareTo(Object o)
351 return compareTo((Short)o);