diff options
Diffstat (limited to 'libjava/classpath/java/lang/Short.java')
-rw-r--r-- | libjava/classpath/java/lang/Short.java | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/libjava/classpath/java/lang/Short.java b/libjava/classpath/java/lang/Short.java index fbeea915bd3..eb40cd9e0e6 100644 --- a/libjava/classpath/java/lang/Short.java +++ b/libjava/classpath/java/lang/Short.java @@ -77,6 +77,19 @@ public final class Short extends Number implements Comparable public static final Class TYPE = VMClassLoader.getPrimitiveClass('S'); /** + * The number of bits needed to represent a <code>short</code>. + * @since 1.5 + */ + public static final int SIZE = 16; + + // This caches some Short values, and is used by boxing conversions + // via valueOf(). We must cache at least -128..127; these constants + // control how much we actually cache. + private static final int MIN_CACHE = -128; + private static final int MAX_CACHE = 127; + private static Short[] shortCache = new Short[MAX_CACHE - MIN_CACHE + 1]; + + /** * The immutable value of this Short. * * @serial the wrapped short @@ -189,6 +202,28 @@ public final class Short extends Number implements Comparable } /** + * Returns a <code>Short</code> object wrapping the value. + * In contrast to the <code>Short</code> constructor, this method + * will cache some values. It is used by boxing conversion. + * + * @param val the value to wrap + * @return the <code>Short</code> + * + * @since 1.5 + */ + public static Short valueOf(short val) + { + if (val < MIN_CACHE || val > MAX_CACHE) + return new Short(val); + synchronized (shortCache) + { + if (shortCache[val - MIN_CACHE] == null) + shortCache[val - MIN_CACHE] = new Short(val); + return shortCache[val - MIN_CACHE]; + } + } + + /** * Convert the specified <code>String</code> into a <code>Short</code>. * The <code>String</code> may represent decimal, hexadecimal, or * octal numbers. @@ -350,4 +385,13 @@ public final class Short extends Number implements Comparable { return compareTo((Short)o); } + + /** + * Reverse the bytes in val. + * @since 1.5 + */ + public static short reverseBytes(short val) + { + return (short) (((val >> 8) & 0xff) | ((val << 8) & 0xff00)); + } } |