harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r470753 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Scanner.java test/java/tests/api/java/util/ScannerTest.java
Date Fri, 03 Nov 2006 10:45:14 GMT
Author: pyang
Date: Fri Nov  3 02:45:13 2006
New Revision: 470753

URL: http://svn.apache.org/viewvc?view=rev&rev=470753
Log:
Apply patch for HARMONY-2063 ([classlib][luni]method hasNextXXX() of j.u.Scanner does not
cache the value.)

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=470753&r1=470752&r2=470753
==============================================================================
--- 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
Fri Nov  3 02:45:13 2006
@@ -137,6 +137,10 @@
     // Records whether the underlying readable has more input.
     private boolean inputExhausted = false;
     
+    private Object cacheHasNextValue = null;
+    
+    private int cachehasNextIndex = -1;
+    
     private enum DataType{
         /*
          * Stands for Integer
@@ -571,6 +575,7 @@
         boolean hasNext = false;
         //check whether next token matches the specified pattern
         if (matcher.matches()) {
+            cachehasNextIndex = findStartIndex;
             matchSuccessful = true;
             hasNext = true;
         }
@@ -805,7 +810,7 @@
             String intString = matcher.group();
             intString = removeLocaleInfo(intString, DataType.INT);
             try {
-                Integer.parseInt(intString, radix);
+            	cacheHasNextValue = Integer.parseInt(intString, radix);
                 isIntValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -927,7 +932,7 @@
             String intString = matcher.group();
             intString = removeLocaleInfo(intString, DataType.INT);
             try {
-                Short.parseShort(intString, radix);
+            	cacheHasNextValue = Short.parseShort(intString, radix);
                 isShortValue = true;
             } catch (NumberFormatException e) {
                 matchSuccessful = false;
@@ -1385,6 +1390,12 @@
      *             value
      */
     public int nextInt(int radix) {
+    	Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj != null && obj instanceof Integer) {
+			findStartIndex = cachehasNextIndex;
+			return (Integer) obj;
+		}
         Pattern integerPattern = getIntegerPattern(radix);
         String intString=next(integerPattern);
         intString = removeLocaleInfo(intString, DataType.INT);
@@ -1566,6 +1577,12 @@
      *             value, or it is out of range
      */
     public short nextShort(int radix) {
+    	Object obj = cacheHasNextValue;
+		cacheHasNextValue = null;
+		if (obj != null && obj instanceof Short) {
+			findStartIndex = cachehasNextIndex;
+			return (Short) obj;
+		}
         Pattern integerPattern = getIntegerPattern(radix);
         String intString = next(integerPattern);
         intString = removeLocaleInfo(intString, DataType.INT);

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=470753&r1=470752&r2=470753
==============================================================================
--- 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
Fri Nov  3 02:45:13 2006
@@ -3383,6 +3383,29 @@
 
     /**
      * @throws IOException
+     * @tests java.util.Scanner#hasNextInt(int)
+     */
+    public void test_hasNextIntI_cache() throws IOException {
+    	s = new Scanner("123 456");
+		assertTrue(s.hasNextInt(16));
+		assertEquals(291, s.nextInt());
+		assertEquals(456, s.nextInt());
+
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextInt(16));
+		assertTrue(s.hasNextInt(8));
+		assertEquals(83, s.nextInt());
+		assertEquals(456, s.nextInt());
+
+		s = new Scanner("-123 -456 -789");
+		assertTrue(s.hasNextInt(8));
+		assertEquals(-123, s.nextShort());
+		assertEquals(-456, s.nextInt());
+		assertTrue(s.hasNextShort(16));
+		assertEquals(-789, s.nextInt());
+    }
+    /**
+     * @throws IOException
      * @tests java.util.Scanner#hasNextInt()
      */
     public void test_hasNextInt() throws IOException {
@@ -4013,9 +4036,33 @@
     }
     
     /**
-     * @throws IOException
-     * @tests java.util.Scanner#hasNextLong(int)
-     */
+	 * @throws IOException
+	 * @tests java.util.Scanner#hasNextShort(int)
+	 */
+	public void test_hasNextShortI_cache() throws IOException {
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextShort(16));
+		assertEquals(291, s.nextShort());
+		assertEquals(456, s.nextShort());
+
+		s = new Scanner("123 456");
+		assertTrue(s.hasNextShort(16));
+		assertTrue(s.hasNextShort(8));
+		assertEquals(83, s.nextShort());
+		assertEquals(456, s.nextShort());
+
+		s = new Scanner("-123 -456 -789");
+		assertTrue(s.hasNextShort(8));
+		assertEquals(-123, s.nextInt());
+		assertEquals(-456, s.nextShort());
+		assertTrue(s.hasNextInt(16));
+		assertEquals(-789, s.nextShort());
+	}
+    
+    /**
+	 * @throws IOException
+	 * @tests java.util.Scanner#hasNextLong(int)
+	 */
     public void test_hasNextLongI() throws IOException {
         s = new Scanner("123 456");
         assertTrue(s.hasNextLong(10));



Mime
View raw message