jaroslav@67: /* jaroslav@67: * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved. jaroslav@67: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@67: * jaroslav@67: * This code is free software; you can redistribute it and/or modify it jaroslav@67: * under the terms of the GNU General Public License version 2 only, as jaroslav@67: * published by the Free Software Foundation. Oracle designates this jaroslav@67: * particular file as subject to the "Classpath" exception as provided jaroslav@67: * by Oracle in the LICENSE file that accompanied this code. jaroslav@67: * jaroslav@67: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@67: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@67: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@67: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@67: * accompanied this code). jaroslav@67: * jaroslav@67: * You should have received a copy of the GNU General Public License version jaroslav@67: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@67: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@67: * jaroslav@67: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@67: * or visit www.oracle.com if you need additional information or have any jaroslav@67: * questions. jaroslav@67: */ jaroslav@67: jaroslav@67: package java.lang; jaroslav@67: jaroslav@67: /** jaroslav@67: * The {@code Short} class wraps a value of primitive type {@code jaroslav@67: * short} in an object. An object of type {@code Short} contains a jaroslav@67: * single field whose type is {@code short}. jaroslav@67: * jaroslav@67: *

In addition, this class provides several methods for converting jaroslav@67: * a {@code short} to a {@code String} and a {@code String} to a jaroslav@67: * {@code short}, as well as other constants and methods useful when jaroslav@67: * dealing with a {@code short}. jaroslav@67: * jaroslav@67: * @author Nakul Saraiya jaroslav@67: * @author Joseph D. Darcy jaroslav@67: * @see java.lang.Number jaroslav@67: * @since JDK1.1 jaroslav@67: */ jaroslav@67: public final class Short extends Number implements Comparable { jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding the minimum value a {@code short} can jaroslav@67: * have, -215. jaroslav@67: */ jaroslav@67: public static final short MIN_VALUE = -32768; jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding the maximum value a {@code short} can jaroslav@67: * have, 215-1. jaroslav@67: */ jaroslav@67: public static final short MAX_VALUE = 32767; jaroslav@67: jaroslav@67: /** jaroslav@67: * The {@code Class} instance representing the primitive type jaroslav@67: * {@code short}. jaroslav@67: */ jaroslav@67: public static final Class TYPE = (Class) Class.getPrimitiveClass("short"); jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a new {@code String} object representing the jaroslav@67: * specified {@code short}. The radix is assumed to be 10. jaroslav@67: * jaroslav@67: * @param s the {@code short} to be converted jaroslav@67: * @return the string representation of the specified {@code short} jaroslav@67: * @see java.lang.Integer#toString(int) jaroslav@67: */ jaroslav@67: public static String toString(short s) { jaroslav@67: return Integer.toString((int)s, 10); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Parses the string argument as a signed {@code short} in the jaroslav@67: * radix specified by the second argument. The characters in the jaroslav@67: * string must all be digits, of the specified radix (as jaroslav@67: * determined by whether {@link java.lang.Character#digit(char, jaroslav@67: * int)} returns a nonnegative value) except that the first jaroslav@67: * character may be an ASCII minus sign {@code '-'} jaroslav@67: * ('\u002D') to indicate a negative value or an jaroslav@67: * ASCII plus sign {@code '+'} ('\u002B') to jaroslav@67: * indicate a positive value. The resulting {@code short} value jaroslav@67: * is returned. jaroslav@67: * jaroslav@67: *

An exception of type {@code NumberFormatException} is jaroslav@67: * thrown if any of the following situations occurs: jaroslav@67: *

jaroslav@67: * jaroslav@67: * @param s the {@code String} containing the jaroslav@67: * {@code short} representation to be parsed jaroslav@67: * @param radix the radix to be used while parsing {@code s} jaroslav@67: * @return the {@code short} represented by the string jaroslav@67: * argument in the specified radix. jaroslav@67: * @throws NumberFormatException If the {@code String} jaroslav@67: * does not contain a parsable {@code short}. jaroslav@67: */ jaroslav@67: public static short parseShort(String s, int radix) jaroslav@67: throws NumberFormatException { jaroslav@67: int i = Integer.parseInt(s, radix); jaroslav@67: if (i < MIN_VALUE || i > MAX_VALUE) jaroslav@67: throw new NumberFormatException( jaroslav@67: "Value out of range. Value:\"" + s + "\" Radix:" + radix); jaroslav@67: return (short)i; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Parses the string argument as a signed decimal {@code jaroslav@67: * short}. The characters in the string must all be decimal jaroslav@67: * digits, except that the first character may be an ASCII minus jaroslav@67: * sign {@code '-'} ('\u002D') to indicate a jaroslav@67: * negative value or an ASCII plus sign {@code '+'} jaroslav@67: * ('\u002B') to indicate a positive value. The jaroslav@67: * resulting {@code short} value is returned, exactly as if the jaroslav@67: * argument and the radix 10 were given as arguments to the {@link jaroslav@67: * #parseShort(java.lang.String, int)} method. jaroslav@67: * jaroslav@67: * @param s a {@code String} containing the {@code short} jaroslav@67: * representation to be parsed jaroslav@67: * @return the {@code short} value represented by the jaroslav@67: * argument in decimal. jaroslav@67: * @throws NumberFormatException If the string does not jaroslav@67: * contain a parsable {@code short}. jaroslav@67: */ jaroslav@67: public static short parseShort(String s) throws NumberFormatException { jaroslav@67: return parseShort(s, 10); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Short} object holding the value jaroslav@67: * extracted from the specified {@code String} when parsed jaroslav@67: * with the radix given by the second argument. The first argument jaroslav@67: * is interpreted as representing a signed {@code short} in jaroslav@67: * the radix specified by the second argument, exactly as if the jaroslav@67: * argument were given to the {@link #parseShort(java.lang.String, jaroslav@67: * int)} method. The result is a {@code Short} object that jaroslav@67: * represents the {@code short} value specified by the string. jaroslav@67: * jaroslav@67: *

In other words, this method returns a {@code Short} object jaroslav@67: * equal to the value of: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code new Short(Short.parseShort(s, radix))} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @param s the string to be parsed jaroslav@67: * @param radix the radix to be used in interpreting {@code s} jaroslav@67: * @return a {@code Short} object holding the value jaroslav@67: * represented by the string argument in the jaroslav@67: * specified radix. jaroslav@67: * @throws NumberFormatException If the {@code String} does jaroslav@67: * not contain a parsable {@code short}. jaroslav@67: */ jaroslav@67: public static Short valueOf(String s, int radix) jaroslav@67: throws NumberFormatException { jaroslav@67: return valueOf(parseShort(s, radix)); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Short} object holding the jaroslav@67: * value given by the specified {@code String}. The argument jaroslav@67: * is interpreted as representing a signed decimal jaroslav@67: * {@code short}, exactly as if the argument were given to jaroslav@67: * the {@link #parseShort(java.lang.String)} method. The result is jaroslav@67: * a {@code Short} object that represents the jaroslav@67: * {@code short} value specified by the string. jaroslav@67: * jaroslav@67: *

In other words, this method returns a {@code Short} object jaroslav@67: * equal to the value of: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code new Short(Short.parseShort(s))} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @param s the string to be parsed jaroslav@67: * @return a {@code Short} object holding the value jaroslav@67: * represented by the string argument jaroslav@67: * @throws NumberFormatException If the {@code String} does jaroslav@67: * not contain a parsable {@code short}. jaroslav@67: */ jaroslav@67: public static Short valueOf(String s) throws NumberFormatException { jaroslav@67: return valueOf(s, 10); jaroslav@67: } jaroslav@67: jaroslav@67: private static class ShortCache { jaroslav@67: private ShortCache(){} jaroslav@67: jaroslav@67: static final Short cache[] = new Short[-(-128) + 127 + 1]; jaroslav@67: jaroslav@67: static { jaroslav@67: for(int i = 0; i < cache.length; i++) jaroslav@67: cache[i] = new Short((short)(i - 128)); jaroslav@67: } jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Short} instance representing the specified jaroslav@67: * {@code short} value. jaroslav@67: * If a new {@code Short} instance is not required, this method jaroslav@67: * should generally be used in preference to the constructor jaroslav@67: * {@link #Short(short)}, as this method is likely to yield jaroslav@67: * significantly better space and time performance by caching jaroslav@67: * frequently requested values. jaroslav@67: * jaroslav@67: * This method will always cache values in the range -128 to 127, jaroslav@67: * inclusive, and may cache other values outside of this range. jaroslav@67: * jaroslav@67: * @param s a short value. jaroslav@67: * @return a {@code Short} instance representing {@code s}. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static Short valueOf(short s) { jaroslav@67: final int offset = 128; jaroslav@67: int sAsInt = s; jaroslav@67: if (sAsInt >= -128 && sAsInt <= 127) { // must cache jaroslav@67: return ShortCache.cache[sAsInt + offset]; jaroslav@67: } jaroslav@67: return new Short(s); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Decodes a {@code String} into a {@code Short}. jaroslav@67: * Accepts decimal, hexadecimal, and octal numbers given by jaroslav@67: * the following grammar: jaroslav@67: * jaroslav@67: *
jaroslav@67: *
jaroslav@67: *
DecodableString: jaroslav@67: *
Signopt DecimalNumeral jaroslav@67: *
Signopt {@code 0x} HexDigits jaroslav@67: *
Signopt {@code 0X} HexDigits jaroslav@67: *
Signopt {@code #} HexDigits jaroslav@67: *
Signopt {@code 0} OctalDigits jaroslav@67: *

jaroslav@67: *

Sign: jaroslav@67: *
{@code -} jaroslav@67: *
{@code +} jaroslav@67: *
jaroslav@67: *
jaroslav@67: * jaroslav@67: * DecimalNumeral, HexDigits, and OctalDigits jaroslav@67: * are as defined in section 3.10.1 of jaroslav@67: * The Java™ Language Specification, jaroslav@67: * except that underscores are not accepted between digits. jaroslav@67: * jaroslav@67: *

The sequence of characters following an optional jaroslav@67: * sign and/or radix specifier ("{@code 0x}", "{@code 0X}", jaroslav@67: * "{@code #}", or leading zero) is parsed as by the {@code jaroslav@67: * Short.parseShort} method with the indicated radix (10, 16, or jaroslav@67: * 8). This sequence of characters must represent a positive jaroslav@67: * value or a {@link NumberFormatException} will be thrown. The jaroslav@67: * result is negated if first character of the specified {@code jaroslav@67: * String} is the minus sign. No whitespace characters are jaroslav@67: * permitted in the {@code String}. jaroslav@67: * jaroslav@67: * @param nm the {@code String} to decode. jaroslav@67: * @return a {@code Short} object holding the {@code short} jaroslav@67: * value represented by {@code nm} jaroslav@67: * @throws NumberFormatException if the {@code String} does not jaroslav@67: * contain a parsable {@code short}. jaroslav@67: * @see java.lang.Short#parseShort(java.lang.String, int) jaroslav@67: */ jaroslav@67: public static Short decode(String nm) throws NumberFormatException { jaroslav@67: int i = Integer.decode(nm); jaroslav@67: if (i < MIN_VALUE || i > MAX_VALUE) jaroslav@67: throw new NumberFormatException( jaroslav@67: "Value " + i + " out of range from input " + nm); jaroslav@67: return valueOf((short)i); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * The value of the {@code Short}. jaroslav@67: * jaroslav@67: * @serial jaroslav@67: */ jaroslav@67: private final short value; jaroslav@67: jaroslav@67: /** jaroslav@67: * Constructs a newly allocated {@code Short} object that jaroslav@67: * represents the specified {@code short} value. jaroslav@67: * jaroslav@67: * @param value the value to be represented by the jaroslav@67: * {@code Short}. jaroslav@67: */ jaroslav@67: public Short(short value) { jaroslav@67: this.value = value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Constructs a newly allocated {@code Short} object that jaroslav@67: * represents the {@code short} value indicated by the jaroslav@67: * {@code String} parameter. The string is converted to a jaroslav@67: * {@code short} value in exactly the manner used by the jaroslav@67: * {@code parseShort} method for radix 10. jaroslav@67: * jaroslav@67: * @param s the {@code String} to be converted to a jaroslav@67: * {@code Short} jaroslav@67: * @throws NumberFormatException If the {@code String} jaroslav@67: * does not contain a parsable {@code short}. jaroslav@67: * @see java.lang.Short#parseShort(java.lang.String, int) jaroslav@67: */ jaroslav@67: public Short(String s) throws NumberFormatException { jaroslav@67: this.value = parseShort(s, 10); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Short} as a jaroslav@67: * {@code byte}. jaroslav@67: */ jaroslav@67: public byte byteValue() { jaroslav@67: return (byte)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Short} as a jaroslav@67: * {@code short}. jaroslav@67: */ jaroslav@67: public short shortValue() { jaroslav@67: return value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Short} as an jaroslav@67: * {@code int}. jaroslav@67: */ jaroslav@67: public int intValue() { jaroslav@67: return (int)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Short} as a jaroslav@67: * {@code long}. jaroslav@67: */ jaroslav@67: public long longValue() { jaroslav@67: return (long)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Short} as a jaroslav@67: * {@code float}. jaroslav@67: */ jaroslav@67: public float floatValue() { jaroslav@67: return (float)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Short} as a jaroslav@67: * {@code double}. jaroslav@67: */ jaroslav@67: public double doubleValue() { jaroslav@67: return (double)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code String} object representing this jaroslav@67: * {@code Short}'s value. The value is converted to signed jaroslav@67: * decimal representation and returned as a string, exactly as if jaroslav@67: * the {@code short} value were given as an argument to the jaroslav@67: * {@link java.lang.Short#toString(short)} method. jaroslav@67: * jaroslav@67: * @return a string representation of the value of this object in jaroslav@67: * base 10. jaroslav@67: */ jaroslav@67: public String toString() { jaroslav@67: return Integer.toString((int)value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a hash code for this {@code Short}; equal to the result jaroslav@67: * of invoking {@code intValue()}. jaroslav@67: * jaroslav@67: * @return a hash code value for this {@code Short} jaroslav@67: */ jaroslav@67: public int hashCode() { jaroslav@67: return (int)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares this object to the specified object. The result is jaroslav@67: * {@code true} if and only if the argument is not jaroslav@67: * {@code null} and is a {@code Short} object that jaroslav@67: * contains the same {@code short} value as this object. jaroslav@67: * jaroslav@67: * @param obj the object to compare with jaroslav@67: * @return {@code true} if the objects are the same; jaroslav@67: * {@code false} otherwise. jaroslav@67: */ jaroslav@67: public boolean equals(Object obj) { jaroslav@67: if (obj instanceof Short) { jaroslav@67: return value == ((Short)obj).shortValue(); jaroslav@67: } jaroslav@67: return false; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares two {@code Short} objects numerically. jaroslav@67: * jaroslav@67: * @param anotherShort the {@code Short} to be compared. jaroslav@67: * @return the value {@code 0} if this {@code Short} is jaroslav@67: * equal to the argument {@code Short}; a value less than jaroslav@67: * {@code 0} if this {@code Short} is numerically less jaroslav@67: * than the argument {@code Short}; and a value greater than jaroslav@67: * {@code 0} if this {@code Short} is numerically jaroslav@67: * greater than the argument {@code Short} (signed jaroslav@67: * comparison). jaroslav@67: * @since 1.2 jaroslav@67: */ jaroslav@67: public int compareTo(Short anotherShort) { jaroslav@67: return compare(this.value, anotherShort.value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares two {@code short} values numerically. jaroslav@67: * The value returned is identical to what would be returned by: jaroslav@67: *

jaroslav@67:      *    Short.valueOf(x).compareTo(Short.valueOf(y))
jaroslav@67:      * 
jaroslav@67: * jaroslav@67: * @param x the first {@code short} to compare jaroslav@67: * @param y the second {@code short} to compare jaroslav@67: * @return the value {@code 0} if {@code x == y}; jaroslav@67: * a value less than {@code 0} if {@code x < y}; and jaroslav@67: * a value greater than {@code 0} if {@code x > y} jaroslav@67: * @since 1.7 jaroslav@67: */ jaroslav@67: public static int compare(short x, short y) { jaroslav@67: return x - y; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * The number of bits used to represent a {@code short} value in two's jaroslav@67: * complement binary form. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static final int SIZE = 16; jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value obtained by reversing the order of the bytes in the jaroslav@67: * two's complement representation of the specified {@code short} value. jaroslav@67: * jaroslav@67: * @return the value obtained by reversing (or, equivalently, swapping) jaroslav@67: * the bytes in the specified {@code short} value. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static short reverseBytes(short i) { jaroslav@67: return (short) (((i & 0xFF00) >> 8) | (i << 8)); jaroslav@67: } jaroslav@67: jaroslav@67: /** use serialVersionUID from JDK 1.1. for interoperability */ jaroslav@67: private static final long serialVersionUID = 7515723908773894738L; jaroslav@67: }