From: Markus Scherer Date: Wed, 6 Feb 2019 23:23:17 +0000 (-0800) Subject: ICU-20375 string tries: covariant clone() return types, and copy constructors X-Git-Tag: release-64-rc~121 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=65852f1a741bc427e21459b8ccdda62dafa29782;p=icu ICU-20375 string tries: covariant clone() return types, and copy constructors --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/BytesTrie.java b/icu4j/main/classes/core/src/com/ibm/icu/util/BytesTrie.java index ba3d7f73e0e..b5e0db357cc 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/BytesTrie.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/BytesTrie.java @@ -48,6 +48,22 @@ public final class BytesTrie implements Cloneable, Iterable { 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 { * @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 { // 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 stack_=new ArrayList(); + private ArrayList stack_=new ArrayList<>(); } private void stop() { diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/CharsTrie.java b/icu4j/main/classes/core/src/com/ibm/icu/util/CharsTrie.java index 6ffe5b026c4..8b934230ef6 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/CharsTrie.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/CharsTrie.java @@ -51,6 +51,22 @@ public final class CharsTrie implements Cloneable, Iterable { 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 { * @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 { // 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 stack_=new ArrayList(); + private ArrayList stack_=new ArrayList<>(); } private void stop() { diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/BytesTrieTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/BytesTrieTest.java index d3f88494f8e..37af0166a13 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/BytesTrieTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/BytesTrieTest.java @@ -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); } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CharsTrieTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CharsTrieTest.java index 6654e7ebbbc..d15398731f1 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CharsTrieTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CharsTrieTest.java @@ -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); }