You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by dk...@apache.org on 2016/01/22 18:12:03 UTC

incubator-tinkerpop git commit: Added `P.type()`, `P.notType()`, `Compare.type()` and `Compare.notType()`.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-818 [created] 11845b7a0


Added `P.type()`, `P.notType()`, `Compare.type()` and `Compare.notType()`.


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

Branch: refs/heads/TINKERPOP-818
Commit: 11845b7a0408f910fed96a261369c315b3a39773
Parents: 5e6d668
Author: Daniel Kuppitz <da...@hotmail.com>
Authored: Fri Jan 22 18:09:47 2016 +0100
Committer: Daniel Kuppitz <da...@hotmail.com>
Committed: Fri Jan 22 18:09:47 2016 +0100

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |  1 +
 docs/src/reference/the-traversal.asciidoc       |  2 +
 .../gremlin/process/traversal/Compare.java      | 41 +++++++++++
 .../tinkerpop/gremlin/process/traversal/P.java  |  8 +++
 .../gremlin/process/traversal/CompareTest.java  | 72 ++++++++++++--------
 .../gremlin/process/traversal/PTest.java        | 25 ++++++-
 6 files changed, 118 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/11845b7a/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 2ccfa12..3ee1d33 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -26,6 +26,7 @@ image::https://raw.githubusercontent.com/apache/incubator-tinkerpop/master/docs/
 TinkerPop 3.1.1 (NOT OFFICIALLY RELEASED YET)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+* Added `P.type()`, `P.notType()`, `Compare.type()` and `Compare.notType()`.
 * Fixed Java comparator contract issue around `Order.shuffle`.
 * Optimized a very inefficient implementation of `SampleLocalStep`.
 * Reduced the complexity and execution time of all `AbstractLambdaTraversal` instances.

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/11845b7a/docs/src/reference/the-traversal.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/reference/the-traversal.asciidoc b/docs/src/reference/the-traversal.asciidoc
index 3f6b4e9..1c82fb3 100644
--- a/docs/src/reference/the-traversal.asciidoc
+++ b/docs/src/reference/the-traversal.asciidoc
@@ -2023,6 +2023,8 @@ provided predicates are outlined in the table below and are used in various step
 | `between(number,number)` | Is the incoming number greater than or equal to the first provided number and less than the second?
 | `within(objects...)` | Is the incoming object in the array of provided objects?
 | `without(objects...)` | Is the incoming object not in the array of the provided objects?
+| `type(class)` | Is the incoming object an instance or a subclass of the provided class?
+| `type(class)` | Is the incoming object neither an instance nor a subclass of the provided class?
 |=========================================================
 
 [gremlin-groovy]

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/11845b7a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Compare.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Compare.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Compare.java
index 97b52b8..78004f9 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Compare.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Compare.java
@@ -169,6 +169,47 @@ public enum Compare implements BiPredicate<Object, Object> {
         public Compare negate() {
             return gt;
         }
+    },
+
+    /**
+     * Evaluates if the type of the first object is either an instance of the class provided in the second or
+     * a subclass of it.
+     */
+    type {
+        @Override
+        public boolean test(final Object first, final Object second) {
+            if (second != null && second instanceof Class) {
+                return first == null || ((Class) second).isAssignableFrom(first.getClass());
+            }
+            throw new IllegalArgumentException("The comparator must be an instance of java.lang.Class.");
+        }
+
+        /**
+         * The negative of {@code type} is {@link #notType}.
+         */
+        @Override
+        public Compare negate() {
+            return notType;
+        }
+    },
+
+    /**
+     * Evaluates if the type of the first object is neither an instance of the class provided in the second nor
+     * a subclass of it.
+     */
+    notType {
+        @Override
+        public boolean test(final Object first, final Object second) {
+            return !type.test(first, second);
+        }
+
+        /**
+         * The negative of {@code notType} is {@link #type}.
+         */
+        @Override
+        public Compare negate() {
+            return type;
+        }
     };
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/11845b7a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/P.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/P.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/P.java
index b4072cc..ff6f6d5 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/P.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/P.java
@@ -180,4 +180,12 @@ public class P<V> implements Predicate<V>, Serializable, Cloneable {
     public static <V> P<V> not(final P<V> predicate) {
         return predicate.negate();
     }
+
+    public static <V> P<V> type(final Class<V> value) {
+        return new P(Compare.type, value);
+    }
+
+    public static <V> P<V> notType(final Class<V> value) {
+        return new P(Compare.notType, value);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/11845b7a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/CompareTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/CompareTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/CompareTest.java
index a7d8e1b..5cefa80 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/CompareTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/CompareTest.java
@@ -87,20 +87,32 @@ public class CompareTest {
                 {Compare.lte, 100, 99, false},
                 {Compare.lte, 100, 101, true},
                 {Compare.lte, "z", "a", false},
-                {Compare.lte, "a", "z", true}
+                {Compare.lte, "a", "z", true},
+                {Compare.type, 1, Integer.class, true},
+                {Compare.type, 1, Long.class, false},
+                {Compare.type, 1, Number.class, true},
+                {Compare.type, 1, String.class, false},
+                {Compare.type, 1L, Integer.class, false},
+                {Compare.type, 1L, Long.class, true},
+                {Compare.type, 1L, Number.class, true},
+                {Compare.type, 1L, String.class, false},
+                {Compare.type, "1", Integer.class, false},
+                {Compare.type, "1", Long.class, false},
+                {Compare.type, "1", Number.class, false},
+                {Compare.type, "1", String.class, true},
         }));
         // Compare Numbers of mixed types.
         final List<Object> one = Arrays.asList(1, 1l, 1d, 1f, BigDecimal.ONE, BigInteger.ONE);
         for (Object i : one) {
             for (Object j : one) {
                 testCases.addAll(Arrays.asList(new Object[][]{
-                            {Compare.eq, i, j, true},
-                            {Compare.neq, i, j, false},
-                            {Compare.gt, i, j, false},
-                            {Compare.lt, i, j, false},
-                            {Compare.gte, i, j, true},
-                            {Compare.lte, i, j, true},
-                        }));
+                        {Compare.eq, i, j, true},
+                        {Compare.neq, i, j, false},
+                        {Compare.gt, i, j, false},
+                        {Compare.lt, i, j, false},
+                        {Compare.gte, i, j, true},
+                        {Compare.lte, i, j, true},
+                }));
             }
         }
         // Compare large numbers of different types that cannot convert to doubles losslessly.
