Repository: accumulo Updated Branches: refs/heads/master 35de95264 -> f7481b4ce
ACCUMULO-4326 Accept Text and CharSeq in Value Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/7b8a11bf Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/7b8a11bf Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/7b8a11bf Branch: refs/heads/master Commit: 7b8a11bf7f36592aceba9835b4b93adc6f176b8f Parents: 68576ad Author: Keith Turner <ke...@deenlo.com> Authored: Thu Jun 2 17:48:07 2016 -0400 Committer: Keith Turner <ktur...@apache.org> Committed: Fri Jun 3 11:23:19 2016 -0400 ---------------------------------------------------------------------- .../org/apache/accumulo/core/data/Value.java | 26 ++++++++++++++++++++ .../apache/accumulo/core/data/ValueTest.java | 25 +++++++++++++++++++ 2 files changed, 51 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/7b8a11bf/core/src/main/java/org/apache/accumulo/core/data/Value.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/data/Value.java b/core/src/main/java/org/apache/accumulo/core/data/Value.java index 6883885..95c3c70 100644 --- a/core/src/main/java/org/apache/accumulo/core/data/Value.java +++ b/core/src/main/java/org/apache/accumulo/core/data/Value.java @@ -25,9 +25,11 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.List; import org.apache.hadoop.io.BytesWritable; +import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableComparator; @@ -48,6 +50,30 @@ public class Value implements WritableComparable<Object> { } /** + * Creates a value using the UTF-8 encoding of the CharSequence + * + * @param cs + * may not be null + * + * @since 1.8.0 + */ + public Value(CharSequence cs) { + this(cs.toString().getBytes(StandardCharsets.UTF_8)); + } + + /** + * Creates a Value using the bytes of the Text. Makes a copy, does not use the byte array from the Text. + * + * @param text + * may not be null + * + * @since 1.8.0 + */ + public Value(Text text) { + this(text.getBytes(), 0, text.getLength()); + } + + /** * Creates a Value using a byte array as the initial value. The given byte array is used directly as the backing array, so later changes made to the array * reflect into the new Value. * http://git-wip-us.apache.org/repos/asf/accumulo/blob/7b8a11bf/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java b/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java index 81e7b08..0cae140 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java +++ b/core/src/test/java/org/apache/accumulo/core/data/ValueTest.java @@ -35,6 +35,7 @@ import java.io.DataOutputStream; import java.nio.ByteBuffer; import java.util.List; +import org.apache.hadoop.io.Text; import org.junit.Before; import org.junit.Test; @@ -215,4 +216,28 @@ public class ValueTest { assertArrayEquals(two, a[1]); assertArrayEquals(three, a[2]); } + + @Test + public void testString() { + Value v1 = new Value("abc"); + Value v2 = new Value("abc".getBytes(UTF_8)); + assertEquals(v2, v1); + } + + @Test(expected = NullPointerException.class) + public void testNullCharSequence() { + new Value((CharSequence) null); + } + + @Test + public void testText() { + Value v1 = new Value(new Text("abc")); + Value v2 = new Value("abc".getBytes(UTF_8)); + assertEquals(v2, v1); + } + + @Test(expected = NullPointerException.class) + public void testNullText() { + new Value((Text) null); + } }