Index: /luni/src/main/java/java/lang/Short.java =================================================================== --- /luni/src/main/java/java/lang/Short.java (revision 383745) +++ /luni/src/main/java/java/lang/Short.java (working copy) @@ -15,25 +15,46 @@ package java.lang; - /** - * Shorts are objects (non-base types) which represent short values. + *

+ * Short is the wrapper for the primitive type short. + *

+ * + * @see java.lang.Number + * @since 1.1 */ public final class Short extends Number implements Comparable { - + //TODO Add Comparable to implements when generics are supported. private static final long serialVersionUID = 7515723908773894738L; /** - * The value which the receiver represents. - */ - final short value; + * The value which the receiver represents. + */ + private final short value; + + /** + *

+ * Constant for the maximum short value, 215-1. + *

+ */ + public static final short MAX_VALUE = (short) 0x7FFF; - /** - * Most positive and most negative possible short values. - */ - public static final short MAX_VALUE = (short) 0x7FFF; + /** + *

+ * Constant for the minimum short value, -215. + *

+ */ + public static final short MIN_VALUE = (short) 0x8000; - public static final short MIN_VALUE = (short) 0x8000; + /** + *

+ * Constant for the number of bits to represent a short in + * two's compliment form. + *

+ * + * @since 1.5 + */ + public static final int SIZE = 16; /** * The java.lang.Class that represents this class. @@ -43,6 +64,13 @@ // Note: This can't be set to "short.class", since *that* is // defined to be "java.lang.Short.TYPE"; + /** + *

+ * A cache of instances used by {@link #valueOf(short)} and auto-boxing. + *

+ */ + private static final Short[] CACHE = new Short[256]; + /** * Constructs a new instance of this class given a string. * @@ -99,7 +127,7 @@ int intValue = Integer.decode(string).intValue(); short result = (short) intValue; if (result == intValue) - return new Short(result); + return valueOf(result); throw new NumberFormatException(); } @@ -251,7 +279,7 @@ * if the argument could not be parsed as a short quantity. */ public static Short valueOf(String string) throws NumberFormatException { - return new Short(parseShort(string)); + return valueOf(parseShort(string)); } /** @@ -270,6 +298,43 @@ */ public static Short valueOf(String string, int radix) throws NumberFormatException { - return new Short(parseShort(string, radix)); + return valueOf(parseShort(string, radix)); } + + /** + *

+ * Reverses the bytes of a short. + *

+ * + * @param s The short to reverse. + * @return The reversed value. + * @since 1.5 + */ + public static short reverseBytes(short s) { + int high = (s >> 8) & 0xFF; + int low = (s & 0xFF) << 8; + return (short) (low | high); + } + + /** + *

+ * Returns a Short instance for the short + * value passed. This method is preferred over the constructor, as this + * method may maintain a cache of instances. + *

+ * + * @param s The short value. + * @return A Short instance. + * @since 1.5 + */ + public static Short valueOf(short s) { + if (s < -128 || s > 127) { + return new Short(s); + } + synchronized (CACHE) { + int idx = 128 + s; // 128 matches a cache size of 256 + Short result = CACHE[idx]; + return (result == null ? CACHE[idx] = new Short(s) : result); + } + } }