You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fluo.apache.org by kt...@apache.org on 2017/04/26 20:37:31 UTC

incubator-fluo git commit: fixes #823

Repository: incubator-fluo
Updated Branches:
  refs/heads/master 73739fa28 -> 38dc66d15


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


Project: http://git-wip-us.apache.org/repos/asf/incubator-fluo/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-fluo/commit/38dc66d1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-fluo/tree/38dc66d1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-fluo/diff/38dc66d1

Branch: refs/heads/master
Commit: 38dc66d1572d98617b8d7ef139c7b951777cd9b5
Parents: 73739fa
Author: Christopher McTague <cj...@vwc.edu>
Authored: Fri Apr 21 20:25:03 2017 -0400
Committer: Keith Turner <kt...@apache.org>
Committed: Wed Apr 26 16:35:25 2017 -0400

----------------------------------------------------------------------
 .../java/org/apache/fluo/api/data/Bytes.java    | 47 ++++++++++++++++++++
 .../org/apache/fluo/api/data/BytesTest.java     | 29 ++++++++++++
 2 files changed, 76 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-fluo/blob/38dc66d1/modules/api/src/main/java/org/apache/fluo/api/data/Bytes.java
----------------------------------------------------------------------
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 @@ public final class Bytes implements Comparable<Bytes>, Serializable {
   }
 
   /**
+   * 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.
    *

http://git-wip-us.apache.org/repos/asf/incubator-fluo/blob/38dc66d1/modules/api/src/test/java/org/apache/fluo/api/data/BytesTest.java
----------------------------------------------------------------------
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 @@ public class BytesTest {
   }
 
   @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");