]> granicus.if.org Git - icu/commitdiff
ICU-12982 add BreakIterator.setText(CharSequence) overload
authorAndy Heninger <andy.heninger@gmail.com>
Mon, 19 Jun 2017 20:28:41 +0000 (20:28 +0000)
committerAndy Heninger <andy.heninger@gmail.com>
Mon, 19 Jun 2017 20:28:41 +0000 (20:28 +0000)
X-SVN-Rev: 40179

icu4j/main/classes/core/src/com/ibm/icu/impl/CSCharacterIterator.java [new file with mode: 0644]
icu4j/main/classes/core/src/com/ibm/icu/text/BreakIterator.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/CSCharacterIteratorTest.java [new file with mode: 0644]
icu4j/main/tests/core/src/com/ibm/icu/dev/test/rbbi/RBBIAPITest.java

diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/CSCharacterIterator.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/CSCharacterIterator.java
new file mode 100644 (file)
index 0000000..4de2889
--- /dev/null
@@ -0,0 +1,106 @@
+// © 2017 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html#License
+package com.ibm.icu.impl;
+
+import java.text.CharacterIterator;
+
+/**
+ * Implement the Java CharacterIterator interface on a CharSequence.
+ * Intended for internal use by ICU only.
+ */
+public class CSCharacterIterator implements CharacterIterator {
+
+    private int index;
+    private CharSequence seq;
+
+
+    /**
+     * Constructor.
+     * @param text The CharSequence to iterate over.
+     */
+    public CSCharacterIterator(CharSequence text) {
+        if (text == null) {
+            throw new NullPointerException();
+        }
+        seq = text;
+        index = 0;
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public char first() {
+        index = 0;
+        return current();
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public char last() {
+        index = seq.length();
+        return previous();
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public char current() {
+        if (index == seq.length()) {
+            return DONE;
+        }
+        return seq.charAt(index);
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public char next() {
+        if (index < seq.length()) {
+            ++index;
+        }
+        return current();
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public char previous() {
+        if (index == 0) {
+            return DONE;
+        }
+        --index;
+        return current();
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public char setIndex(int position) {
+        if (position < 0 || position > seq.length()) {
+            throw new IllegalArgumentException();
+        }
+        index = position;
+        return current();
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public int getBeginIndex() {
+        return 0;
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public int getEndIndex() {
+        return seq.length();
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public int getIndex() {
+        return index;
+    }
+
+    /** @{inheritDoc} */
+    @Override
+    public Object clone() {
+        CSCharacterIterator copy = new CSCharacterIterator(seq);
+        copy.setIndex(index);
+        return copy;
+    }
+}
index e7723caa26b3c86d7195fae270821eed796eced7..451b8c0ea769f9a895cb021962e8d3b8269887d0 100644 (file)
@@ -14,6 +14,7 @@ import java.text.StringCharacterIterator;
 import java.util.Locale;
 import java.util.MissingResourceException;
 
+import com.ibm.icu.impl.CSCharacterIterator;
 import com.ibm.icu.impl.CacheValue;
 import com.ibm.icu.impl.ICUDebug;
 import com.ibm.icu.util.ICUCloneNotSupportedException;
@@ -513,6 +514,19 @@ public abstract class BreakIterator implements Cloneable
         setText(new StringCharacterIterator(newText));
     }
 
+    /**
+     * Sets the iterator to analyze a new piece of text.  The new
+     * piece of text is passed in as a CharSequence, and the current
+     * iteration position is reset to the beginning of the text.
+     * (The old text is dropped.)
+     * @param newText A CharSequence containing the text to analyze with
+     * this BreakIterator.
+     * @draft ICU 60
+     */
+    public void setText(CharSequence newText) {
+        setText(new CSCharacterIterator(newText));
+    }
+
     /**
      * Sets the iterator to analyze a new piece of text.  The
      * BreakIterator is passed a CharacterIterator through which
diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/CSCharacterIteratorTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/CSCharacterIteratorTest.java
new file mode 100644 (file)
index 0000000..10657ee
--- /dev/null
@@ -0,0 +1,45 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html#License
+
+package com.ibm.icu.dev.test.impl;
+
+import java.text.CharacterIterator;
+import java.text.StringCharacterIterator;
+
+import org.junit.Test;
+
+import com.ibm.icu.dev.test.TestFmwk;
+import com.ibm.icu.impl.CSCharacterIterator;
+
+public class CSCharacterIteratorTest extends TestFmwk {
+    public CSCharacterIteratorTest() {};
+
+    @Test
+    public void TestAPI() {
+        String text = "Hello, World";
+
+        CharSequence cs = text;
+        CharacterIterator csci = new CSCharacterIterator(cs);
+        CharacterIterator sci = new StringCharacterIterator(text);
+
+        assertEquals("", sci.setIndex(6), csci.setIndex(6));
+        assertEquals("", sci.getIndex(), csci.getIndex());
+        assertEquals("", sci.current(), csci.current());
+        assertEquals("", sci.previous(), csci.previous());
+        assertEquals("", sci.next(), csci.next());
+        assertEquals("", sci.getBeginIndex(), csci.getBeginIndex());
+        assertEquals("", sci.getEndIndex(), csci.getEndIndex());
+        assertEquals("", sci.first(), csci.first());
+        assertEquals("", sci.last(), csci.last());
+
+        csci.setIndex(4);
+        sci.setIndex(4);
+        CharacterIterator clci = (CharacterIterator)csci.clone();
+        for (int i=0; i<50; ++i) {
+            assertEquals("", sci.next(), clci.next());
+        }
+        for (int i=0; i<50; ++i) {
+            assertEquals("", sci.previous(), clci.previous());
+        }
+    }
+}
index 01ef72d00c8ea3aa836658222407996cf1710979..a71613bb64b96d164281a7b7aa41aabcb7b27c88 100644 (file)
@@ -18,6 +18,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
 import java.text.CharacterIterator;
 import java.text.StringCharacterIterator;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 
 import org.junit.Test;
@@ -142,22 +144,19 @@ public class RBBIAPITest extends com.ibm.icu.dev.test.TestFmwk {
         if (wordIter1.current() != 0)
             errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is"
                     + wordIter1.current() + "\n");
-        //ICU4J has remove the method adoptText
-        /*
-        charIter1.adoptText(text1Clone);
-        if (wordIter1.getText() == charIter1.getText()
-            || wordIter1.getText() != text2
-            || charIter1.getText() != text1)
-            errln((UnicodeString) "ERROR:2 error is getText or setText()");
 
-        RuleBasedBreakIterator rb = (RuleBasedBreakIterator) wordIter1.clone();
-        rb.adoptText(text1);
-        if (rb.getText() != text1)
-            errln((UnicodeString) "ERROR:1 error in adoptText ");
-        rb.adoptText(text2);
-        if (rb.getText() != text2)
-            errln((UnicodeString) "ERROR:2 error in adoptText ");
-        */
+        // Test the CharSequence overload of setText() for a simple case.
+        BreakIterator lineIter = BreakIterator.getLineInstance(Locale.ENGLISH);
+        CharSequence csText = "Hello, World. ";
+        // Expected Line Brks  ^      ^      ^
+        //                     0123456789012345
+        List<Integer> expected = new ArrayList<Integer>();
+        expected.add(0); expected.add(7); expected.add(14);
+        lineIter.setText(csText);
+        for (int pos = lineIter.first(); pos != BreakIterator.DONE; pos = lineIter.next()) {
+            assertTrue("", expected.contains(pos));
+        }
+        assertEquals("", csText.length(), lineIter.current());
     }
 
     /**
@@ -458,7 +457,7 @@ public class RBBIAPITest extends com.ibm.icu.dev.test.TestFmwk {
         }
     }
 
-    /*Internal subroutine used for comparision of expected and acquired results */
+    /*Internal subroutine used for comparison of expected and acquired results */
     private void doTest(String testString, int start, int gotoffset, int expectedOffset, String expectedString) {
         String selected;
         String expected = expectedString;