diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/language/grammar/ArgumentVisitor.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/language/grammar/ArgumentVisitor.java index 9c0a1eca4e2..e9eec826f4b 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/language/grammar/ArgumentVisitor.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/language/grammar/ArgumentVisitor.java @@ -20,11 +20,9 @@ import org.apache.tinkerpop.gremlin.process.traversal.step.GType; import org.apache.tinkerpop.gremlin.process.traversal.step.GValue; -import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; import java.lang.reflect.Array; -import java.time.OffsetDateTime; import java.util.Map; import java.util.Objects; 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 a878bbb8396..ecb2622140b 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 @@ -77,15 +77,11 @@ public void setValue(final V value) { @Override public boolean test(final V testValue) { - if (this.value instanceof GValue) { - return this.biPredicate.test(testValue, ((GValue) this.value).get()); + // this might be a bunch of GValue that need to be resolved. zomg + if (this.value instanceof List) { + return this.biPredicate.test(testValue, (V) ((List) this.value).stream().map(GValue::valueOf).collect(Collectors.toList())); } else { - // this might be a bunch of GValue that need to be resolved. zomg - if (this.value instanceof List) { - return this.biPredicate.test(testValue, (V) ((List) this.value).stream().map(GValue::valueOf).collect(Collectors.toList())); - } else { - return this.biPredicate.test(testValue, this.value); - } + return this.biPredicate.test(testValue, (V) GValue.valueOf(this.value)); } } diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValue.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValue.java index b9f88684e3f..127a8e71b94 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValue.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValue.java @@ -117,6 +117,11 @@ public int hashCode() { return Objects.hash(name, type, value); } + @Override + public GValue clone() { + return new GValue<>(this.name, this.type, this.value); + } + /** * Create a new {@code Var} from a particular value but without the specified name. If the argument provide is * already a {@code GValue} then it is returned as-is. diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/Parameters.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/Parameters.java index 9b4a926c742..6c775a53f40 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/Parameters.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/Parameters.java @@ -189,10 +189,8 @@ public Object[] getKeyValues(final Traverser.Admin traverser, final Objec private static Object resolve(final Object object, final Traverser.Admin traverser) { if (object instanceof Traversal.Admin) { return TraversalUtil.apply(traverser, (Traversal.Admin) object); - } else if (object instanceof GValue) { - return ((GValue) object).get(); } else { - return object; + return GValue.valueOf(object); } } diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SideEffectStrategy.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SideEffectStrategy.java index 9b309dba3f1..f4ed5e9b267 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SideEffectStrategy.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SideEffectStrategy.java @@ -64,8 +64,10 @@ public static void addSideEffect(final TraversalStrategies traversalStrategi // don't want the GValue to leak beyond strategy application or else the Supplier will start producing it // during execution - final ConstantSupplier initialValue = value instanceof GValue ? new ConstantSupplier<>(((GValue) value).get()) : new ConstantSupplier<>(value); - strategy.sideEffects.add(new Triplet<>(key, value instanceof Supplier ? (Supplier) value : initialValue, reducer)); + strategy.sideEffects.add(new Triplet<>(key, () -> { + Object initialValue = value instanceof Supplier ? ((Supplier) value).get() : value; + return GValue.valueOf(initialValue); + }, reducer)); } public boolean contains(final String sideEffectKey) { diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/CollectionUtil.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/CollectionUtil.java index 27d41d8e181..1efc1367b36 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/CollectionUtil.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/CollectionUtil.java @@ -102,11 +102,7 @@ public static ConcurrentHashMap clone(final ConcurrentHashMap ma * element as a new array with just that item in it. */ public static T[] addFirst(final T[] array, final T element, final Class clazz) { - if (null == array && null == element) { - final T[] singleElementArray = (T[]) Array.newInstance(clazz, 1); - singleElementArray[0] = null; - return singleElementArray; - } else if (null == array) { + if (null == array) { final T[] singleElementArray = (T[]) Array.newInstance(clazz, 1); singleElementArray[0] = element; return singleElementArray; diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValueTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValueTest.java index 0335d4a89cf..3e8cfac0de7 100644 --- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValueTest.java +++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/GValueTest.java @@ -638,4 +638,28 @@ public void numberOfShouldThrowBecauseNullIntegerGType() { public void numberOfShouldThrowBecauseNullObject() { GValue.numberOf(null); } + + @Test + public void numberOfShouldAcceptAllNumericTypeLiterals() { + assertEquals((byte) 123, GValue.numberOf((byte) 123)); + assertEquals((short) 123, GValue.numberOf((short) 123)); + assertEquals(123, GValue.numberOf(123)); + assertEquals(123l, GValue.numberOf(123l)); + assertEquals(123.45f, GValue.numberOf(123.45f)); + assertEquals(123.45, GValue.numberOf(123.45)); + assertEquals(BigInteger.ONE, GValue.numberOf(BigInteger.ONE)); + assertEquals(BigDecimal.ONE, GValue.numberOf(BigDecimal.ONE)); + } + + @Test + public void numberOfShouldAcceptAllNumericTypeGValues() { + assertEquals((byte) 123, GValue.numberOf(GValue.of((byte) 123))); + assertEquals((short) 123, GValue.numberOf((GValue.of((short) 123)))); + assertEquals(123, GValue.numberOf((GValue.of(123)))); + assertEquals(123l, GValue.numberOf((GValue.of(123l)))); + assertEquals(123.45f, GValue.numberOf((GValue.of(123.45f)))); + assertEquals(123.45, GValue.numberOf((GValue.of(123.45)))); + assertEquals(BigInteger.ONE, GValue.numberOf((GValue.of(BigInteger.ONE)))); + assertEquals(BigDecimal.ONE, GValue.numberOf((GValue.of(BigDecimal.ONE)))); + } } \ No newline at end of file