diff options
author | Paul Phillips <paulp@improving.org> | 2009-04-08 13:45:47 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-04-08 13:45:47 +0000 |
commit | 7a458d71310d747914a50715bfba1f20f521f502 (patch) | |
tree | 63212511ff358a84e617d1819811273fc118c477 /src | |
parent | 4e2f93073a79e49321fd06f0a69ac120b448ba0c (diff) | |
download | scala-7a458d71310d747914a50715bfba1f20f521f502.tar.gz scala-7a458d71310d747914a50715bfba1f20f521f502.tar.bz2 scala-7a458d71310d747914a50715bfba1f20f521f502.zip |
Modified all boxing methods to use java.lang.{P...
Modified all boxing methods to use java.lang.{Primitive}.valueOf
instead of instantiating a new object. Removed scala's boxing
cache since all boxing now goes through the JVM's cache.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/Console.scala | 24 | ||||
-rw-r--r-- | src/library/scala/Predef.scala | 26 | ||||
-rw-r--r-- | src/library/scala/mobile/Code.scala | 26 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxesRunTime.java | 129 |
4 files changed, 33 insertions, 172 deletions
diff --git a/src/library/scala/Console.scala b/src/library/scala/Console.scala index d2a06e7e4a..f22c272e4e 100644 --- a/src/library/scala/Console.scala +++ b/src/library/scala/Console.scala @@ -410,23 +410,13 @@ object Console { while (iter.hasNext) { res(i) = iter.next match { case x: Boolean => java.lang.Boolean.valueOf(x) - /** Should use java.lang.Byte.valueOf(Byte), but only available - * in Java 1.5 and above. */ - case x: Byte => new java.lang.Byte(x) - /** Should use java.lang.Short.valueOf(Short), but only available - * in Java 1.5 and above. */ - case x: Short => new java.lang.Short(x) - /** Should use java.lang.Character.valueOf(Char), but only available - * in Java 1.5 and above. */ - case x: Char => new java.lang.Character(x) - /** Should use java.lang.Integer.valueOf(Int), but only available - * in Java 1.5 and above. */ - case x: Int => new java.lang.Integer(x) - /** Should use java.lang.Long.valueOf(Long), but only available - * in Java 1.5 and above. */ - case x: Long => new java.lang.Long(x) - case x: Float => new java.lang.Float(x) - case x: Double => new java.lang.Double(x) + case x: Byte => java.lang.Byte.valueOf(x) + case x: Short => java.lang.Short.valueOf(x) + case x: Char => java.lang.Character.valueOf(x) + case x: Int => java.lang.Integer.valueOf(x) + case x: Long => java.lang.Long.valueOf(x) + case x: Float => java.lang.Float.valueOf(x) + case x: Double => java.lang.Double.valueOf(x) case x: Unit => "()" case x: AnyRef => x } diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 6662c5cf36..8b4c00bf9c 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -321,24 +321,14 @@ object Predef { implicit def float2double(x: Float): Double = x.toDouble - /** Should use java.lang.Byte.valueOf(Byte), but only available - * in Java 1.5 and above. */ - implicit def byte2Byte(x: Byte) = new java.lang.Byte(x) - /** Should use java.lang.Short.valueOf(Short), but only available - * in Java 1.5 and above. */ - implicit def short2Short(x: Short) = new java.lang.Short(x) - /** Should use java.lang.Character.valueOf(Char), but only available - * in Java 1.5 and above. */ - implicit def char2Character(x: Char) = new java.lang.Character(x) - /** Should use java.lang.Integer.valueOf(Int), but only available - * in Java 1.5 and above. */ - implicit def int2Integer(x: Int) = new java.lang.Integer(x) - /** Should use java.lang.Long.valueOf(Long), but only available - * in Java 1.5 and above. */ - implicit def long2Long(x: Long) = new java.lang.Long(x) - implicit def float2Float(x: Float) = new java.lang.Float(x) - implicit def double2Double(x: Double) = new java.lang.Double(x) - implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x) + implicit def byte2Byte(x: Byte) = java.lang.Byte.valueOf(x) + implicit def short2Short(x: Short) = java.lang.Short.valueOf(x) + implicit def char2Character(x: Char) = java.lang.Character.valueOf(x) + implicit def int2Integer(x: Int) = java.lang.Integer.valueOf(x) + implicit def long2Long(x: Long) = java.lang.Long.valueOf(x) + implicit def float2Float(x: Float) = java.lang.Float.valueOf(x) + implicit def double2Double(x: Double) = java.lang.Double.valueOf(x) + implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x) /** any array projection can be automatically converted into an array */ implicit def forceArrayProjection[A](x: Array.Projection[A]): Array[A] = x.force diff --git a/src/library/scala/mobile/Code.scala b/src/library/scala/mobile/Code.scala index ea8da7ce29..cd898e8a3e 100644 --- a/src/library/scala/mobile/Code.scala +++ b/src/library/scala/mobile/Code.scala @@ -160,24 +160,14 @@ class Code(clazz: java.lang.Class[_]) { ////////////////////// private functions /////////////////////// private def boxValue(value: Any) = value match { - /** Should use java.lang.Byte.valueOf(Byte), but only available - * in Java 1.5 and above. */ - case x: Byte => (new java.lang.Byte(x), java.lang.Byte.TYPE) - case x: Boolean => (java.lang.Boolean.valueOf(x), java.lang.Boolean.TYPE) - /** Should use java.lang.Character.valueOf(Char), but only available - * in Java 1.5 and above. */ - case x: Char => (new java.lang.Character(x), java.lang.Character.TYPE) - /** Should use java.lang.Short.valueOf(Short), but only available - * in Java 1.5 and above. */ - case x: Short => (new java.lang.Short(x), java.lang.Short.TYPE) - /** Should use java.lang.Integer.valueOf(Int), but only available - * in Java 1.5 and above. */ - case x: Int => (new java.lang.Integer(x), java.lang.Integer.TYPE) - /** Should use java.lang.Long.valueOf(Long), but only available - * in Java 1.5 and above. */ - case x: Long => (new java.lang.Long(x), java.lang.Long.TYPE) - case x: Float => (new java.lang.Float(x), java.lang.Float.TYPE) - case x: Double => (new java.lang.Double(x), java.lang.Double.TYPE) + case x: Byte => (java.lang.Byte.valueOf(x), java.lang.Byte.TYPE) + case x: Boolean => (java.lang.Boolean.valueOf(x), java.lang.Boolean.TYPE) + case x: Char => (java.lang.Character.valueOf(x), java.lang.Character.TYPE) + case x: Short => (java.lang.Short.valueOf(x), java.lang.Short.TYPE) + case x: Int => (java.lang.Integer.valueOf(x), java.lang.Integer.TYPE) + case x: Long => (java.lang.Long.valueOf(x), java.lang.Long.TYPE) + case x: Float => (java.lang.Float.valueOf(x), java.lang.Float.TYPE) + case x: Double => (java.lang.Double.valueOf(x), java.lang.Double.TYPE) case _ => val x = value.asInstanceOf[JObject] (x, x.getClass()) diff --git a/src/library/scala/runtime/BoxesRunTime.java b/src/library/scala/runtime/BoxesRunTime.java index 7b8d6bb672..966fbe8cd1 100644 --- a/src/library/scala/runtime/BoxesRunTime.java +++ b/src/library/scala/runtime/BoxesRunTime.java @@ -42,96 +42,36 @@ public class BoxesRunTime { /* BOXING ... BOXING ... BOXING ... BOXING ... BOXING ... BOXING ... BOXING ... BOXING */ - // for 2.8: change to valueOf methods. - - private static int charLowBound = 0; - private static int charUpBound = 255; - private static Character[] charCache = new Character[charUpBound - charLowBound + 1]; - - private static int byteLowBound = -128; - private static int byteUpBound = 127; - private static Byte[] byteCache = new Byte[byteUpBound - byteLowBound + 1]; - - private static int shortLowBound = -128; - private static int shortUpBound = 127; - private static Short[] shortCache = new Short[shortUpBound - shortLowBound + 1]; - - private static int intLowBound = -128; - private static int intUpBound = 1024; - private static Integer[] intCache = new Integer[intUpBound - intLowBound + 1]; - - private static int longLowBound = -128; - private static int longUpBound = 1024; - private static Long[] longCache = new Long[longUpBound - longLowBound + 1]; - - static { - int idx = 0; - while (idx <= charUpBound - charLowBound) { - charCache[idx] = new Character((char)(idx + charLowBound)); - idx = idx + 1; - } - idx = 0; - while (idx <= byteUpBound - byteLowBound) { - byteCache[idx] = new Byte((byte)(idx + byteLowBound)); - idx = idx + 1; - } - idx = 0; - while (idx <= shortUpBound - shortLowBound) { - shortCache[idx] = new Short((short)(idx + shortLowBound)); - idx = idx + 1; - } - idx = 0; - while (idx <= intUpBound - intLowBound) { - intCache[idx] = new Integer((int)(idx + intLowBound)); - idx = idx + 1; - } - idx = 0; - while (idx <= longUpBound - longLowBound) { - longCache[idx] = new Long((long)(idx + longLowBound)); - idx = idx + 1; - } - } - public static Boolean boxToBoolean(boolean b) { - return b ? Boolean.TRUE : Boolean.FALSE; + return Boolean.valueOf(b); } public static Character boxToCharacter(char c) { - if (c >= charLowBound && c <= charUpBound) - return charCache[(int)c - charLowBound]; - return new Character(c); + return Character.valueOf(c); } public static Byte boxToByte(byte b) { - if (b >= byteLowBound && b <= byteUpBound) - return byteCache[(int)b - byteLowBound]; - return new Byte(b); + return Byte.valueOf(b); } public static Short boxToShort(short s) { - if (s >= shortLowBound && s <= shortUpBound) - return shortCache[(int)s - shortLowBound]; - return new Short(s); + return Short.valueOf(s); } public static Integer boxToInteger(int i) { - if (i >= intLowBound && i <= intUpBound) - return intCache[(int)i - intLowBound]; - return new Integer(i); + return Integer.valueOf(i); } public static Long boxToLong(long l) { - if (l >= longLowBound && l <= longUpBound) - return longCache[(int)l - longLowBound]; - return new Long(l); + return Long.valueOf(l); } public static Float boxToFloat(float f) { - return new Float(f); + return Float.valueOf(f); } public static Double boxToDouble(double d) { - return new Double(d); + return Double.valueOf(d); } /* UNBOXING ... UNBOXING ... UNBOXING ... UNBOXING ... UNBOXING ... UNBOXING ... UNBOXING */ @@ -168,56 +108,6 @@ public class BoxesRunTime { return d == null ? 0.0d : ((Double)d).doubleValue(); } - /* - public static boolean unboxToBoolean(Object b) { - if (b == null) - throw new ClassCastException("null is no Boolean value"); - return ((Boolean)b).booleanValue(); - } - - public static char unboxToChar(Object c) { - if (c == null) - throw new ClassCastException("null is no Char value"); - return ((Character)c).charValue(); - } - - public static byte unboxToByte(Object b) { - if (b == null) - throw new ClassCastException("null is no Byte value"); - return ((Byte)b).byteValue(); - } - - public static short unboxToShort(Object s) { - if (s == null) - throw new ClassCastException("null is no Short value"); - return ((Short)s).shortValue(); - } - - public static int unboxToInt(Object i) { - if (i == null) - throw new ClassCastException("null is no Int value"); - return ((Integer)i).intValue(); - } - - public static long unboxToLong(Object l) { - if (l == null) - throw new ClassCastException("null is no Long value"); - return ((Long)l).longValue(); - } - - public static float unboxToFloat(Object f) { - if (f == null) - throw new ClassCastException("null is no Float value"); - return ((Float)f).floatValue(); - } - - public static double unboxToDouble(Object d) { - if (d == null) - throw new ClassCastException("null is no Double value"); - return ((Double)d).doubleValue(); - } - */ - /* COMPARISON ... COMPARISON ... COMPARISON ... COMPARISON ... COMPARISON ... COMPARISON */ /** A rich implementation of the <code>equals</code> method that overrides the @@ -256,7 +146,8 @@ public class BoxesRunTime { double bb = (bcode == CHAR) ? ((Character) b).charValue() : ((Number) b).doubleValue(); return aa == bb; } - return b.equals(a); + + return b.equals(a); } return false; } |