fixes #823
-added startsWith(Byte prefix) method to Bytes.java
-added endsWith(Bytes suffix) method to Bytes.java
-added testPrefixSuffix() method to BytesTest.java
closes #825
diff --git a/modules/api/src/main/java/org/apache/fluo/api/data/Bytes.java b/modules/api/src/main/java/org/apache/fluo/api/data/Bytes.java
index 59c4321..e5bde63 100644
--- a/modules/api/src/main/java/org/apache/fluo/api/data/Bytes.java
+++ b/modules/api/src/main/java/org/apache/fluo/api/data/Bytes.java
@@ -355,6 +355,53 @@
}
/**
+ * Checks if this has the passed prefix
+ *
+ * @param prefix is a Bytes object to compare to this
+ * @return true or false
+ * @since 1.1.0
+ */
+ public boolean startsWith(Bytes prefix) {
+ Objects.requireNonNull(prefix, "startWith(Bytes prefix) cannot have null parameter");
+
+ if (prefix.length > this.length) {
+ return false;
+ } else {
+ int end = this.offset + prefix.length;
+ for (int i = this.offset, j = prefix.offset; i < end; i++, j++) {
+ if (this.data[i] != prefix.data[j]) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Checks if this has the passed suffix
+ *
+ * @param suffix is a Bytes object to compare to this
+ * @return true or false
+ * @since 1.1.0
+ */
+ public boolean endsWith(Bytes suffix) {
+ Objects.requireNonNull(suffix, "endsWith(Bytes suffix) cannot have null parameter");
+ int startOffset = this.length - suffix.length;
+
+ if (startOffset < 0) {
+ return false;
+ } else {
+ int end = startOffset + this.offset + suffix.length;
+ for (int i = startOffset + this.offset, j = suffix.offset; i < end; i++, j++) {
+ if (this.data[i] != suffix.data[j]) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
* This class provides an easy, efficient, reusable mechanism for building immutable Bytes
* objects.
*
diff --git a/modules/api/src/test/java/org/apache/fluo/api/data/BytesTest.java b/modules/api/src/test/java/org/apache/fluo/api/data/BytesTest.java
index 14d2e76..7d3aff9 100644
--- a/modules/api/src/test/java/org/apache/fluo/api/data/BytesTest.java
+++ b/modules/api/src/test/java/org/apache/fluo/api/data/BytesTest.java
@@ -79,6 +79,35 @@
}
@Test
+ public void testPrefixSuffix() {
+ Bytes b1 = Bytes.of("abcde");
+ Bytes prefix = Bytes.of("ab");
+ Bytes suffix = Bytes.of("de");
+ Bytes empty = new Bytes();
+ Bytes mid = Bytes.of("cd");
+
+ Assert.assertTrue(b1.startsWith(prefix));
+ Assert.assertTrue(b1.endsWith(suffix));
+ Assert.assertFalse(b1.startsWith(mid));
+ Assert.assertFalse(b1.endsWith(mid));
+ Assert.assertTrue(empty.startsWith(empty));
+ Assert.assertTrue(empty.endsWith(empty));
+ Assert.assertTrue(b1.startsWith(b1));
+ Assert.assertTrue(b1.endsWith(b1));
+ Assert.assertTrue(b1.startsWith(empty));
+ Assert.assertTrue(b1.endsWith(empty));
+ Assert.assertFalse(empty.startsWith(b1));
+ Assert.assertFalse(empty.endsWith(b1));
+ Assert.assertFalse(prefix.startsWith(b1));
+ Assert.assertFalse(prefix.endsWith(b1));
+ Assert.assertTrue(b1.startsWith(b1.subSequence(0, 2)));
+ Assert.assertFalse(b1.subSequence(0, 2).startsWith(b1));
+ Assert.assertTrue(b1.endsWith(b1.subSequence(3, 5)));
+ Assert.assertFalse(b1.endsWith(b1.subSequence(0, 2)));
+ Assert.assertFalse(b1.subSequence(0, 2).endsWith(b1));
+ }
+
+ @Test
public void testCompare() {
Bytes b1 = Bytes.of("a");
Bytes b2 = Bytes.of("b");