TINKERPOP-1355 Design HasContainer for extension

Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/ca8a4f9f
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/ca8a4f9f
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/ca8a4f9f

Branch: refs/heads/TINKERPOP-1254
Commit: ca8a4f9fb8a9bc1d69aa10870d75076b8fa906b5
Parents: c0b0eed
Author: Bryn Cooke <[email protected]>
Authored: Mon Jul 4 09:29:40 2016 +0100
Committer: Bryn Cooke <[email protected]>
Committed: Mon Jul 4 10:40:43 2016 +0100

----------------------------------------------------------------------
 .../traversal/step/util/HasContainer.java       | 56 ++++++++++++++------
 1 file changed, 41 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/ca8a4f9f/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/HasContainer.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/HasContainer.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/HasContainer.java
index b634e37..d08f408 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/HasContainer.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/HasContainer.java
@@ -33,12 +33,12 @@ import java.util.function.Predicate;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public final class HasContainer implements Serializable, Cloneable, 
Predicate<Element> {
+public class HasContainer implements Serializable, Cloneable, 
Predicate<Element> {
 
     private String key;
     private P predicate;
 
-    private boolean testingIdString;
+    private final boolean testingIdString;
 
     public HasContainer(final String key, final P<?> predicate) {
         this.key = key;
@@ -66,34 +66,60 @@ public final class HasContainer implements Serializable, 
Cloneable, Predicate<El
         }
     }
 
-    public boolean test(final Element element) {
+    public final boolean test(final Element element) {
         // it is OK to evaluate equality of ids via toString(), given that the 
test suite enforces the value of
         // id().toString() to be a first class representation of the 
identifier. a string test is only executed
         // if the predicate value is a String.  this allows stuff like: 
g.V().has(id,lt(10)) to work properly
         if (this.key.equals(T.id.getAccessor()))
-            return testingIdString ?  
this.predicate.test(element.id().toString()) : 
this.predicate.test(element.id());
+            return testingIdString ? testIdAsString(element) : testId(element);
         else if (this.key.equals(T.label.getAccessor()))
-            return this.predicate.test(element.label());
+            return testLabel(element);
         else if (element instanceof VertexProperty && 
this.key.equals(T.value.getAccessor()))
-            return this.predicate.test(((VertexProperty) element).value());
+            return testValue((VertexProperty) element);
         else if (element instanceof VertexProperty && 
this.key.equals(T.key.getAccessor()))
-            return this.predicate.test(((VertexProperty) element).key());
+            return testKey((VertexProperty) element);
         else {
             if (element instanceof Vertex) {
                 final Iterator<? extends Property> itty = 
element.properties(this.key);
                 while (itty.hasNext()) {
-                    if (this.predicate.test(itty.next().value()))
+                    if (testValue(itty.next()))
                         return true;
                 }
                 return false;
             } else {
                 final Property property = element.property(this.key);
-                return property.isPresent() && 
this.predicate.test(property.value());
+                return property.isPresent() && testValue(property);
             }
         }
     }
 
-    public String toString() {
+    protected boolean testId(Element element)
+    {
+        return this.predicate.test(element.id());
+    }
+
+    protected boolean testIdAsString(Element element)
+    {
+        return this.predicate.test(element.id().toString());
+    }
+
+    protected boolean testLabel(Element element)
+    {
+        return this.predicate.test(element.label());
+    }
+
+    protected boolean testValue(Property property)
+    {
+        return this.predicate.test(property.value());
+    }
+
+    protected boolean testKey(Property property)
+    {
+        return this.predicate.test(property.key());
+    }
+
+
+    public final String toString() {
         return this.key + '.' + this.predicate;
     }
 
@@ -112,23 +138,23 @@ public final class HasContainer implements Serializable, 
Cloneable, Predicate<El
         return (this.key != null ? this.key.hashCode() : 0) ^ (this.predicate 
!= null ? this.predicate.hashCode() : 0);
     }
 
-    public String getKey() {
+    public final String getKey() {
         return this.key;
     }
 
-    public void setKey(final String key) {
+    public final void setKey(final String key) {
         this.key = key;
     }
 
-    public P<?> getPredicate() {
+    public final P<?> getPredicate() {
         return this.predicate;
     }
 
-    public BiPredicate<?, ?> getBiPredicate() {
+    public final BiPredicate<?, ?> getBiPredicate() {
         return this.predicate.getBiPredicate();
     }
 
-    public Object getValue() {
+    public final Object getValue() {
         return this.predicate.getValue();
     }
 

Reply via email to