[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cp-patches] [generics] Patch: FYI: primitive wrapper additions
From: |
Tom Tromey |
Subject: |
[cp-patches] [generics] Patch: FYI: primitive wrapper additions |
Date: |
09 Oct 2004 16:35:51 -0600 |
I'm checking this in on the generics branch.
This updates the primitive wrappers for 1.5.
There are some things I haven't done: Float and Double can format as
hex floats, and there are a lot of Character additions related to
Unicode 4 and support for surrogate pairs.
Tom
Index: ChangeLog
from Tom Tromey <address@hidden>
* java/lang/Void.java (TYPE): Changed type.
* java/lang/Character.java: Implement Comparable<Character>.
(TYPE): Changed type.
(SIZE): New field.
(MAX_CACHE, charCache): New fields.
(valueOf): New method.
(reverseBytes): Likewise.
* java/lang/Double.java: Implement Comparable<Double>.
(TYPE): Changed type.
(SIZE): New field.
(valueOf): New method.
* java/lang/Float.java: Implement Comparable<Float>
(TYPE): Changed type.
(SIZE): New field.
(valueOf): New method.
* java/lang/Short.java: Implement Comparable<Short>.
(TYPE): Changed type.
(MIN_CACHE, MAX_CACHE, shortCache): New fields.
(valueOf): New method.
(reverseBytes): Likewise.
* java/lang/Byte.java: Implement Comparable<Byte>.
(TYPE): Changed type.
(SIZE): New field.
(byteCache): Likewise.
(valueOf): New method.
* java/lang/Boolean.java (TYPE): Changed type.
* java/lang/Long.java (TYPE): Changed type.
(SIZE): New field.
(valueOf): New method.
(bitCount, rotateLeft, rotateRight, highestOneBit,
numberOfLeadingZeros, lowestOneBit, numberOfTrailingZeros,
signum, reverseBytes, reverse): New methods.
Implement Comparable<Long>.
* java/lang/Integer.java: Implement Comparable<Integer>.
(SIZE): New field.
(intCache): Likewise.
(MIN_CACHE, MAX_CACHE): Likewise.
(valueOf): New method.
(bitCount, rotateLeft, rotateRight, highestOneBit,
numberOfLeadingZeros, lowestOneBit, numberOfTrailingZeros,
signum, reverseBytes, reverse): New methods.
(TYPE): Changed type.
Index: java/lang/Boolean.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Boolean.java,v
retrieving revision 1.20.2.1
diff -u -r1.20.2.1 Boolean.java
--- java/lang/Boolean.java 7 Aug 2004 00:27:06 -0000 1.20.2.1
+++ java/lang/Boolean.java 9 Oct 2004 22:37:34 -0000
@@ -78,7 +78,7 @@
*
* @since 1.1
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('Z');
+ public static final Class<Boolean> TYPE =
VMClassLoader.getPrimitiveClass('Z');
/**
* The immutable value of this Boolean.
Index: java/lang/Byte.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Byte.java,v
retrieving revision 1.22
diff -u -r1.22 Byte.java
--- java/lang/Byte.java 17 Apr 2004 17:08:22 -0000 1.22
+++ java/lang/Byte.java 9 Oct 2004 22:37:34 -0000
@@ -1,5 +1,5 @@
/* Byte.java -- object wrapper for byte
- Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 2001, 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -52,7 +52,7 @@
* @since 1.1
* @status updated to 1.4
*/
-public final class Byte extends Number implements Comparable
+public final class Byte extends Number implements Comparable<Byte>
{
/**
* Compatible with JDK 1.1+.
@@ -75,7 +75,18 @@
* The primitive type <code>byte</code> is represented by this
* <code>Class</code> object.
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('B');
+ public static final Class<Byte> TYPE = VMClassLoader.getPrimitiveClass('B');
+
+ /**
+ * The number of bits needed to represent a <code>byte</code>.
+ * @since 1.5
+ */
+ public static final int SIZE = 8;
+
+ // This caches Byte values, and is used by boxing conversions via
+ // valueOf(). We're required to cache all possible values here.
+ private static Byte[] byteCache = new Byte[MAX_VALUE - MIN_VALUE + 1];
+
/**
* The immutable value of this Byte.
@@ -192,6 +203,24 @@
}
/**
+ * Returns a <code>Byte</code> object wrapping the value.
+ * In contrast to the <code>Byte</code> constructor, this method
+ * will cache some values. It is used by boxing conversion.
+ *
+ * @param val the value to wrap
+ * @return the <code>Byte</code>
+ */
+ public static Byte valueOf(byte val)
+ {
+ synchronized (byteCache)
+ {
+ if (byteCache[val - MIN_CACHE] == null)
+ byteCache[val - MIN_CACHE] = new Byte(val);
+ return byteCache[val - MIN_CACHE];
+ }
+ }
+
+ /**
* Convert the specified <code>String</code> into a <code>Byte</code>.
* The <code>String</code> may represent decimal, hexadecimal, or
* octal numbers.
Index: java/lang/Character.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Character.java,v
retrieving revision 1.33
diff -u -r1.33 Character.java
--- java/lang/Character.java 8 Apr 2003 03:39:45 -0000 1.33
+++ java/lang/Character.java 9 Oct 2004 22:37:35 -0000
@@ -1,5 +1,5 @@
/* java.lang.Character -- Wrapper class for char, and Unicode subsets
- Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2001, 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -66,7 +66,7 @@
* @since 1.0
* @status updated to 1.4
*/
-public final class Character implements Serializable, Comparable
+public final class Character implements Serializable, Comparable<Character>
{
/**
* A subset of Unicode blocks.
@@ -1030,7 +1030,19 @@
*
* @since 1.1
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('C');
+ public static final Class<Character> TYPE =
VMClassLoader.getPrimitiveClass('C');
+
+ /**
+ * The number of bits needed to represent a <code>char</code>.
+ * @since 1.5
+ */
+ public static final int SIZE = 16;
+
+ // This caches some Character values, and is used by boxing
+ // conversions via valueOf(). We must cache at least 0..127;
+ // this constant controls how much we actually cache.
+ private static final int MAX_CACHE = 127;
+ private static Character[] charCache = new Character[MAX_CACHE + 1];
/**
* Lu = Letter, Uppercase (Informative).
@@ -2249,4 +2261,33 @@
{
return compareTo((Character) o);
}
+
+ /**
+ * Returns an <code>Character</code> object wrapping the value.
+ * In contrast to the <code>Character</code> constructor, this method
+ * will cache some values. It is used by boxing conversion.
+ *
+ * @param val the value to wrap
+ * @return the <code>Character</code>
+ */
+ public static Character valueOf(char val)
+ {
+ if (val > MAX_CACHE)
+ return new Character(val);
+ synchronized (charCache)
+ {
+ if (charCache[val - MIN_CACHE] == null)
+ charCache[val - MIN_CACHE] = new Character(val);
+ return charCache[val - MIN_CACHE];
+ }
+ }
+
+ /**
+ * Reverse the bytes in val.
+ * @since 1.5
+ */
+ public static char reverseBytes(char val)
+ {
+ return (char) (((val >> 8) & 0xff) | ((val << 8) & 0xff00));
+ }
} // class Character
Index: java/lang/Double.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Double.java,v
retrieving revision 1.34
diff -u -r1.34 Double.java
--- java/lang/Double.java 17 Apr 2004 17:08:22 -0000 1.34
+++ java/lang/Double.java 9 Oct 2004 22:37:35 -0000
@@ -1,5 +1,5 @@
/* Double.java -- object wrapper for double
- Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
+ Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -53,7 +53,7 @@
* @since 1.0
* @status updated to 1.4
*/
-public final class Double extends Number implements Comparable
+public final class Double extends Number implements Comparable<Double>
{
/**
* Compatible with JDK 1.0+.
@@ -93,7 +93,13 @@
* <code>Class</code> object.
* @since 1.1
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('D');
+ public static final Class<Double> TYPE =
VMClassLoader.getPrimitiveClass('D');
+
+ /**
+ * The number of bits needed to represent a <code>double</code>.
+ * @since 1.5
+ */
+ public static final int SIZE = 64;
/**
* The immutable value of this Double.
@@ -195,6 +201,20 @@
}
/**
+ * Returns a <code>Double</code> object wrapping the value.
+ * In contrast to the <code>Double</code> constructor, this method
+ * may cache some values. It is used by boxing conversion.
+ *
+ * @param val the value to wrap
+ * @return the <code>Double</code>
+ */
+ public static Double valueOf(double val)
+ {
+ // We don't actually cache, but we could.
+ return new Double(val);
+ }
+
+ /**
* Parse the specified <code>String</code> as a <code>double</code>. The
* extended BNF grammar is as follows:<br>
* <pre>
Index: java/lang/Float.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Float.java,v
retrieving revision 1.28
diff -u -r1.28 Float.java
--- java/lang/Float.java 17 Apr 2004 17:08:22 -0000 1.28
+++ java/lang/Float.java 9 Oct 2004 22:37:35 -0000
@@ -1,5 +1,5 @@
/* Float.java -- object wrapper for float
- Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
+ Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -52,7 +52,7 @@
* @since 1.0
* @status updated to 1.4
*/
-public final class Float extends Number implements Comparable
+public final class Float extends Number implements Comparable<Float>
{
/**
* Compatible with JDK 1.0+.
@@ -91,7 +91,13 @@
* <code>Class</code> object.
* @since 1.1
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('F');
+ public static final Class<Float> TYPE = VMClassLoader.getPrimitiveClass('F');
+
+ /**
+ * The number of bits needed to represent a <code>float</code>.
+ * @since 1.5
+ */
+ public static final int SIZE = 32;
/**
* The immutable value of this Float.
@@ -192,6 +198,20 @@
}
/**
+ * Returns a <code>Float</code> object wrapping the value.
+ * In contrast to the <code>Float</code> constructor, this method
+ * may cache some values. It is used by boxing conversion.
+ *
+ * @param val the value to wrap
+ * @return the <code>Float</code>
+ */
+ public static Float valueOf(float val)
+ {
+ // We don't actually cache, but we could.
+ return new Float(val);
+ }
+
+ /**
* Parse the specified <code>String</code> as a <code>float</code>. The
* extended BNF grammar is as follows:<br>
* <pre>
Index: java/lang/Integer.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Integer.java,v
retrieving revision 1.29
diff -u -r1.29 Integer.java
--- java/lang/Integer.java 17 Apr 2004 17:08:22 -0000 1.29
+++ java/lang/Integer.java 9 Oct 2004 22:37:35 -0000
@@ -1,5 +1,5 @@
/* Integer.java -- object wrapper for int
- Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2001, 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -52,7 +52,7 @@
* @since 1.0
* @status updated to 1.4
*/
-public final class Integer extends Number implements Comparable
+public final class Integer extends Number implements Comparable<Integer>
{
/**
* Compatible with JDK 1.0.2+.
@@ -76,7 +76,20 @@
* <code>Class</code> object.
* @since 1.1
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('I');
+ public static final Class<Integer> TYPE =
VMClassLoader.getPrimitiveClass('I');
+
+ /**
+ * The number of bits needed to represent an <code>int</code>.
+ * @since 1.5
+ */
+ public static final int SIZE = 32;
+
+ // This caches some Integer 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 Integer[] intCache = new Integer[MAX_CACHE - MIN_CACHE + 1];
/**
* The immutable value of this Integer.
@@ -279,6 +292,26 @@
}
/**
+ * Returns an <code>Integer</code> object wrapping the value.
+ * In contrast to the <code>Integer</code> constructor, this method
+ * will cache some values. It is used by boxing conversion.
+ *
+ * @param val the value to wrap
+ * @return the <code>Integer</code>
+ */
+ public static Integer valueOf(int val)
+ {
+ if (val < MIN_CACHE || val > MAX_CACHE)
+ return new Integer(val);
+ synchronized (intCache)
+ {
+ if (intCache[val - MIN_CACHE] == null)
+ intCache[val - MIN_CACHE] = new Integer(val);
+ return intCache[val - MIN_CACHE];
+ }
+ }
+
+ /**
* Return the value of this <code>Integer</code> as a <code>byte</code>.
*
* @return the byte value
@@ -507,6 +540,137 @@
}
/**
+ * Return the number of bits set in x.
+ * @param x value to examine
+ * @since 1.5
+ */
+ public static int bitCount(int x)
+ {
+ // Successively collapse alternating bit groups into a sum.
+ x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
+ x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
+ x = ((x >> 4) & 0x0f0f0f0f) + (x & 0x0f0f0f0f);
+ x = ((x >> 8) & 0x00ff00ff) + (x & 0x00ff00ff);
+ return ((x >> 16) & 0x0000ffff) + (x & 0x0000ffff);
+ }
+
+ /**
+ * Rotate x to the left by distance bits.
+ * @param x the value to rotate
+ * @param distance the number of bits by which to rotate
+ * @since 1.5
+ */
+ public static int rotateLeft(int x, int distance)
+ {
+ // This trick works because the shift operators implicitly mask
+ // the shift count.
+ return (x << distance) | (x >>> - distance);
+ }
+
+ /**
+ * Rotate x to the right by distance bits.
+ * @param x the value to rotate
+ * @param distance the number of bits by which to rotate
+ * @since 1.5
+ */
+ public static int rotateRight(int x, int distance)
+ {
+ // This trick works because the shift operators implicitly mask
+ // the shift count.
+ return (x << - distance) | (x >>> distance);
+ }
+
+ /**
+ * Find the highest set bit in value, and return a new value
+ * with only that bit set.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static int highestOneBit(int value)
+ {
+ value |= value >>> 1;
+ value |= value >>> 2;
+ value |= value >>> 4;
+ value |= value >>> 8;
+ value |= value >>> 16;
+ return value ^ (value >>> 1);
+ }
+
+ /**
+ * Return the number of leading zeros in value.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static int numberOfLeadingZeros(int value)
+ {
+ value |= value >>> 1;
+ value |= value >>> 2;
+ value |= value >>> 4;
+ value |= value >>> 8;
+ value |= value >>> 16;
+ return bitCount(~value);
+ }
+
+ /**
+ * Find the lowest set bit in value, and return a new value
+ * with only that bit set.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static int lowestOneBit(int value)
+ {
+ // Classic assembly trick.
+ return value & - value;
+ }
+
+ /**
+ * Find the number of trailing zeros in value.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static int numberOfTrailingZeros(int value)
+ {
+ return bitCount((value & -value) - 1);
+ }
+
+ /**
+ * Return 1 if x is positive, -1 if it is negative, and 0 if it is
+ * zero.
+ * @param x the value to examine
+ * @since 1.5
+ */
+ public static int signum(int x)
+ {
+ return x < 0 ? -1 : (x > 0 ? 1 : 0);
+ }
+
+ /**
+ * Reverse the bytes in val.
+ * @since 1.5
+ */
+ public static int reverseBytes(int val)
+ {
+ return ( ((val >> 24) & 0xff)
+ | ((val >> 8) & 0xff00)
+ | ((val << 8) & 0xff0000)
+ | ((val << 24) & 0xff000000));
+ }
+
+ /**
+ * Reverse the bits in val.
+ * @since 1.5
+ */
+ public static int reverse(int val)
+ {
+ // Successively swap alternating bit groups.
+ x = ((x >> 1) & 0x55555555) + ((x << 1) & ~0x55555555);
+ x = ((x >> 2) & 0x33333333) + ((x << 2) & ~0x33333333);
+ x = ((x >> 4) & 0x0f0f0f0f) + ((x << 4) & ~0x0f0f0f0f);
+ x = ((x >> 8) & 0x00ff00ff) + ((x << 8) & ~0x00ff00ff);
+ return ((x >> 16) & 0x0000ffff) + ((x << 16) & ~0x0000ffff);
+ }
+
+ /**
* Helper for converting unsigned numbers to String.
*
* @param num the number
Index: java/lang/Long.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Long.java,v
retrieving revision 1.19
diff -u -r1.19 Long.java
--- java/lang/Long.java 17 Apr 2004 17:08:22 -0000 1.19
+++ java/lang/Long.java 9 Oct 2004 22:37:35 -0000
@@ -1,5 +1,5 @@
/* Long.java -- object wrapper for long
- Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2001, 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -52,7 +52,7 @@
* @since 1.0
* @status updated to 1.4
*/
-public final class Long extends Number implements Comparable
+public final class Long extends Number implements Comparable<Long>
{
/**
* Compatible with JDK 1.0.2+.
@@ -76,7 +76,13 @@
* <code>Class</code> object.
* @since 1.1
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J');
+ public static final Class<Long> TYPE = VMClassLoader.getPrimitiveClass ('J');
+
+ /**
+ * The number of bits needed to represent a <code>long</code>.
+ * @since 1.5
+ */
+ public static final int SIZE = 64;
/**
* The immutable value of this Long.
@@ -282,6 +288,19 @@
}
/**
+ * Returns a <code>Long</code> object wrapping the value.
+ *
+ * @param val the value to wrap
+ * @return the <code>Long</code>
+ */
+ public static synchronized Long valueOf(long val)
+ {
+ // We aren't required to cache here. We could, though perhaps we
+ // ought to consider that as an empirical question.
+ return new Long(val);
+ }
+
+ /**
* Convert the specified <code>String</code> into a <code>Long</code>.
* The <code>String</code> may represent decimal, hexadecimal, or
* octal numbers.
@@ -511,6 +530,137 @@
return compareTo((Long) o);
}
+
+ /**
+ * Return the number of bits set in x.
+ * @param x value to examine
+ * @since 1.5
+ */
+ public static int bitCount(long x)
+ {
+ // Successively collapse alternating bit groups into a sum.
+ x = ((x >> 1) & 0x5555555555555555L) + (x & 0x5555555555555555L);
+ x = ((x >> 2) & 0x3333333333333333L) + (x & 0x3333333333333333L);
+ int v = (int) ((x >>> 32) + x);
+ v = ((v >> 4) & 0x0f0f0f0f) + (v & 0x0f0f0f0f);
+ v = ((v >> 8) & 0x00ff00ff) + (v & 0x00ff00ff);
+ return ((v >> 16) & 0x0000ffff) + (v & 0x0000ffff);
+ }
+
+ /**
+ * Rotate x to the left by distance bits.
+ * @param x the value to rotate
+ * @param distance the number of bits by which to rotate
+ * @since 1.5
+ */
+ public static long rotateLeft(long x, int distance)
+ {
+ // This trick works because the shift operators implicitly mask
+ // the shift count.
+ return (x << distance) | (x >>> - distance);
+ }
+
+ /**
+ * Rotate x to the right by distance bits.
+ * @param x the value to rotate
+ * @param distance the number of bits by which to rotate
+ * @since 1.5
+ */
+ public static int rotateRight(long x, int distance)
+ {
+ // This trick works because the shift operators implicitly mask
+ // the shift count.
+ return (x << - distance) | (x >>> distance);
+ }
+
+ /**
+ * Find the highest set bit in value, and return a new value
+ * with only that bit set.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static long highestOneBit(long value)
+ {
+ value |= value >>> 1;
+ value |= value >>> 2;
+ value |= value >>> 4;
+ value |= value >>> 8;
+ value |= value >>> 16;
+ value |= value >>> 32;
+ return value ^ (value >>> 1);
+ }
+
+ /**
+ * Return the number of leading zeros in value.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static int numberOfLeadingZeros(long value)
+ {
+ value |= value >>> 1;
+ value |= value >>> 2;
+ value |= value >>> 4;
+ value |= value >>> 8;
+ value |= value >>> 16;
+ value |= value >>> 32;
+ return bitCount(~value);
+ }
+
+ /**
+ * Find the lowest set bit in value, and return a new value
+ * with only that bit set.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static long lowestOneBit(long value)
+ {
+ // Classic assembly trick.
+ return value & - value;
+ }
+
+ /**
+ * Find the number of trailing zeros in value.
+ * @param value the value to examine
+ * @since 1.5
+ */
+ public static int numberOfTrailingZeros(long value)
+ {
+ return bitCount((value & -value) - 1);
+ }
+
+ /**
+ * Return 1 if x is positive, -1 if it is negative, and 0 if it is
+ * zero.
+ * @param x the value to examine
+ * @since 1.5
+ */
+ public static int signum(long x)
+ {
+ return x < 0 ? -1 : (x > 0 ? 1 : 0);
+ }
+
+ /**
+ * Reverse the bytes in val.
+ * @since 1.5
+ */
+ public static long reverseBytes(long val)
+ {
+ int hi = Integer.reverseBytes((int) val);
+ int lo = Integer.reverseBytes((int) (val >>> 32));
+ return (((long) hi) << 32) | lo;
+ }
+
+ /**
+ * Reverse the bits in val.
+ * @since 1.5
+ */
+ public static long reverse(long val)
+ {
+ int hi = Integer.reverse((int) val);
+ int lo = Integer.reverse((int) (val >>> 32));
+ return (((long) hi) << 32) | lo;
+ }
+
/**
* Helper for converting unsigned numbers to String.
*
Index: java/lang/Short.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Short.java,v
retrieving revision 1.15
diff -u -r1.15 Short.java
--- java/lang/Short.java 12 Apr 2004 14:32:59 -0000 1.15
+++ java/lang/Short.java 9 Oct 2004 22:37:35 -0000
@@ -1,5 +1,5 @@
/* Short.java -- object wrapper for short
- Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 2001, 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -51,7 +51,7 @@
* @since 1.1
* @status updated to 1.4
*/
-public final class Short extends Number implements Comparable
+public final class Short extends Number implements Comparable<Short>
{
/**
* Compatible with JDK 1.1+.
@@ -74,7 +74,20 @@
* The primitive type <code>short</code> is represented by this
* <code>Class</code> object.
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('S');
+ public static final Class<Short> 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.
@@ -189,6 +202,26 @@
}
/**
+ * 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>
+ */
+ 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 +383,13 @@
{
return compareTo((Short)o);
}
+
+ /**
+ * Reverse the bytes in val.
+ * @since 1.5
+ */
+ public static short reverseBytes(int val)
+ {
+ return (short) (((val >> 8) & 0xff) | ((val << 8) & 0xff00));
+ }
}
Index: java/lang/Void.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Void.java,v
retrieving revision 1.14
diff -u -r1.14 Void.java
--- java/lang/Void.java 24 Feb 2002 04:25:16 -0000 1.14
+++ java/lang/Void.java 9 Oct 2004 22:37:35 -0000
@@ -1,5 +1,5 @@
/* Void.class - defines void.class
- Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2001, 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -57,7 +57,7 @@
* The return type <code>void</code> is represented by this
* <code>Class</code> object.
*/
- public static final Class TYPE = VMClassLoader.getPrimitiveClass('V');
+ public static final Class<Void> TYPE = VMClassLoader.getPrimitiveClass('V');
/**
* Void is non-instantiable.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [cp-patches] [generics] Patch: FYI: primitive wrapper additions,
Tom Tromey <=