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/23 16:58:04 UTC

incubator-tinkerpop git commit: Reimplemented `P.type()`. It no longer depends on `Compare` and accepts `String` arguments in order to support non JVM languages.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-818 7bde6fae2 -> d93c5217d


Reimplemented `P.type()`. It no longer depends on `Compare` and accepts `String` arguments in order to support non JVM languages.


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

Branch: refs/heads/TINKERPOP-818
Commit: d93c5217d367047739404dd4a36048d67bf69878
Parents: 7bde6fa
Author: Daniel Kuppitz <da...@hotmail.com>
Authored: Sat Jan 23 16:56:00 2016 +0100
Committer: Daniel Kuppitz <da...@hotmail.com>
Committed: Sat Jan 23 16:56:00 2016 +0100

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |  2 +-
 docs/src/reference/the-traversal.asciidoc       |  1 -
 .../gremlin/process/traversal/Compare.java      | 41 ------------
 .../tinkerpop/gremlin/process/traversal/P.java  |  8 +--
 .../gremlin/process/traversal/Type.java         | 67 ++++++++++++++++++++
 .../gremlin/process/traversal/CompareTest.java  | 12 ----
 .../gremlin/process/traversal/PTest.java        | 20 +++---
 7 files changed, 80 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/d93c5217/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 3ee1d33..d8c4eac 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -26,7 +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()`.
+* Added `P.type()`.
 * 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/d93c5217/docs/src/reference/the-traversal.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/reference/the-traversal.asciidoc b/docs/src/reference/the-traversal.asciidoc
index 541e146..e2bce36 100644
--- a/docs/src/reference/the-traversal.asciidoc
+++ b/docs/src/reference/the-traversal.asciidoc
@@ -2024,7 +2024,6 @@ provided predicates are outlined in the table below and are used in various step
 | `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?
-| `notType(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/d93c5217/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 78004f9..97b52b8 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,47 +169,6 @@ 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/d93c5217/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 ff6f6d5..4a8372f 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
@@ -181,11 +181,7 @@ public class P<V> implements Predicate<V>, Serializable, Cloneable {
         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);
+    public static <V> P<V> type(final Object value) {
+        return new P(Type.instanceOf, value);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/d93c5217/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Type.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Type.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Type.java
new file mode 100644
index 0000000..876abe0
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Type.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.process.traversal;
+
+import org.slf4j.LoggerFactory;
+
+import java.util.function.BiPredicate;
+
+/**
+ * {@link org.apache.tinkerpop.gremlin.process.traversal.Type} is a {@link java.util.function.BiPredicate} that
+ * evaluates whether the first object is an instance of the class given in the second argument. If the second argument
+ * is an instance of {@link java.lang.String}, then it will be handled as a class name. If it's neither an instance of
+ * {@link java.lang.Class} nor an instance of {@link java.lang.String}, then the objects class will be used.
+ * <p/>
+ * <pre>
+ * "gremlin" Type.instanceOf String == true
+ * "gremlin" Type.instanceOf Number == false
+ * "gremlin" Type.instanceOf "java.lang.String" == true
+ * </pre>
+ *
+ * @author Daniel Kuppitz (http://gremlin.guru)
+ */
+public enum Type implements BiPredicate<Object, Object> {
+
+    /**
+     * The first object is within the {@link java.util.Collection} provided in the second object.
+     */
+    instanceOf {
+        @Override
+        public boolean test(final Object first, final Object second) {
+            try {
+                return first == null || asClass(second).isInstance(first);
+            } catch (ClassNotFoundException e) {
+                LoggerFactory.getLogger(Type.class).error(e.getMessage());
+                return false;
+            }
+        }
+    };
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public abstract boolean test(final Object first, final Object second);
+
+    private static Class asClass(final Object obj) throws ClassNotFoundException {
+        if (obj instanceof Class) return (Class) obj;
+        if (obj instanceof String) return Class.forName((String) obj);
+        return obj.getClass();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/d93c5217/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 5cefa80..e87aa6c 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
@@ -88,18 +88,6 @@ public class CompareTest {
                 {Compare.lte, 100, 101, true},
                 {Compare.lte, "z", "a", false},
                 {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);

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/d93c5217/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 629fabc..322c856 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
@@ -98,16 +98,16 @@ public class PTest {
                 {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},
+                {P.type(Integer.class.getCanonicalName()), 1, true},
+                {P.type(Integer.class.getCanonicalName()), 1L, false},
+                {P.type(Long.class.getCanonicalName()), 1, false},
+                {P.type(Long.class.getCanonicalName()), 1L, true},
+                {P.type(Number.class.getCanonicalName()), 1, true},
+                {P.type(Number.class.getCanonicalName()), 1L, true},
+                {P.type(String.class.getCanonicalName()), 1, false},
+                {P.type(String.class.getCanonicalName()), 1L, false},
+                {P.type(Number.class.getCanonicalName()), null, true},
+                {P.type(String.class.getCanonicalName()), null, true},
         }));
     }