You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/06/30 13:31:52 UTC
svn commit: r418241 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/Scanner.java
test/java/tests/api/java/util/ScannerTest.java
Author: mloenko
Date: Fri Jun 30 04:31:52 2006
New Revision: 418241
URL: http://svn.apache.org/viewvc?rev=418241&view=rev
Log:
applied patch from HARMONY-712
Implementation of java.util.Scanner.next()
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=418241&r1=418240&r2=418241&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 Fri Jun 30 04:31:52 2006
@@ -59,6 +59,9 @@
// Default delimiting pattern
private static final Pattern DEFAULT_DELIMITER = Pattern
.compile("\\p{javaWhitespace}+"); //$NON-NLS-1$
+
+ // The pattern matching anything
+ private static final Pattern ANY_PATTERN = Pattern.compile("(?s).*");
private static final int DIPLOID = 2;
@@ -403,9 +406,22 @@
throw new NotYetImplementedException();
}
- //TODO: To implement this feature
+ /**
+ * Finds and Returns the next complete token which is prefixed and postfixed
+ * by input that matches the delimiter pattern. This method may be blocked
+ * when it is waiting for input to scan, even if a previous invocation of
+ * hasNext() returned true. If this match successes, the scanner advances
+ * past the next complete token.
+ *
+ * @return
+ * the next complete token
+ * @throws IllegalStateException
+ * if this scanner has been closed
+ * @throws NoSuchElementException
+ * if input has been exhausted
+ */
public String next() {
- throw new NotYetImplementedException();
+ return next(ANY_PATTERN);
}
/**
@@ -413,12 +429,13 @@
* matches the delimiter pattern if this token matches the specified
* pattern. This method may be blocked when it is waiting for input to scan,
* even if a previous invocation of hasNext(Pattern) returned true. If this
- * match successes, the scanner advances past the nest token that matched
+ * match successes, the scanner advances past the next token that matched
* the pattern.
*
* @param pattern
* the specified pattern to scan
- * @return the next token
+ * @return
+ * the next token
* @throws IllegalStateException
* if this scanner has been closed
* @throws NoSuchElementException
@@ -449,14 +466,15 @@
* matches the delimiter pattern if this token matches the pattern
* constructed from the sepcified string. This method may be blocked when it
* is waiting for input to scan. If this match successes, the scanner
- * advances past the nest token that matched the pattern.
+ * advances past the next token that matched the pattern.
*
* The invocation of this method in the form next(pattern) behaves in the
* same way as the invocaiton of next(Pattern.compile(pattern)).
*
* @param pattern
* the string specifying the pattern to scan for
- * @return the next token
+ * @return
+ * the next token
* @throws IllegalStateException
* if this scanner has been closed
* @throws NoSuchElementException
@@ -674,7 +692,7 @@
}
/*
- * save the matcher's last find position
+ * Change the matcher's status to last find position
*/
private void recoverPreviousStatus() {
findStartIndex = preStartIndex;
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=418241&r1=418240&r2=418241&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 Fri Jun 30 04:31:52 2006
@@ -464,6 +464,116 @@
/**
* @throws IOException
+ * @tests java.util.Scanner#next()
+ */
+ public void test_next() throws IOException {
+ // use special delimiter
+ s = new Scanner("1**2").useDelimiter("\\*");
+ assertEquals("1", s.next());
+ assertEquals("", s.next());
+ assertEquals("2", s.next());
+
+ s = new Scanner("word( )test( )").useDelimiter("\\( \\)");
+ assertEquals("word", s.next());
+ assertEquals("test", s.next());
+
+ s = new Scanner("? next ").useDelimiter("( )");
+ assertEquals("?", s.next());
+ assertEquals("next", s.next());
+ assertEquals("", s.next());
+
+ s = new Scanner("word1 word2 ");
+ assertEquals("word1", s.next());
+ assertEquals("word2", s.next());
+ // test boundary case
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // just delimiter exists in this scanner
+ s = new Scanner(" ");
+ try {
+ s.next();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // nothing exists in this scanner
+ s = new Scanner("");
+ try {
+ s.next();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // no delimiter exites in this scanner
+ s = new Scanner("test");
+ assertEquals("test", s.next());
+
+ // input resourse starts with delimiter
+ s = new Scanner(" test");
+ assertEquals("test", s.next());
+
+ // input resource ends with delimiter
+ s = new Scanner(" test ");
+ assertEquals("test", s.next());
+
+ // What if a sentence can not be read in all in once.
+ StringBuilder longSentence = new StringBuilder(1025);
+ for (int i = 0; i <= 10; i++) {
+ longSentence.append(" ");
+ }
+ for (int i = 11; i <= 1025; i++) {
+ longSentence.append("a");
+ }
+ s = new Scanner(longSentence.toString());
+ assertEquals(longSentence.toString().trim(), s.next());
+
+ s = new Scanner(" test test");
+ assertEquals("test", s.next());
+ assertEquals("test", s.next());
+
+ // What if use a delimiter of length 0.
+ s = new Scanner("test\ntest").useDelimiter(Pattern.compile("^",
+ Pattern.MULTILINE));
+ assertEquals("test\n", s.next());
+ assertEquals("test", s.next());
+
+ s = new Scanner("\ntest\ntest").useDelimiter(Pattern.compile("$",
+ Pattern.MULTILINE));
+ assertEquals("\ntest", s.next());
+ assertEquals("\ntest", s.next());
+
+ // test socket inputStream
+ // Harmony uses 1024 as default buffer size,
+ // what if the leading delimiter is larger than 1023
+ for (int i = 0; i < 1024; i++) {
+ os.write(" ".getBytes());
+ }
+ os.write(" 1 2 ".getBytes());
+ s = new Scanner(client);
+ assertEquals("1", s.next());
+ assertEquals("2", s.next());
+ os.write(" 1 2".getBytes());
+ serverSocket.close();
+ assertEquals("1", s.next());
+ assertEquals("2", s.next());
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ }
+
+ /**
+ * @throws IOException
* @tests java.util.Scanner#next(Pattern)
*/
public void test_nextLPattern() throws IOException {