harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r386352 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/Short.java test/java/org/apache/harmony/tests/java/lang/AllTests.java test/java/org/apache/harmony/tests/java/lang/ShortTest.java
Date Thu, 16 Mar 2006 15:15:56 GMT
Author: tellison
Date: Thu Mar 16 07:15:54 2006
New Revision: 386352

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

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ShortTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.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/Short.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java?rev=386352&r1=386351&r2=386352&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java
Thu Mar 16 07:15:54 2006
@@ -15,25 +15,46 @@
 
 package java.lang;
 
-
 /**
- * Shorts are objects (non-base types) which represent short values.
+ * <p>
+ * Short is the wrapper for the primitive type <code>short</code>.
+ * </p>
+ * 
+ * @see java.lang.Number
+ * @since 1.1
  */
 public final class Short extends Number implements Comparable {
-
+    //TODO Add Comparable<Short> to implements when generics are supported.
 	private static final long serialVersionUID = 7515723908773894738L;
 
 	/**
-	 * The value which the receiver represents.
-	 */
-	final short value;
-
-	/**
-	 * Most positive and most negative possible short values.
-	 */
-	public static final short MAX_VALUE = (short) 0x7FFF;
-
-	public static final short MIN_VALUE = (short) 0x8000;
+     * The value which the receiver represents.
+     */
+    private final short value;
+
+    /**
+     * <p>
+     * Constant for the maximum <code>short</code> value, 2<sup>15</sup>-1.
+     * </p>
+     */
+    public static final short MAX_VALUE = (short) 0x7FFF;
+
+    /**
+     * <p>
+     * Constant for the minimum <code>short</code> value, -2<sup>15</sup>.
+     * </p>
+     */
+    public static final short MIN_VALUE = (short) 0x8000;
+
+    /**
+     * <p>
+     * Constant for the number of bits to represent a <code>short</code> in
+     * two's compliment form.
+     * </p>
+     * 
+     * @since 1.5
+     */
+    public static final int SIZE = 16;
 
 	/**
 	 * The java.lang.Class that represents this class.
@@ -43,6 +64,13 @@
 	// Note: This can't be set to "short.class", since *that* is
 	// defined to be "java.lang.Short.TYPE";
 
+    /**
+     * <p>
+     * A cache of instances used by {@link #valueOf(short)} and auto-boxing.
+     * </p>
+     */
+    private static final Short[] CACHE = new Short[256];
+    
 	/**
 	 * Constructs a new instance of this class given a string.
 	 * 
@@ -99,7 +127,7 @@
 		int intValue = Integer.decode(string).intValue();
 		short result = (short) intValue;
 		if (result == intValue)
-			return new Short(result);
+			return valueOf(result);
 		throw new NumberFormatException();
 	}
 
@@ -251,7 +279,7 @@
 	 *                if the argument could not be parsed as a short quantity.
 	 */
 	public static Short valueOf(String string) throws NumberFormatException {
-		return new Short(parseShort(string));
+		return valueOf(parseShort(string));
 	}
 
 	/**
@@ -270,6 +298,43 @@
 	 */
 	public static Short valueOf(String string, int radix)
 			throws NumberFormatException {
-		return new Short(parseShort(string, radix));
+		return valueOf(parseShort(string, radix));
 	}
+    
+    /**
+     * <p>
+     * Reverses the bytes of a <code>short</code>.
+     * </p>
+     * 
+     * @param s The <code>short</code> to reverse.
+     * @return The reversed value.
+     * @since 1.5
+     */
+    public static short reverseBytes(short s) {
+        int high = (s >> 8) & 0xFF;
+        int low = (s & 0xFF) << 8;
+        return (short) (low | high);
+    }
+
+    /**
+     * <p>
+     * Returns a <code>Short</code> instance for the <code>short</code>
+     * value passed. This method is preferred over the constructor, as this
+     * method may maintain a cache of instances.
+     * </p>
+     * 
+     * @param s The short value.
+     * @return A <code>Short</code> instance.
+     * @since 1.5
+     */
+    public static Short valueOf(short s) {
+        if (s < -128 || s > 127) {
+            return new Short(s);
+        }
+        synchronized (CACHE) {
+            int idx = 128 + s; // 128 matches a cache size of 256
+            Short result = CACHE[idx];
+            return (result == null ? CACHE[idx] = new Short(s) : 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=386352&r1=386351&r2=386352&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
Thu Mar 16 07:15:54 2006
@@ -36,6 +36,7 @@
 		suite.addTestSuite(StringBufferTest.class);
 		suite.addTestSuite(SecurityManagerTest.class);
 		suite.addTestSuite(DoubleTest.class);
+		suite.addTestSuite(ShortTest.class);
 		//$JUnit-END$
 		return suite;
 	}

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ShortTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ShortTest.java?rev=386352&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ShortTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/ShortTest.java
Thu Mar 16 07:15:54 2006
@@ -0,0 +1,339 @@
+/* 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 ShortTest extends TestCase {
+
+	/**
+	 * @tests java.lang.Short#valueOf(byte)
+	 */
+	public void test_valueOfS() {
+		assertEquals(new Short(Short.MIN_VALUE), Short.valueOf(Short.MIN_VALUE));
+		assertEquals(new Short(Short.MAX_VALUE), Short.valueOf(Short.MAX_VALUE));
+		assertEquals(new Short((short) 0), Short.valueOf((short) 0));
+
+		short s = -128;
+		while (s < 128) {
+			assertEquals(new Short(s), Short.valueOf(s));
+			assertSame(Short.valueOf(s), Short.valueOf(s));
+			s++;
+		}
+	}
+    
+    /**
+     * @tests java.lang.Short#hashCode()
+     */
+    public void test_hashCode() {
+        assertEquals(1, new Short((short)1).hashCode());
+        assertEquals(2, new Short((short)2).hashCode());
+        assertEquals(0, new Short((short)0).hashCode());
+        assertEquals(-1, new Short((short)-1).hashCode());
+    }
+
+    /**
+     * @tests java.lang.Short#Short(String)
+     */
+    public void test_ConstructorLjava_lang_String() {
+        assertEquals(new Short((short)0), new Short("0"));
+        assertEquals(new Short((short)1), new Short("1"));
+        assertEquals(new Short((short)-1), new Short("-1"));
+        
+        try {
+            new Short("0x1");
+            fail("Expected NumberFormatException with hex string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            new Short("9.2");
+            fail("Expected NumberFormatException with floating point string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            new Short("");
+            fail("Expected NumberFormatException with empty string.");
+        } catch (NumberFormatException e) {}
+        
+        try {
+            new Short(null);
+            fail("Expected NumberFormatException with null string.");
+        } catch (NumberFormatException e) {}
+    }
+
+    /**
+     * @tests java.lang.Short#Short(short)
+     */
+    public void test_ConstructorS() {
+        assertEquals(1, new Short((short)1).shortValue());
+        assertEquals(2, new Short((short)2).shortValue());
+        assertEquals(0, new Short((short)0).shortValue());
+        assertEquals(-1, new Short((short)-1).shortValue());
+    }
+
+    /**
+     * @tests java.lang.Short#byteValue()
+     */
+    public void test_booleanValue() {
+        assertEquals(1, new Short((short)1).byteValue());    
+        assertEquals(2, new Short((short)2).byteValue());
+        assertEquals(0, new Short((short)0).byteValue());
+        assertEquals(-1, new Short((short)-1).byteValue());
+    }
+
+    /**
+     * @tests java.lang.Short#equals(Object)
+     */
+    public void test_equalsLjava_lang_Object() {
+        assertEquals(new Short((short)0), Short.valueOf((short)0));
+        assertEquals(new Short((short)1), Short.valueOf((short)1));
+        assertEquals(new Short((short)-1), Short.valueOf((short)-1));
+        
+        Short fixture = new Short((short)25);
+        assertEquals(fixture, fixture);
+        assertFalse(fixture.equals(null));
+        assertFalse(fixture.equals("Not a Short"));
+    }
+
+    /**
+     * @tests java.lang.Short#toString()
+     */
+    public void test_toString() {
+        assertEquals("-1", new Short((short)-1).toString());
+        assertEquals("0", new Short((short)0).toString());
+        assertEquals("1", new Short((short)1).toString());
+        assertEquals("-1", new Short((short)0xFFFF).toString());
+    }
+
+    /**
+     * @tests java.lang.Short#toString(short)
+     */
+    public void test_toStringS() {
+        assertEquals("-1", Short.toString((short)-1));
+        assertEquals("0", Short.toString((short)0));
+        assertEquals("1", Short.toString((short)1));
+        assertEquals("-1", Short.toString((short)0xFFFF));
+    }
+
+    /**
+     * @tests java.lang.Short#valueOf(String)
+     */
+    public void test_valueOfLjava_lang_String() {
+        assertEquals(new Short((short)0), Short.valueOf("0"));
+        assertEquals(new Short((short)1), Short.valueOf("1"));
+        assertEquals(new Short((short)-1), Short.valueOf("-1"));
+        
+        try {
+            Short.valueOf("0x1");
+            fail("Expected NumberFormatException with hex string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.valueOf("9.2");
+            fail("Expected NumberFormatException with floating point string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.valueOf("");
+            fail("Expected NumberFormatException with empty string.");
+        } catch (NumberFormatException e) {}
+        
+        try {
+            Short.valueOf(null);
+            fail("Expected NumberFormatException with null string.");
+        } catch (NumberFormatException e) {}
+    }
+    
+    /**
+     * @tests java.lang.Short#valueOf(String,int)
+     */
+    public void test_valueOfLjava_lang_StringI() {
+        assertEquals(new Short((short)0), Short.valueOf("0", 10));
+        assertEquals(new Short((short)1), Short.valueOf("1", 10));
+        assertEquals(new Short((short)-1), Short.valueOf("-1", 10));
+        
+        //must be consistent with Character.digit()
+        assertEquals(Character.digit('1', 2), Short.valueOf("1", 2).byteValue());
+        assertEquals(Character.digit('F', 16), Short.valueOf("F", 16).byteValue());
+        
+        try {
+            Short.valueOf("0x1", 10);
+            fail("Expected NumberFormatException with hex string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.valueOf("9.2", 10);
+            fail("Expected NumberFormatException with floating point string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.valueOf("", 10);
+            fail("Expected NumberFormatException with empty string.");
+        } catch (NumberFormatException e) {}
+        
+        try {
+            Short.valueOf(null, 10);
+            fail("Expected NumberFormatException with null string.");
+        } catch (NumberFormatException e) {}
+    }
+    
+    /**
+     * @tests java.lang.Short#parseShort(String)
+     */
+    public void test_parseShortLjava_lang_String() {
+        assertEquals(0, Short.parseShort("0"));
+        assertEquals(1, Short.parseShort("1"));
+        assertEquals(-1, Short.parseShort("-1"));
+        
+        try {
+            Short.parseShort("0x1");
+            fail("Expected NumberFormatException with hex string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.parseShort("9.2");
+            fail("Expected NumberFormatException with floating point string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.parseShort("");
+            fail("Expected NumberFormatException with empty string.");
+        } catch (NumberFormatException e) {}
+        
+        try {
+            Short.parseShort(null);
+            fail("Expected NumberFormatException with null string.");
+        } catch (NumberFormatException e) {}
+    }
+    
+    /**
+     * @tests java.lang.Short#parseShort(String,int)
+     */
+    public void test_parseShortLjava_lang_StringI() {
+        assertEquals(0, Short.parseShort("0", 10));
+        assertEquals(1, Short.parseShort("1", 10));
+        assertEquals(-1, Short.parseShort("-1", 10));
+        
+        //must be consistent with Character.digit()
+        assertEquals(Character.digit('1', 2), Short.parseShort("1", 2));
+        assertEquals(Character.digit('F', 16), Short.parseShort("F", 16));
+        
+        try {
+            Short.parseShort("0x1", 10);
+            fail("Expected NumberFormatException with hex string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.parseShort("9.2", 10);
+            fail("Expected NumberFormatException with floating point string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.parseShort("", 10);
+            fail("Expected NumberFormatException with empty string.");
+        } catch (NumberFormatException e) {}
+        
+        try {
+            Short.parseShort(null, 10);
+            fail("Expected NumberFormatException with null string.");
+        } catch (NumberFormatException e) {}
+    }
+    
+    /**
+     * @tests java.lang.Short#decode(String)
+     */
+    public void test_decodeLjava_lang_String() {
+        assertEquals(new Short((short)0), Short.decode("0"));
+        assertEquals(new Short((short)1), Short.decode("1"));
+        assertEquals(new Short((short)-1), Short.decode("-1"));
+        assertEquals(new Short((short)0xF), Short.decode("0xF"));
+        assertEquals(new Short((short)0xF), Short.decode("#F"));
+        assertEquals(new Short((short)0xF), Short.decode("0XF"));
+        assertEquals(new Short((short)07), Short.decode("07"));
+        
+        try {
+            Short.decode("9.2");
+            fail("Expected NumberFormatException with floating point string.");
+        } catch (NumberFormatException e) {}
+
+        try {
+            Short.decode("");
+            fail("Expected NumberFormatException with empty string.");
+        } catch (NumberFormatException e) {}
+        
+        try {
+            Short.decode(null);
+            //undocumented NPE, but seems consistent across JREs
+            fail("Expected NullPointerException with null string.");
+        } catch (NullPointerException e) {}
+    }
+    
+    /**
+     * @tests java.lang.Short#doubleValue()
+     */
+    public void test_doubleValue() {
+        assertEquals(-1D, new Short((short)-1).doubleValue(), 0D);
+        assertEquals(0D, new Short((short)0).doubleValue(), 0D);
+        assertEquals(1D, new Short((short)1).doubleValue(), 0D);
+    }
+    
+    /**
+     * @tests java.lang.Short#floatValue()
+     */
+    public void test_floatValue() {
+        assertEquals(-1F, new Short((short)-1).floatValue(), 0F);
+        assertEquals(0F, new Short((short)0).floatValue(), 0F);
+        assertEquals(1F, new Short((short)1).floatValue(), 0F);
+    }
+    
+    /**
+     * @tests java.lang.Short#intValue()
+     */
+    public void test_intValue() {
+        assertEquals(-1, new Short((short)-1).intValue());
+        assertEquals(0, new Short((short)0).intValue());
+        assertEquals(1, new Short((short)1).intValue());
+    }
+    
+    /**
+     * @tests java.lang.Short#longValue()
+     */
+    public void test_longValue() {
+        assertEquals(-1L, new Short((short)-1).longValue());
+        assertEquals(0L, new Short((short)0).longValue());
+        assertEquals(1L, new Short((short)1).longValue());
+    }
+    
+    /**
+     * @tests java.lang.Short#shortValue()
+     */
+    public void test_shortValue() {
+        assertEquals(-1, new Short((short)-1).shortValue());
+        assertEquals(0, new Short((short)0).shortValue());
+        assertEquals(1, new Short((short)1).shortValue());
+    }
+    
+    /**
+     * @tests java.lang.Short#reverseBytes(short)
+     */
+    public void test_reverseBytesS() {
+        assertEquals((short)0xABCD, Short.reverseBytes((short)0xCDAB));
+        assertEquals((short)0x1234, Short.reverseBytes((short)0x3412));
+        assertEquals((short)0x0011, Short.reverseBytes((short)0x1100));
+        assertEquals((short)0x2002, Short.reverseBytes((short)0x0220));
+    }
+    
+}



Mime
View raw message