harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r471795 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Scanner.java test/java/tests/api/java/util/ScannerTest.java
Date Mon, 06 Nov 2006 17:05:43 GMT
Author: pyang
Date: Mon Nov  6 09:05:42 2006
New Revision: 471795

URL: http://svn.apache.org/viewvc?view=rev&rev=471795
Log:
Apply second patch for HARMONY-2063( [classlib][luni]method hasNextXXX() of j.u.Scanner does
not cache the value.), with some tidy up to Scanner.java 

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java?view=diff&rev=471795&r1=471794&r2=471795
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
Mon Nov  6 09:05:42 2006
@@ -620,7 +620,7 @@
             String floatString = matcher.group();
             floatString = removeLocaleInfoFromFloat(floatString);
             try {
-                new BigDecimal(floatString);
+                cacheHasNextValue = new BigDecimal(floatString);
                 isBigDecimalValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -662,7 +662,7 @@
             String intString = matcher.group();
             intString = removeLocaleInfo(intString, DataType.INT);
             try {
-                new BigInteger(intString, radix);
+                cacheHasNextValue = new BigInteger(intString, radix);
                 isBigIntegerValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -718,7 +718,7 @@
             String intString = matcher.group();
             intString = removeLocaleInfo(intString, DataType.INT);
             try {
-                Byte.parseByte(intString, radix);
+                cacheHasNextValue = Byte.valueOf(intString, radix);
                 isByteValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -743,7 +743,7 @@
             String floatString = matcher.group();
             floatString = removeLocaleInfoFromFloat(floatString);
             try {
-                Double.parseDouble(floatString);
+                cacheHasNextValue = Double.valueOf(floatString);
                 isDoubleValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -768,7 +768,7 @@
             String floatString = matcher.group();
             floatString = removeLocaleInfoFromFloat(floatString);
             try {
-                Float.parseFloat(floatString);
+                cacheHasNextValue = Float.valueOf(floatString);
                 isFloatValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -810,7 +810,7 @@
             String intString = matcher.group();
             intString = removeLocaleInfo(intString, DataType.INT);
             try {
-            	cacheHasNextValue = Integer.parseInt(intString, radix);
+            	cacheHasNextValue = Integer.valueOf(intString, radix);
                 isIntValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -889,7 +889,7 @@
             String intString = matcher.group();
             intString = removeLocaleInfo(intString, DataType.INT);
             try {
-                Long.parseLong(intString, radix);
+                cacheHasNextValue = Long.valueOf(intString, radix);
                 isLongValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -932,7 +932,7 @@
             String intString = matcher.group();
             intString = removeLocaleInfo(intString, DataType.INT);
             try {
-            	cacheHasNextValue = Short.parseShort(intString, radix);
+            	cacheHasNextValue = Short.valueOf(intString, radix);
                 isShortValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -1089,6 +1089,13 @@
      *             BigDecimal
      */
     public BigDecimal nextBigDecimal() {
+    	checkClosed();
+		Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj instanceof BigDecimal) {
+			findStartIndex = cachehasNextIndex;
+			return (BigDecimal) obj;
+		}
         Pattern floatPattern = getFloatPattern();
         String floatString = next(floatPattern);
         floatString = removeLocaleInfoFromFloat(floatString);
@@ -1155,6 +1162,13 @@
      *             BigInteger, or it is out of range
      */
     public BigInteger nextBigInteger(int radix) {
+    	checkClosed();
+		Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj instanceof BigInteger) {
+			findStartIndex = cachehasNextIndex;
+			return (BigInteger) obj;
+		}
         Pattern integerPattern = getIntegerPattern(radix);
         String intString = next(integerPattern);
         intString = removeLocaleInfo(intString, DataType.INT);
@@ -1240,7 +1254,15 @@
      *             if the next token can not be translated into a valid byte
      *             value, or it is out of range
      */
+    @SuppressWarnings("boxing")
     public byte nextByte(int radix) {
+    	checkClosed();
+    	Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj instanceof Byte) {
+			findStartIndex = cachehasNextIndex;
+			return (Byte) obj;
+		}
         Pattern integerPattern = getIntegerPattern(radix);
         String intString = next(integerPattern);
         intString = removeLocaleInfo(intString, DataType.INT);
@@ -1283,7 +1305,15 @@
      *             if the next token can not be translated into a valid double
      *             value
      */
+    @SuppressWarnings("boxing")
     public double nextDouble() {
+    	checkClosed();
+    	Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj instanceof Double) {
+			findStartIndex = cachehasNextIndex;
+			return (Double) obj;
+		}
         Pattern floatPattern = getFloatPattern();
         String floatString = next(floatPattern);
         floatString = removeLocaleInfoFromFloat(floatString);
@@ -1324,7 +1354,15 @@
      *             if the next token can not be translated into a valid float
      *             value
      */
+    @SuppressWarnings("boxing")
     public float nextFloat() {
+    	checkClosed();
+    	Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj instanceof Float) {
+			findStartIndex = cachehasNextIndex;
+			return (Float) obj;
+		}
         Pattern floatPattern = getFloatPattern();
         String floatString = next(floatPattern);
         floatString = removeLocaleInfoFromFloat(floatString);
@@ -1389,10 +1427,12 @@
      *             if the next token can not be translated into a valid int
      *             value
      */
+    @SuppressWarnings("boxing")
     public int nextInt(int radix) {
+    	checkClosed();
     	Object obj = cacheHasNextValue;
 		cacheHasNextValue = null;
-		if (obj != null && obj instanceof Integer) {
+		if (obj instanceof Integer) {
 			findStartIndex = cachehasNextIndex;
 			return (Integer) obj;
 		}
@@ -1510,7 +1550,15 @@
      *             if the next token can not be translated into a valid long
      *             value, or it is out of range
      */
+    @SuppressWarnings("boxing")
     public long nextLong(int radix) {
+    	checkClosed();
+    	Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj instanceof Long) {
+			findStartIndex = cachehasNextIndex;
+			return (Long) obj;
+		}
         Pattern integerPattern = getIntegerPattern(radix);
         String intString = next(integerPattern);
         intString = removeLocaleInfo(intString, DataType.INT);
@@ -1576,10 +1624,12 @@
      *             if the next token can not be translated into a valid short
      *             value, or it is out of range
      */
+    @SuppressWarnings("boxing")
     public short nextShort(int radix) {
+    	checkClosed();
     	Object obj = cacheHasNextValue;
 		cacheHasNextValue = null;
-		if (obj != null && obj instanceof Short) {
+		if (obj instanceof Short) {
 			findStartIndex = cachehasNextIndex;
 			return (Short) obj;
 		}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java?view=diff&rev=471795&r1=471794&r2=471795
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
Mon Nov  6 09:05:42 2006
@@ -2784,6 +2784,38 @@
 
     /**
      * @throws IOException
+     * @tests java.util.Scanner#hasNextByte(int)
+     */
+    public void test_hasNextByteI_cache() throws IOException{
+        //regression for HARMONY-2063
+    	s = new Scanner("123 45");
+		assertTrue(s.hasNextByte(8));
+		assertEquals(83, s.nextByte());
+		assertEquals(45, s.nextByte());
+
+		s = new Scanner("123 45");
+		assertTrue(s.hasNextByte(10));
+		assertTrue(s.hasNextByte(8));
+		assertEquals(83, s.nextByte());
+		assertEquals(45, s.nextByte());
+
+		s = new Scanner("-123 -45");
+		assertTrue(s.hasNextByte(8));
+		assertEquals(-123, s.nextInt());
+		assertEquals(-45, s.nextByte());
+		
+		s = new Scanner("123 45");
+		assertTrue(s.hasNextByte());
+		s.close();
+		try {
+			s.nextByte();
+			fail("Should throw IllegalStateException");
+		} catch (IllegalStateException e) {
+			// expected
+		}
+    }
+    /**
+     * @throws IOException
      * @tests java.util.Scanner#hasNextByte()
      */
     public void test_hasNextByte() throws IOException {
@@ -3041,6 +3073,39 @@
     
     /**
      * @throws IOException
+     * @tests java.util.Scanner#hasNextBigInteger(int)
+     */
+    public void test_hasNextBigIntegerI_cache() throws IOException {
+        //regression for HARMONY-2063
+    	s = new Scanner("123 123456789123456789");
+		assertTrue(s.hasNextBigInteger(16));
+		assertEquals(new BigInteger("291"), s.nextBigInteger());
+		assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger());
+
+		s = new Scanner("123456789123456789 456");
+		assertTrue(s.hasNextBigInteger(16));
+		assertTrue(s.hasNextBigInteger(10));
+		assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger());
+		assertEquals(new BigInteger("456"), s.nextBigInteger());
+
+		s = new Scanner("-123 -123456789123456789");
+		assertTrue(s.hasNextBigInteger(8));
+		assertEquals(-123, s.nextShort());
+		assertEquals(new BigInteger("-123456789123456789"), s.nextBigInteger());
+		
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextBigInteger());
+		s.close();
+		try {
+			s.nextBigInteger();
+			fail("Should throw IllegalStateException");
+		} catch (IllegalStateException e) {
+			// expected
+		}
+    }
+    
+    /**
+     * @throws IOException
      * @tests java.util.Scanner#hasNextBigInteger()
      */
     public void test_hasNextBigInteger() throws IOException {
@@ -3386,9 +3451,10 @@
      * @tests java.util.Scanner#hasNextInt(int)
      */
     public void test_hasNextIntI_cache() throws IOException {
+        //regression for HARMONY-2063
     	s = new Scanner("123 456");
 		assertTrue(s.hasNextInt(16));
-		assertEquals(291, s.nextInt());
+		assertEquals(291, s.nextInt(10));
 		assertEquals(456, s.nextInt());
 
 		s = new Scanner("123 456");
@@ -3403,6 +3469,16 @@
 		assertEquals(-456, s.nextInt());
 		assertTrue(s.hasNextShort(16));
 		assertEquals(-789, s.nextInt());
+		
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextInt());
+		s.close();
+		try {
+			s.nextInt();
+			fail("Should throw IllegalStateException");
+		} catch (IllegalStateException e) {
+			// expected
+		}
     }
     /**
      * @throws IOException
@@ -3690,6 +3766,17 @@
         assertEquals("123-", s.next());
         assertTrue(s.hasNextFloat());
         assertEquals((float)-123.0, s.nextFloat());
+        
+        s = new Scanner("+123.4 -456.7");
+        s.useLocale(Locale.ENGLISH);
+        assertTrue(s.hasNextFloat());
+        s.close();
+        try{
+        	s.nextFloat();
+        	fail("Should throw IllegalStateException");
+        }catch(IllegalStateException e){
+        	//expected
+        }
 
     }
     
@@ -4040,6 +4127,7 @@
 	 * @tests java.util.Scanner#hasNextShort(int)
 	 */
 	public void test_hasNextShortI_cache() throws IOException {
+        //regression for HARMONY-2063
 		s = new Scanner("123 456");
 		assertTrue(s.hasNextShort(16));
 		assertEquals(291, s.nextShort());
@@ -4057,6 +4145,16 @@
 		assertEquals(-456, s.nextShort());
 		assertTrue(s.hasNextInt(16));
 		assertEquals(-789, s.nextShort());
+		
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextShort());
+		s.close();
+		try {
+			s.nextShort();
+			fail("Should throw IllegalStateException");
+		} catch (IllegalStateException e) {
+			// expected
+		}
 	}
     
     /**
@@ -4228,6 +4326,41 @@
         assertTrue(s.hasNextLong(10));
         assertEquals(-123, s.nextLong(10));
     }
+    
+    /**
+	 * @throws IOException
+	 * @tests java.util.Scanner#hasNextLong(int)
+	 */
+    public void test_hasNextLongI_cache() throws IOException {
+        //regression for HARMONY-2063
+    	s = new Scanner("123 456");
+		assertTrue(s.hasNextLong(16));
+		assertEquals(291, s.nextLong());
+		assertEquals(456, s.nextLong());
+
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextLong(16));
+		assertTrue(s.hasNextLong(8));
+		assertEquals(83, s.nextLong());
+		assertEquals(456, s.nextLong());
+
+		s = new Scanner("-123 -456 -789");
+		assertTrue(s.hasNextLong(8));
+		assertEquals(-123, s.nextInt());
+		assertEquals(-456, s.nextLong());
+		assertTrue(s.hasNextShort(16));
+		assertEquals(-789, s.nextLong());
+		
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextLong());
+		s.close();
+		try {
+			s.nextLong();
+			fail("Should throw IllegalStateException");
+		} catch (IllegalStateException e) {
+			// expected
+		}
+    }
 
     /**
      * @throws IOException
@@ -4495,6 +4628,17 @@
         s.useLocale(Locale.ENGLISH);
         assertTrue(s.hasNextDouble());
         assertEquals(-123.4, s.nextDouble());
+        
+        s = new Scanner("+123.4 -456.7");
+        s.useLocale(Locale.ENGLISH);
+        assertTrue(s.hasNextDouble());
+        s.close();
+        try{
+        	s.nextDouble();
+        	fail("Should throw IllegalStateException");
+        }catch(IllegalStateException e){
+        	//expected
+        }
     }
     
     /**



Mime
View raw message