@@ -108,28 +120,28 @@ public class CompareTest {
         final BigDecimal big1d = new BigDecimal("123456789012345678901234567890");
         final BigDecimal big2 = new BigDecimal(big1.add(BigInteger.ONE));
         testCases.addAll(Arrays.asList(new Object[][]{
-                    // big1 == big1d
-                    {Compare.eq, big1, big1d, true},
-                    {Compare.neq, big1, big1d, false},
-                    {Compare.gt, big1, big1d, false},
-                    {Compare.lt, big1, big1d, false},
-                    {Compare.gte, big1, big1d, true},
-                    {Compare.lte, big1, big1d, true},
-                    // big1 < big2
-                    {Compare.eq, big1, big2, false},
-                    {Compare.neq, big1, big2, true},
-                    {Compare.gt, big1, big2, false},
-                    {Compare.lt, big1, big2, true},
-                    {Compare.gte, big1, big2, false},
-                    {Compare.lte, big1, big2, true},
-                    // Reverse the operands for symmetric test coverage (big2 > big1)
-                    {Compare.eq, big2, big1, false},
-                    {Compare.neq, big2, big1, true},
-                    {Compare.gt, big2, big1, true},
-                    {Compare.lt, big2, big1, false},
-                    {Compare.gte, big2, big1, true},
-                    {Compare.lte, big2, big1, false},
-                }));
+                // big1 == big1d
+                {Compare.eq, big1, big1d, true},
+                {Compare.neq, big1, big1d, false},
+                {Compare.gt, big1, big1d, false},
+                {Compare.lt, big1, big1d, false},
+                {Compare.gte, big1, big1d, true},
+                {Compare.lte, big1, big1d, true},
+                // big1 < big2
+                {Compare.eq, big1, big2, false},
+                {Compare.neq, big1, big2, true},
+                {Compare.gt, big1, big2, false},
+                {Compare.lt, big1, big2, true},
+                {Compare.gte, big1, big2, false},
+                {Compare.lte, big1, big2, true},
+                // Reverse the operands for symmetric test coverage (big2 > big1)
+                {Compare.eq, big2, big1, false},
+                {Compare.neq, big2, big1, true},
+                {Compare.gt, big2, big1, true},
+                {Compare.lt, big2, big1, false},
+                {Compare.gte, big2, big1, true},
+                {Compare.lte, big2, big1, false},
+        }));
         return testCases;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/11845b7a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/PTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/PTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/PTest.java
index 3810c96..629fabc 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/PTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/PTest.java
@@ -29,7 +29,10 @@ import java.util.Arrays;
 import java.util.Random;
 import java.util.function.Predicate;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 /**
  * @author Daniel Kuppitz (http://gremlin.guru)
@@ -85,6 +88,26 @@ public class PTest {
                 {P.between("m", "n").or(P.eq("daniel")), "marko", true},
                 {P.between("m", "n").or(P.eq("daniel")), "daniel", true},
                 {P.between("m", "n").or(P.eq("daniel")), "stephen", false},
+                {P.type(Integer.class), 1, true},
+                {P.type(Integer.class), 1L, false},
+                {P.type(Long.class), 1, false},
+                {P.type(Long.class), 1L, true},
+                {P.type(Number.class), 1, true},
+                {P.type(Number.class), 1L, true},
+                {P.type(String.class), 1, false},
+                {P.type(String.class), 1L, false},
+                {P.type(Number.class), null, true},
+                {P.type(String.class), null, true},
+                {P.notType(Integer.class), 1, false},
+                {P.notType(Integer.class), 1L, true},
+                {P.notType(Long.class), 1, true},
+                {P.notType(Long.class), 1L, false},
+                {P.notType(Number.class), 1, false},
+                {P.notType(Number.class), 1L, false},
+                {P.notType(String.class), 1, true},
+                {P.notType(String.class), 1L, true},
+                {P.notType(Number.class), null, false},
+                {P.notType(String.class), null, false},
         }));
     }