harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r384228 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/Byte.java test/java/org/apache/harmony/tests/java/lang/AllTests.java test/java/org/apache/harmony/tests/java/lang/ByteTest.java
Date Wed, 08 Mar 2006 15:07:46 GMT
Author: tellison
Date: Wed Mar  8 07:07:44 2006
New Revision: 384228

URL: http://svn.apache.org/viewcvs?rev=384228&view=rev
Log:
Apply patch HARMONY-180 ([classlib][luni] Java 5 enhancements for java.lang.Byte)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Byte.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/AllTests.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Byte.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Byte.java?rev=384228&r1=384227&r2=384228&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Byte.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Byte.java
Wed Mar  8 07:07:44 2006
@@ -15,25 +15,42 @@
 
 package java.lang;
 
-
 /**
- * Bytes are objects (i.e. non-base types) which represent byte values.
+ * <p>Byte is the wrapper for the primitive type <code>byte</code>.</p>
+ * @since 1.0
  */
 public final class Byte extends Number implements Comparable {
-
+    //TODO Add Comparable<Byte> to implements when generics are supported.    
 	private static final long serialVersionUID = -7183698231559129828L;
 
 	/**
 	 * The value which the receiver represents.
 	 */
-	final byte value;
+	private final byte value;
 
 	/**
-	 * Most positive and most negative possible byte values.
-	 */
-	public static final byte MAX_VALUE = (byte) 0x7F;
-
-	public static final byte MIN_VALUE = (byte) 0x80;
+     * <p>
+     * Constant for the maximum <code>byte</code> value, 2<sup>7</sup>-1.
+     * </p>
+     */
+    public static final byte MAX_VALUE = (byte) 0x7F;
+
+    /**
+     * <p>
+     * Constant for the minimum <code>byte</code> value, -2<sup>7</sup>.
+     * </p>
+     */
+    public static final byte MIN_VALUE = (byte) 0x80;
+    
+    /**
+     * <p>
+     * Constant for the number of bits to represent a <code>byte</code> in
+     * two's compliment form.
+     * </p>
+     * 
+     * @since 1.5
+     */
+    public static final int SIZE = 8;
 
 	/**
 	 * The java.lang.Class that represents this class.
@@ -42,6 +59,13 @@
 
 	// Note: This can't be set to "byte.class", since *that* is
 	// defined to be "java.lang.Byte.TYPE";
+    
+    /**
+     * <p>
+     * A cache of instances used by {@link #valueOf(byte)} and auto-boxing.
+     * </p>
+     */
+    private static final Byte[] CACHE = new Byte[256];
 
 	/**
 	 * Constructs a new instance of the receiver which represents the byte
@@ -99,7 +123,7 @@
 		int intValue = Integer.decode(string).intValue();
 		byte result = (byte) intValue;
 		if (result == intValue)
-			return new Byte(result);
+			return valueOf(result);
 		throw new NumberFormatException();
 	}
 
@@ -255,7 +279,7 @@
 	 *             if the argument could not be parsed as a byte quantity.
 	 */
 	public static Byte valueOf(String string) throws NumberFormatException {
-		return new Byte(parseByte(string));
+		return valueOf(parseByte(string));
 	}
 
 	/**
@@ -274,6 +298,22 @@
 	 */
 	public static Byte valueOf(String string, int radix)
 			throws NumberFormatException {
-		return new Byte(parseByte(string, radix));
+		return valueOf(parseByte(string, radix));
 	}
+    
+    /**
+     * <p>Returns a <code>Byte</code> instance for the <code>byte</code>
value passed.
+     * This method is preferred over the constructor, as this method may maintain a cache
+     * of instances.</p>
+     * @param b The byte value.
+     * @return A <code>Byte</code> instance.
+     * @since 1.5
+     */
+    public static Byte valueOf(byte b) {
+        synchronized (CACHE) {
+            int idx = b - MIN_VALUE;
+            Byte result = CACHE[idx];
+            return (result == null ? CACHE[idx] = new Byte(b) : result);
+        }
+    }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/AllTests.java?rev=384228&r1=384227&r2=384228&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/AllTests.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/AllTests.java
Wed Mar  8 07:07:44 2006
@@ -34,6 +34,7 @@
 		suite.addTestSuite(DoubleTest.class);
 		suite.addTestSuite(StringBuilderTest.class);
 		suite.addTestSuite(BooleanTest.class);
+		suite.addTestSuite(ByteTest.class);
 		//$JUnit-END$
 		return suite;
 	}

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java?rev=384228&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java
Wed Mar  8 07:07:44 2006
@@ -0,0 +1,37 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.lang;
+
+import junit.framework.TestCase;
+
+public class ByteTest extends TestCase {
+
+	/**
+	 * @tests java.lang.Byte#valueOf(byte)
+	 */
+	public void test_valueOfB() {
+		assertEquals(new Byte(Byte.MIN_VALUE), Byte.valueOf(Byte.MIN_VALUE));
+		assertEquals(new Byte(Byte.MAX_VALUE), Byte.valueOf(Byte.MAX_VALUE));
+		assertEquals(new Byte((byte) 0), Byte.valueOf((byte) 0));
+
+		byte b = Byte.MIN_VALUE + 1;
+		while (b < Byte.MAX_VALUE) {
+			assertEquals(new Byte(b), Byte.valueOf(b));
+			assertSame(Byte.valueOf(b), Byte.valueOf(b));
+			b++;
+		}
+	}
+}



Mime
View raw message