harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r419011 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Scanner.java test/java/tests/api/java/util/ScannerTest.java
Date Tue, 04 Jul 2006 14:08:30 GMT
Author: gharley
Date: Tue Jul  4 07:08:29 2006
New Revision: 419011

URL: http://svn.apache.org/viewvc?rev=419011&view=rev
Log:
HARMONY 729 : Implementation of java.util.Scanner.hasNext(Pattern)

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=419011&r1=419010&r2=419011&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
Tue Jul  4 07:08:29 2006
@@ -296,10 +296,39 @@
         throw new NotYetImplementedException();
     }
 
-    //TODO: To implement this feature
+    /**
+     * Returns true if this scanner's next token matches the specified pattern.
+     * This method may be blocked when it is waiting for input to scan. This
+     * scanner does not advance past the input that matched the pattern.
+     * 
+     * @param pattern
+     *            the specified pattern to scan
+     * @return 
+     *            true iff this scanner's next token matches the specified pattern
+     * @throws IllegalStateException
+     *            if the scanner has been closed
+     */
     public boolean hasNext(Pattern pattern) {
-        throw new NotYetImplementedException();
+        checkClosed();
+        if (isInputExhausted()) {
+            return false;
+        }
+        saveCurrentStatus();
+        //if the next token exists, set the match region, otherwise return false
+        if (!setTokenRegion()) {
+            recoverPreviousStatus();
+            return false;
+        }
+        matcher.usePattern(pattern);
+        boolean hasNext = false;
+        //check whether next token matches the specified pattern
+        if (matcher.matches()) {
+            hasNext = true;
+        }
+        recoverPreviousStatus();
+        return hasNext;
     }
+
 
     //TODO: To implement this feature
     public boolean hasNext(String pattern) {

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=419011&r1=419010&r2=419011&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
Tue Jul  4 07:08:29 2006
@@ -674,6 +674,83 @@
         }
     }
     
+    /**
+     * @throws IOException
+     * @tests java.util.Scanner#hasNext(Pattern)
+     */
+    public void test_hasNextLPattern() throws IOException {
+        Pattern pattern;
+        s = new Scanner("aab@2@abb@").useDelimiter("\\@");
+        pattern = Pattern.compile("a*b");
+        assertTrue(s.hasNext(pattern));
+        assertEquals("aab", s.next(pattern));
+        assertFalse(s.hasNext(pattern));
+        try {
+            s.next(pattern);
+            fail("should throw InputMismatchException");
+        } catch (InputMismatchException e) {
+            // Expected
+        }
+
+        s = new Scanner("word ? ");
+        pattern = Pattern.compile("\\w+");
+        assertTrue(s.hasNext(pattern));
+        assertEquals("word", s.next(pattern));
+        assertFalse(s.hasNext(pattern));
+        try {
+            s.next(pattern);
+            fail("should throw InputMismatchException");
+        } catch (InputMismatchException e) {
+            // Expected
+        }
+
+        s = new Scanner("word1 WorD2  ");
+        pattern = Pattern.compile("\\w+");
+        assertTrue(s.hasNext(pattern));
+        assertEquals("word1", s.next(pattern));
+        assertTrue(s.hasNext(pattern));
+        assertEquals("WorD2", s.next(pattern));
+        assertFalse(s.hasNext(pattern));
+        try {
+            s.next(pattern);
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+
+        s = new Scanner("word1 WorD2  ");
+        pattern = Pattern.compile("\\w+");
+        try {
+            s.hasNext((Pattern) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        s.close();
+        try {
+            s.hasNext(pattern);
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+        
+        // test socket inputStream
+        os.write("aab b".getBytes());
+        serverSocket.close();
+
+        s = new Scanner(client);
+        pattern = Pattern.compile("a+b");
+        assertTrue(s.hasNext(pattern));
+        assertEquals("aab", s.next(pattern));
+        assertFalse(s.hasNext(pattern));
+        try {
+            s.next(pattern);
+            fail("should throw InputMismatchException");
+        } catch (InputMismatchException e) {
+            // Expected
+        }
+    }
+    
     public void setUp() throws Exception {
         super.setUp();
 



Mime
View raw message