]> granicus.if.org Git - icu/commitdiff
ICU-20375 string tries: covariant clone() return types, and copy constructors
authorMarkus Scherer <markus.icu@gmail.com>
Wed, 6 Feb 2019 23:23:17 +0000 (15:23 -0800)
committerMarkus Scherer <markus.icu@gmail.com>
Thu, 7 Feb 2019 02:21:07 +0000 (18:21 -0800)
icu4j/main/classes/core/src/com/ibm/icu/util/BytesTrie.java
icu4j/main/classes/core/src/com/ibm/icu/util/CharsTrie.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/BytesTrieTest.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CharsTrieTest.java

index ba3d7f73e0ef1447a00cd598ded8293adae6482a..b5e0db357ccd24ed9d034f576640055f5e849653 100644 (file)
@@ -48,6 +48,22 @@ public final class BytesTrie implements Cloneable, Iterable<BytesTrie.Entry> {
         remainingMatchLength_=-1;
     }
 
+    /**
+     * Copy constructor.
+     * Makes a shallow copy of the other trie reader object and its state.
+     * Does not copy the byte array which will be shared.
+     * Same as clone() but without the throws clause.
+     *
+     * @draft ICU 64
+     * @provisional This API might change or be removed in a future release.
+     */
+    public BytesTrie(BytesTrie other) {
+        bytes_ = other.bytes_;
+        root_ = other.root_;
+        pos_ = other.pos_;
+        remainingMatchLength_ = other.remainingMatchLength_;
+    }
+
     /**
      * Clones this trie reader object and its state,
      * but not the byte array which will be shared.
@@ -55,8 +71,8 @@ public final class BytesTrie implements Cloneable, Iterable<BytesTrie.Entry> {
      * @stable ICU 4.8
      */
     @Override
-    public Object clone() throws CloneNotSupportedException {
-        return super.clone();  // A shallow copy is just what we need.
+    public BytesTrie clone() throws CloneNotSupportedException {
+        return (BytesTrie) super.clone();  // A shallow copy is just what we need.
     }
 
     /**
@@ -703,7 +719,7 @@ public final class BytesTrie implements Cloneable, Iterable<BytesTrie.Entry> {
         // and the remaining branch length in bits 24..16. (Bits 31..25 are unused.)
         // (We could store the remaining branch length minus 1 in bits 23..16 and not use bits 31..24,
         // but the code looks more confusing that way.)
-        private ArrayList<Long> stack_=new ArrayList<Long>();
+        private ArrayList<Long> stack_=new ArrayList<>();
     }
 
     private void stop() {
index 6ffe5b026c4eedda87720690f817d6869d94a515..8b934230ef6d3dab01dc575f5fe24718fef12cd4 100644 (file)
@@ -51,6 +51,22 @@ public final class CharsTrie implements Cloneable, Iterable<CharsTrie.Entry> {
         remainingMatchLength_=-1;
     }
 
+    /**
+     * Copy constructor.
+     * Makes a shallow copy of the other trie reader object and its state.
+     * Does not copy the char array which will be shared.
+     * Same as clone() but without the throws clause.
+     *
+     * @draft ICU 64
+     * @provisional This API might change or be removed in a future release.
+     */
+    public CharsTrie(CharsTrie other) {
+        chars_ = other.chars_;
+        root_ = other.root_;
+        pos_ = other.pos_;
+        remainingMatchLength_ = other.remainingMatchLength_;
+    }
+
     /**
      * Clones this trie reader object and its state,
      * but not the char array which will be shared.
@@ -58,8 +74,8 @@ public final class CharsTrie implements Cloneable, Iterable<CharsTrie.Entry> {
      * @stable ICU 4.8
      */
     @Override
-    public Object clone() throws CloneNotSupportedException {
-        return super.clone();  // A shallow copy is just what we need.
+    public CharsTrie clone() throws CloneNotSupportedException {
+        return (CharsTrie) super.clone();  // A shallow copy is just what we need.
     }
 
     /**
@@ -641,7 +657,7 @@ public final class CharsTrie implements Cloneable, Iterable<CharsTrie.Entry> {
         // and the remaining branch length in bits 31..16.
         // (We could store the remaining branch length minus 1 in bits 30..16 and not use bit 31,
         // but the code looks more confusing that way.)
-        private ArrayList<Long> stack_=new ArrayList<Long>();
+        private ArrayList<Long> stack_=new ArrayList<>();
     }
 
     private void stop() {
index d3f88494f8edf257d937521c8496aa1144a05353..37af0166a13aee21fb7a632ded0581031a3f31e9 100644 (file)
@@ -510,6 +510,27 @@ public class BytesTrieTest extends TestFmwk {
             data);
     }
 
+    @Test
+    public void TestClone() throws CloneNotSupportedException {
+        final StringAndValue[] data={
+            new StringAndValue("a", 1),
+            new StringAndValue("ab", 100),
+            new StringAndValue("abc", 300),
+            new StringAndValue("az", 999)
+        };
+        BytesTrie trie = buildTrie(data, data.length, StringTrieBuilder.Option.SMALL);
+        assertEquals("a result", BytesTrie.Result.INTERMEDIATE_VALUE, trie.next('a'));
+        assertEquals("a value", 1, trie.getValue());
+        BytesTrie clone = trie.clone();
+        trie = null;
+        assertEquals("ab result", BytesTrie.Result.INTERMEDIATE_VALUE, clone.next('b'));
+        assertEquals("ab value", 100, clone.getValue());
+        BytesTrie copy = new BytesTrie(clone);
+        clone = null;
+        assertEquals("abc result", BytesTrie.Result.FINAL_VALUE, copy.next('c'));
+        assertEquals("abc value", 300, copy.getValue());
+    }
+
     private void checkData(StringAndValue data[]) {
         checkData(data, data.length);
     }
index 6654e7ebbbc98080f40723463f57d46ba9645123..d15398731f162cfc32d536bdf56e56a91c15e59d 100644 (file)
@@ -637,6 +637,27 @@ public class CharsTrieTest extends TestFmwk {
         checkIterator(CharsTrie.iterator(trieChars, 0, 0), data);
     }
 
+    @Test
+    public void TestClone() throws CloneNotSupportedException {
+        final StringAndValue[] data={
+            new StringAndValue("a", 1),
+            new StringAndValue("ab", 100),
+            new StringAndValue("abc", 300),
+            new StringAndValue("az", 999)
+        };
+        CharsTrie trie = buildTrie(data, data.length, StringTrieBuilder.Option.SMALL);
+        assertEquals("a result", BytesTrie.Result.INTERMEDIATE_VALUE, trie.next('a'));
+        assertEquals("a value", 1, trie.getValue());
+        CharsTrie clone = trie.clone();
+        trie = null;
+        assertEquals("ab result", BytesTrie.Result.INTERMEDIATE_VALUE, clone.next('b'));
+        assertEquals("ab value", 100, clone.getValue());
+        CharsTrie copy = new CharsTrie(clone);
+        clone = null;
+        assertEquals("abc result", BytesTrie.Result.FINAL_VALUE, copy.next('c'));
+        assertEquals("abc value", 300, copy.getValue());
+    }
+
     private void checkData(StringAndValue data[]) {
         checkData(data, data.length);
     }