classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] Patch: FYI: merge some 1.5 bits to trunk


From: Tom Tromey
Subject: [cp-patches] Patch: FYI: merge some 1.5 bits to trunk
Date: 17 Sep 2005 15:49:25 -0600
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

I'm checking this in.

This merges some new methods in the primitive wrapper types from the
generics branch to the trunk.

Tom

Index: ChangeLog
from  Tom Tromey  <address@hidden>
        * java/lang/Short.java (SIZE): New constant.
        (MIN_CACHE, MAX_CACHE): Likewise.
        (shortCache): New field.
        (reverseBytes): New method.
        * java/lang/Long.java (SIZE): New constant.
        (valueOf): New method.
        (bitCount): New method.
        (rotateLeft): New method.
        (rotateRight): New method.
        (highestOneBit): New method.
        (numberOfLeadingZeros): New method.
        (lowestOneBit): New method.
        (numberOfTrailingZeros): New method.
        (signum): New method.
        (reverseBytes): New method.
        (reverse): New method.
        * java/lang/Float.java (SIZE): New constant.
        * java/lang/Double.java (SIZE): New constant.
        * java/lang/Character.java (SIZE): New constant.
        (MAX_CACHE): Likewise.
        (charCache): New field.
        (valueOf): New method.
        (reverseBytes): Likewise.
        * java/lang/Byte.java (SIZE): New constant.
        (byteCache): New field.
        (valueOf): New method.
        * java/lang/Boolean.java (parseBoolean): New method.

Index: java/lang/Boolean.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Boolean.java,v
retrieving revision 1.22
diff -u -r1.22 Boolean.java
--- java/lang/Boolean.java      2 Jul 2005 20:32:38 -0000       1.22
+++ java/lang/Boolean.java      17 Sep 2005 21:51:18 -0000
@@ -221,4 +221,16 @@
       return false;
     return "true".equalsIgnoreCase(System.getProperty(name));
   }
+
+  /**
+   * If the String argument is "true", ignoring case, return true.
+   * Otherwise, return false.
+   *
+   * @param b String to parse
+   * @since 1.5
+   */
+  public static boolean parseBoolean(String b)
+  {
+    return "true".equalsIgnoreCase(b) ? true : false;
+  }
 }
Index: java/lang/Byte.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Byte.java,v
retrieving revision 1.24
diff -u -r1.24 Byte.java
--- java/lang/Byte.java 2 Jul 2005 20:32:38 -0000       1.24
+++ java/lang/Byte.java 17 Sep 2005 21:51:18 -0000
@@ -50,7 +50,7 @@
  * @author Per Bothner
  * @author Eric Blake (address@hidden)
  * @since 1.1
- * @status updated to 1.4
+ * @status updated to 1.5
  */
 public final class Byte extends Number implements Comparable
 {
@@ -78,6 +78,16 @@
   public static final Class 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.
    *
    * @serial the wrapped byte
@@ -192,6 +202,26 @@
   }
 
   /**
+   * 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>
+   * 
+   * @since 1.5
+   */
+  public static Byte valueOf(byte val)
+  {
+    synchronized (byteCache)
+      {
+    if (byteCache[val - MIN_VALUE] == null)
+      byteCache[val - MIN_VALUE] = new Byte(val);
+    return byteCache[val - MIN_VALUE];
+      }
+  }
+
+ /**
    * 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.39
diff -u -r1.39 Character.java
--- java/lang/Character.java    16 Sep 2005 19:13:35 -0000      1.39
+++ java/lang/Character.java    17 Sep 2005 21:51:18 -0000
@@ -1034,6 +1034,18 @@
   public static final Class 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).
    *
    * @since 1.1
@@ -2314,6 +2326,37 @@
   public int compareTo(Object o)
   {
     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>
+   * 
+   * @since 1.5
+   */
+  public static Character valueOf(char val)
+  {
+    if (val > MAX_CACHE)
+      return new Character(val);
+    synchronized (charCache)
+      {
+    if (charCache[val - MIN_VALUE] == null)
+      charCache[val - MIN_VALUE] = new Character(val);
+    return charCache[val - MIN_VALUE];
+      }
+  }
+
+  /**
+   * Reverse the bytes in val.
+   * @since 1.5
+   */
+  public static char reverseBytes(char val)
+  {
+    return (char) (((val >> 8) & 0xff) | ((val << 8) & 0xff00));
   }
 
   /**
Index: java/lang/Double.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Double.java,v
retrieving revision 1.38
diff -u -r1.38 Double.java
--- java/lang/Double.java       13 Sep 2005 03:19:30 -0000      1.38
+++ java/lang/Double.java       17 Sep 2005 21:51:19 -0000
@@ -88,6 +88,12 @@
   public static final double NaN = 0.0 / 0.0;
 
   /**
+   * The number of bits needed to represent a <code>double</code>.
+   * @since 1.5
+   */
+  public static final int SIZE = 64;
+
+ /**
    * The primitive type <code>double</code> is represented by this
    * <code>Class</code> object.
    * @since 1.1
Index: java/lang/Float.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Float.java,v
retrieving revision 1.31
diff -u -r1.31 Float.java
--- java/lang/Float.java        2 Jul 2005 20:32:38 -0000       1.31
+++ java/lang/Float.java        17 Sep 2005 21:51:19 -0000
@@ -94,6 +94,12 @@
   public static final Class 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.
    *
    * @serial the wrapped float
Index: java/lang/Long.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Long.java,v
retrieving revision 1.21
diff -u -r1.21 Long.java
--- java/lang/Long.java 2 Jul 2005 20:32:38 -0000       1.21
+++ java/lang/Long.java 17 Sep 2005 21:51:19 -0000
@@ -50,7 +50,7 @@
  * @author Warren Levy
  * @author Eric Blake (address@hidden)
  * @since 1.0
- * @status updated to 1.4
+ * @status updated to 1.5
  */
 public final class Long extends Number implements Comparable
 {
@@ -79,6 +79,12 @@
   public static final Class 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.
    *
    * @serial the wrapped long
@@ -282,6 +288,21 @@
   }
 
   /**
+   * Returns a <code>Long</code> object wrapping the value.
+   *
+   * @param val the value to wrap
+   * @return the <code>Long</code>
+   * 
+   * @since 1.5
+   */
+  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.
@@ -509,6 +530,136 @@
   public int compareTo(Object o)
   {
     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 long 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;
   }
 
   /**
Index: java/lang/Short.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Short.java,v
retrieving revision 1.17
diff -u -r1.17 Short.java
--- java/lang/Short.java        2 Jul 2005 20:32:38 -0000       1.17
+++ java/lang/Short.java        17 Sep 2005 21:51:19 -0000
@@ -77,6 +77,19 @@
   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
@@ -349,5 +362,14 @@
   public int compareTo(Object o)
   {
     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));
   }
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]