harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r424204 - 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, 21 Jul 2006 06:54:56 GMT
Author: pyang
Date: Thu Jul 20 23:54:56 2006
New Revision: 424204

URL: http://svn.apache.org/viewvc?rev=424204&view=rev
Log:
Fix for HARMONY-931 ([classlib][luni] Implementation of new method java.util.Scanner.hasNextLine())

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?rev=424204&r1=424203&r2=424204&view=diff
==============================================================================
--- 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
Thu Jul 20 23:54:56 2006
@@ -284,7 +284,7 @@
      * 
      */
     public void close() {
-        if (closed == true) {
+        if (closed) {
             return;
         }
         if (input instanceof Closeable) {
@@ -710,9 +710,41 @@
         return isIntValue;
     }
     
-    //TODO: To implement this feature
+    /**
+     * Returns true if there is another line in the input. Otherwise, returns
+     * false. When waiting for input, the scanner may be blocked. No matter true
+     * or false, the scanner will not advance any input.
+     * 
+     * @return true if there is another line in the input. Otherwise, false will
+     *         be returned.
+     * @throws IllegalStateException
+     *             if the scanner is closed
+     */
     public boolean hasNextLine() {
-        throw new NotYetImplementedException();
+        checkClosed();
+        matcher.usePattern(LINE_PATTERN);
+        matcher.region(findStartIndex, bufferLength);
+
+        boolean hasNextLine = false;
+        while (true) {
+            if (matcher.find()) {
+                if (inputExhausted || matcher.end() != bufferLength) {
+                    matchSuccessful = true;
+                    hasNextLine = true;
+                    break;
+                }
+            } else {
+                if (inputExhausted) {
+                    matchSuccessful = false;
+                    break;
+                }
+            }
+            if (!inputExhausted) {
+                readMore();
+                resetMatcher();
+            }
+        }
+        return hasNextLine;
     }
 
     /**
@@ -801,7 +833,7 @@
     }
 
     /**
-     * returns the last IOException thrown when reading the underlying input. If
+     * Returns the last IOException thrown when reading the underlying input. If
      * no exception is thrown, return null.
      * 
      * @return the last IOException thrown
@@ -811,7 +843,7 @@
     }
 
     /**
-     * return the locale of this scanner.
+     * Return the locale of this scanner.
      * 
      * @return 
      *             the locale of this scanner
@@ -1287,7 +1319,7 @@
         String result = null;
         while (true) {
             if (matcher.find()) {
-                if (inputExhausted || bufferLength != matcher.end()) {
+                if (inputExhausted || matcher.end() != bufferLength) {
                     matchSuccessful = true;
                     findStartIndex = matcher.end();
                     result = matcher.group();
@@ -1302,9 +1334,7 @@
             if (!inputExhausted) {
                 readMore();
                 resetMatcher();
-            } else {
-                break;
-            }
+            } 
         }
         // Find text without line terminator here.
         if (null != result) {
@@ -1449,7 +1479,7 @@
     }
 
     /**
-     * return the radix of this scanner.
+     * Return the radix of this scanner.
      * 
      * @return
      *            the radix of this scanner
@@ -1556,23 +1586,24 @@
 
     /**
      * 
-     * set the locale of this scanner to a specified locale. 
+     * Set the locale of this scanner to a specified locale. 
      *
-     * @param locale
+     * @param l
      *              the specified locale to use
      * @return
      *              this scanner
      */
-    public Scanner useLocale(Locale locale) {
-        if (null == locale)
+    public Scanner useLocale(Locale l) {
+        if (null == l) {
             throw new NullPointerException();
-        this.locale = locale;
+        }
+        this.locale = l;
         return this;
     }
 
     /**
      * 
-     * set the radix of this scanner to a specified radix.
+     * Set the radix of this scanner to a specified radix.
      * 
      * @param radix
      *             the specified radix to use
@@ -1642,7 +1673,7 @@
     }
 
     /*
-     * save the matcher's last find position
+     * Save the matcher's last find position
      */
     private void saveCurrentStatus() {
         preStartIndex = findStartIndex;

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?rev=424204&r1=424203&r2=424204&view=diff
==============================================================================
--- 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
Thu Jul 20 23:54:56 2006
@@ -5087,6 +5087,7 @@
         s.close();
         try {
             s.nextLine();
+            fail("Should throw IllegalStateException");
         } catch (IllegalStateException e) {
             // expected
         }
@@ -5211,6 +5212,79 @@
         s = new Scanner("test\n ");
         result = s.nextLine();
         assertEquals("test", result);
+    }
+    
+    /**
+     * @tests java.util.Scanner#hasNextLine()
+     */
+    public void test_hasNextLine() {
+        
+        s = new Scanner("");
+        s.close();
+        try {
+            s.hasNextLine();
+            fail("Should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+        
+        s = new Scanner("test\r\ntest");
+        boolean result = s.hasNextLine();
+        assertTrue(result);
+        MatchResult matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(6, matchResult.end());
+
+        s = new Scanner("\u0085");
+        result = s.hasNextLine();
+        assertTrue(result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+        
+        s = new Scanner("\u2028");
+        result = s.hasNextLine();
+        assertTrue(result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+        
+        s = new Scanner("\u2029");
+        result = s.hasNextLine();
+        assertTrue(result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+        
+        s = new Scanner("test\n");
+        assertTrue(s.hasNextLine());
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(5, matchResult.end());
+
+        char[] chars = new char[2048];
+        Arrays.fill(chars, 'a');
+        StringBuilder stringBuilder = new StringBuilder();
+        stringBuilder.append(chars);
+        s = new Scanner(stringBuilder.toString());
+        result = s.hasNextLine();
+        assertTrue(result);
+
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(2048, matchResult.end());
+
+        s = new Scanner("\n\n\n");
+        assertTrue(s.hasNextLine());
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+
+        // The scanner will not advance any input.
+        assertTrue(s.hasNextLine());
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
     }
     
     protected void setUp() throws Exception {



Mime
View raw message