1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/lang/Short.java Wed Jan 23 20:39:23 2013 +0100
1.3 @@ -0,0 +1,468 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +/**
1.32 + * The {@code Short} class wraps a value of primitive type {@code
1.33 + * short} in an object. An object of type {@code Short} contains a
1.34 + * single field whose type is {@code short}.
1.35 + *
1.36 + * <p>In addition, this class provides several methods for converting
1.37 + * a {@code short} to a {@code String} and a {@code String} to a
1.38 + * {@code short}, as well as other constants and methods useful when
1.39 + * dealing with a {@code short}.
1.40 + *
1.41 + * @author Nakul Saraiya
1.42 + * @author Joseph D. Darcy
1.43 + * @see java.lang.Number
1.44 + * @since JDK1.1
1.45 + */
1.46 +public final class Short extends Number implements Comparable<Short> {
1.47 +
1.48 + /**
1.49 + * A constant holding the minimum value a {@code short} can
1.50 + * have, -2<sup>15</sup>.
1.51 + */
1.52 + public static final short MIN_VALUE = -32768;
1.53 +
1.54 + /**
1.55 + * A constant holding the maximum value a {@code short} can
1.56 + * have, 2<sup>15</sup>-1.
1.57 + */
1.58 + public static final short MAX_VALUE = 32767;
1.59 +
1.60 + /**
1.61 + * The {@code Class} instance representing the primitive type
1.62 + * {@code short}.
1.63 + */
1.64 + public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short");
1.65 +
1.66 + /**
1.67 + * Returns a new {@code String} object representing the
1.68 + * specified {@code short}. The radix is assumed to be 10.
1.69 + *
1.70 + * @param s the {@code short} to be converted
1.71 + * @return the string representation of the specified {@code short}
1.72 + * @see java.lang.Integer#toString(int)
1.73 + */
1.74 + public static String toString(short s) {
1.75 + return Integer.toString((int)s, 10);
1.76 + }
1.77 +
1.78 + /**
1.79 + * Parses the string argument as a signed {@code short} in the
1.80 + * radix specified by the second argument. The characters in the
1.81 + * string must all be digits, of the specified radix (as
1.82 + * determined by whether {@link java.lang.Character#digit(char,
1.83 + * int)} returns a nonnegative value) except that the first
1.84 + * character may be an ASCII minus sign {@code '-'}
1.85 + * (<code>'\u002D'</code>) to indicate a negative value or an
1.86 + * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
1.87 + * indicate a positive value. The resulting {@code short} value
1.88 + * is returned.
1.89 + *
1.90 + * <p>An exception of type {@code NumberFormatException} is
1.91 + * thrown if any of the following situations occurs:
1.92 + * <ul>
1.93 + * <li> The first argument is {@code null} or is a string of
1.94 + * length zero.
1.95 + *
1.96 + * <li> The radix is either smaller than {@link
1.97 + * java.lang.Character#MIN_RADIX} or larger than {@link
1.98 + * java.lang.Character#MAX_RADIX}.
1.99 + *
1.100 + * <li> Any character of the string is not a digit of the
1.101 + * specified radix, except that the first character may be a minus
1.102 + * sign {@code '-'} (<code>'\u002D'</code>) or plus sign
1.103 + * {@code '+'} (<code>'\u002B'</code>) provided that the
1.104 + * string is longer than length 1.
1.105 + *
1.106 + * <li> The value represented by the string is not a value of type
1.107 + * {@code short}.
1.108 + * </ul>
1.109 + *
1.110 + * @param s the {@code String} containing the
1.111 + * {@code short} representation to be parsed
1.112 + * @param radix the radix to be used while parsing {@code s}
1.113 + * @return the {@code short} represented by the string
1.114 + * argument in the specified radix.
1.115 + * @throws NumberFormatException If the {@code String}
1.116 + * does not contain a parsable {@code short}.
1.117 + */
1.118 + public static short parseShort(String s, int radix)
1.119 + throws NumberFormatException {
1.120 + int i = Integer.parseInt(s, radix);
1.121 + if (i < MIN_VALUE || i > MAX_VALUE)
1.122 + throw new NumberFormatException(
1.123 + "Value out of range. Value:\"" + s + "\" Radix:" + radix);
1.124 + return (short)i;
1.125 + }
1.126 +
1.127 + /**
1.128 + * Parses the string argument as a signed decimal {@code
1.129 + * short}. The characters in the string must all be decimal
1.130 + * digits, except that the first character may be an ASCII minus
1.131 + * sign {@code '-'} (<code>'\u002D'</code>) to indicate a
1.132 + * negative value or an ASCII plus sign {@code '+'}
1.133 + * (<code>'\u002B'</code>) to indicate a positive value. The
1.134 + * resulting {@code short} value is returned, exactly as if the
1.135 + * argument and the radix 10 were given as arguments to the {@link
1.136 + * #parseShort(java.lang.String, int)} method.
1.137 + *
1.138 + * @param s a {@code String} containing the {@code short}
1.139 + * representation to be parsed
1.140 + * @return the {@code short} value represented by the
1.141 + * argument in decimal.
1.142 + * @throws NumberFormatException If the string does not
1.143 + * contain a parsable {@code short}.
1.144 + */
1.145 + public static short parseShort(String s) throws NumberFormatException {
1.146 + return parseShort(s, 10);
1.147 + }
1.148 +
1.149 + /**
1.150 + * Returns a {@code Short} object holding the value
1.151 + * extracted from the specified {@code String} when parsed
1.152 + * with the radix given by the second argument. The first argument
1.153 + * is interpreted as representing a signed {@code short} in
1.154 + * the radix specified by the second argument, exactly as if the
1.155 + * argument were given to the {@link #parseShort(java.lang.String,
1.156 + * int)} method. The result is a {@code Short} object that
1.157 + * represents the {@code short} value specified by the string.
1.158 + *
1.159 + * <p>In other words, this method returns a {@code Short} object
1.160 + * equal to the value of:
1.161 + *
1.162 + * <blockquote>
1.163 + * {@code new Short(Short.parseShort(s, radix))}
1.164 + * </blockquote>
1.165 + *
1.166 + * @param s the string to be parsed
1.167 + * @param radix the radix to be used in interpreting {@code s}
1.168 + * @return a {@code Short} object holding the value
1.169 + * represented by the string argument in the
1.170 + * specified radix.
1.171 + * @throws NumberFormatException If the {@code String} does
1.172 + * not contain a parsable {@code short}.
1.173 + */
1.174 + public static Short valueOf(String s, int radix)
1.175 + throws NumberFormatException {
1.176 + return valueOf(parseShort(s, radix));
1.177 + }
1.178 +
1.179 + /**
1.180 + * Returns a {@code Short} object holding the
1.181 + * value given by the specified {@code String}. The argument
1.182 + * is interpreted as representing a signed decimal
1.183 + * {@code short}, exactly as if the argument were given to
1.184 + * the {@link #parseShort(java.lang.String)} method. The result is
1.185 + * a {@code Short} object that represents the
1.186 + * {@code short} value specified by the string.
1.187 + *
1.188 + * <p>In other words, this method returns a {@code Short} object
1.189 + * equal to the value of:
1.190 + *
1.191 + * <blockquote>
1.192 + * {@code new Short(Short.parseShort(s))}
1.193 + * </blockquote>
1.194 + *
1.195 + * @param s the string to be parsed
1.196 + * @return a {@code Short} object holding the value
1.197 + * represented by the string argument
1.198 + * @throws NumberFormatException If the {@code String} does
1.199 + * not contain a parsable {@code short}.
1.200 + */
1.201 + public static Short valueOf(String s) throws NumberFormatException {
1.202 + return valueOf(s, 10);
1.203 + }
1.204 +
1.205 + private static class ShortCache {
1.206 + private ShortCache(){}
1.207 +
1.208 + static final Short cache[] = new Short[-(-128) + 127 + 1];
1.209 +
1.210 + static {
1.211 + for(int i = 0; i < cache.length; i++)
1.212 + cache[i] = new Short((short)(i - 128));
1.213 + }
1.214 + }
1.215 +
1.216 + /**
1.217 + * Returns a {@code Short} instance representing the specified
1.218 + * {@code short} value.
1.219 + * If a new {@code Short} instance is not required, this method
1.220 + * should generally be used in preference to the constructor
1.221 + * {@link #Short(short)}, as this method is likely to yield
1.222 + * significantly better space and time performance by caching
1.223 + * frequently requested values.
1.224 + *
1.225 + * This method will always cache values in the range -128 to 127,
1.226 + * inclusive, and may cache other values outside of this range.
1.227 + *
1.228 + * @param s a short value.
1.229 + * @return a {@code Short} instance representing {@code s}.
1.230 + * @since 1.5
1.231 + */
1.232 + public static Short valueOf(short s) {
1.233 + final int offset = 128;
1.234 + int sAsInt = s;
1.235 + if (sAsInt >= -128 && sAsInt <= 127) { // must cache
1.236 + return ShortCache.cache[sAsInt + offset];
1.237 + }
1.238 + return new Short(s);
1.239 + }
1.240 +
1.241 + /**
1.242 + * Decodes a {@code String} into a {@code Short}.
1.243 + * Accepts decimal, hexadecimal, and octal numbers given by
1.244 + * the following grammar:
1.245 + *
1.246 + * <blockquote>
1.247 + * <dl>
1.248 + * <dt><i>DecodableString:</i>
1.249 + * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1.250 + * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1.251 + * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1.252 + * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1.253 + * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1.254 + * <p>
1.255 + * <dt><i>Sign:</i>
1.256 + * <dd>{@code -}
1.257 + * <dd>{@code +}
1.258 + * </dl>
1.259 + * </blockquote>
1.260 + *
1.261 + * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
1.262 + * are as defined in section 3.10.1 of
1.263 + * <cite>The Java™ Language Specification</cite>,
1.264 + * except that underscores are not accepted between digits.
1.265 + *
1.266 + * <p>The sequence of characters following an optional
1.267 + * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
1.268 + * "{@code #}", or leading zero) is parsed as by the {@code
1.269 + * Short.parseShort} method with the indicated radix (10, 16, or
1.270 + * 8). This sequence of characters must represent a positive
1.271 + * value or a {@link NumberFormatException} will be thrown. The
1.272 + * result is negated if first character of the specified {@code
1.273 + * String} is the minus sign. No whitespace characters are
1.274 + * permitted in the {@code String}.
1.275 + *
1.276 + * @param nm the {@code String} to decode.
1.277 + * @return a {@code Short} object holding the {@code short}
1.278 + * value represented by {@code nm}
1.279 + * @throws NumberFormatException if the {@code String} does not
1.280 + * contain a parsable {@code short}.
1.281 + * @see java.lang.Short#parseShort(java.lang.String, int)
1.282 + */
1.283 + public static Short decode(String nm) throws NumberFormatException {
1.284 + int i = Integer.decode(nm);
1.285 + if (i < MIN_VALUE || i > MAX_VALUE)
1.286 + throw new NumberFormatException(
1.287 + "Value " + i + " out of range from input " + nm);
1.288 + return valueOf((short)i);
1.289 + }
1.290 +
1.291 + /**
1.292 + * The value of the {@code Short}.
1.293 + *
1.294 + * @serial
1.295 + */
1.296 + private final short value;
1.297 +
1.298 + /**
1.299 + * Constructs a newly allocated {@code Short} object that
1.300 + * represents the specified {@code short} value.
1.301 + *
1.302 + * @param value the value to be represented by the
1.303 + * {@code Short}.
1.304 + */
1.305 + public Short(short value) {
1.306 + this.value = value;
1.307 + }
1.308 +
1.309 + /**
1.310 + * Constructs a newly allocated {@code Short} object that
1.311 + * represents the {@code short} value indicated by the
1.312 + * {@code String} parameter. The string is converted to a
1.313 + * {@code short} value in exactly the manner used by the
1.314 + * {@code parseShort} method for radix 10.
1.315 + *
1.316 + * @param s the {@code String} to be converted to a
1.317 + * {@code Short}
1.318 + * @throws NumberFormatException If the {@code String}
1.319 + * does not contain a parsable {@code short}.
1.320 + * @see java.lang.Short#parseShort(java.lang.String, int)
1.321 + */
1.322 + public Short(String s) throws NumberFormatException {
1.323 + this.value = parseShort(s, 10);
1.324 + }
1.325 +
1.326 + /**
1.327 + * Returns the value of this {@code Short} as a
1.328 + * {@code byte}.
1.329 + */
1.330 + public byte byteValue() {
1.331 + return (byte)value;
1.332 + }
1.333 +
1.334 + /**
1.335 + * Returns the value of this {@code Short} as a
1.336 + * {@code short}.
1.337 + */
1.338 + public short shortValue() {
1.339 + return value;
1.340 + }
1.341 +
1.342 + /**
1.343 + * Returns the value of this {@code Short} as an
1.344 + * {@code int}.
1.345 + */
1.346 + public int intValue() {
1.347 + return (int)value;
1.348 + }
1.349 +
1.350 + /**
1.351 + * Returns the value of this {@code Short} as a
1.352 + * {@code long}.
1.353 + */
1.354 + public long longValue() {
1.355 + return (long)value;
1.356 + }
1.357 +
1.358 + /**
1.359 + * Returns the value of this {@code Short} as a
1.360 + * {@code float}.
1.361 + */
1.362 + public float floatValue() {
1.363 + return (float)value;
1.364 + }
1.365 +
1.366 + /**
1.367 + * Returns the value of this {@code Short} as a
1.368 + * {@code double}.
1.369 + */
1.370 + public double doubleValue() {
1.371 + return (double)value;
1.372 + }
1.373 +
1.374 + /**
1.375 + * Returns a {@code String} object representing this
1.376 + * {@code Short}'s value. The value is converted to signed
1.377 + * decimal representation and returned as a string, exactly as if
1.378 + * the {@code short} value were given as an argument to the
1.379 + * {@link java.lang.Short#toString(short)} method.
1.380 + *
1.381 + * @return a string representation of the value of this object in
1.382 + * base 10.
1.383 + */
1.384 + public String toString() {
1.385 + return Integer.toString((int)value);
1.386 + }
1.387 +
1.388 + /**
1.389 + * Returns a hash code for this {@code Short}; equal to the result
1.390 + * of invoking {@code intValue()}.
1.391 + *
1.392 + * @return a hash code value for this {@code Short}
1.393 + */
1.394 + public int hashCode() {
1.395 + return (int)value;
1.396 + }
1.397 +
1.398 + /**
1.399 + * Compares this object to the specified object. The result is
1.400 + * {@code true} if and only if the argument is not
1.401 + * {@code null} and is a {@code Short} object that
1.402 + * contains the same {@code short} value as this object.
1.403 + *
1.404 + * @param obj the object to compare with
1.405 + * @return {@code true} if the objects are the same;
1.406 + * {@code false} otherwise.
1.407 + */
1.408 + public boolean equals(Object obj) {
1.409 + if (obj instanceof Short) {
1.410 + return value == ((Short)obj).shortValue();
1.411 + }
1.412 + return false;
1.413 + }
1.414 +
1.415 + /**
1.416 + * Compares two {@code Short} objects numerically.
1.417 + *
1.418 + * @param anotherShort the {@code Short} to be compared.
1.419 + * @return the value {@code 0} if this {@code Short} is
1.420 + * equal to the argument {@code Short}; a value less than
1.421 + * {@code 0} if this {@code Short} is numerically less
1.422 + * than the argument {@code Short}; and a value greater than
1.423 + * {@code 0} if this {@code Short} is numerically
1.424 + * greater than the argument {@code Short} (signed
1.425 + * comparison).
1.426 + * @since 1.2
1.427 + */
1.428 + public int compareTo(Short anotherShort) {
1.429 + return compare(this.value, anotherShort.value);
1.430 + }
1.431 +
1.432 + /**
1.433 + * Compares two {@code short} values numerically.
1.434 + * The value returned is identical to what would be returned by:
1.435 + * <pre>
1.436 + * Short.valueOf(x).compareTo(Short.valueOf(y))
1.437 + * </pre>
1.438 + *
1.439 + * @param x the first {@code short} to compare
1.440 + * @param y the second {@code short} to compare
1.441 + * @return the value {@code 0} if {@code x == y};
1.442 + * a value less than {@code 0} if {@code x < y}; and
1.443 + * a value greater than {@code 0} if {@code x > y}
1.444 + * @since 1.7
1.445 + */
1.446 + public static int compare(short x, short y) {
1.447 + return x - y;
1.448 + }
1.449 +
1.450 + /**
1.451 + * The number of bits used to represent a {@code short} value in two's
1.452 + * complement binary form.
1.453 + * @since 1.5
1.454 + */
1.455 + public static final int SIZE = 16;
1.456 +
1.457 + /**
1.458 + * Returns the value obtained by reversing the order of the bytes in the
1.459 + * two's complement representation of the specified {@code short} value.
1.460 + *
1.461 + * @return the value obtained by reversing (or, equivalently, swapping)
1.462 + * the bytes in the specified {@code short} value.
1.463 + * @since 1.5
1.464 + */
1.465 + public static short reverseBytes(short i) {
1.466 + return (short) (((i & 0xFF00) >> 8) | (i << 8));
1.467 + }
1.468 +
1.469 + /** use serialVersionUID from JDK 1.1. for interoperability */
1.470 + private static final long serialVersionUID = 7515723908773894738L;
1.471 +}