You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2015/08/31 22:46:00 UTC

[2/4] incubator-tinkerpop git commit: TINKERPOP3-750: Fix bug in Compare for BigInteger/BigDecimal by converting Number to BigDecimal

TINKERPOP3-750: Fix bug in Compare for BigInteger/BigDecimal by converting Number to BigDecimal


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

Branch: refs/heads/tp30
Commit: 378dd6cb920c9ac82ce53cc599882bf0704a3ab6
Parents: e191717
Author: mhfrantz <mf...@redsealnetworks.com>
Authored: Mon Aug 31 13:25:40 2015 -0700
Committer: mhfrantz <mf...@redsealnetworks.com>
Committed: Mon Aug 31 13:26:06 2015 -0700

----------------------------------------------------------------------
 .../gremlin/process/traversal/Compare.java      | 47 ++++++++++++--------
 1 file changed, 28 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/378dd6cb/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 aad8371..262347f 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
@@ -18,6 +18,7 @@
  */
 package org.apache.tinkerpop.gremlin.process.traversal;
 
+import java.math.BigDecimal;
 import java.util.function.BiPredicate;
 
 /**
@@ -26,12 +27,13 @@ import java.util.function.BiPredicate;
  *
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  * @author Stephen Mallette (http://stephen.genoprime.com)
+ * @author Matt Frantz (http://github.com/mhfrantz)
  */
 public enum Compare implements BiPredicate<Object, Object> {
     /**
      * Evaluates if the first object is equal to the second.  If both are of type {@link Number} but not of the
-     * same class (i.e. double for the first object and long for the second object) both values are cast to
-     * {@link Number} so that it can be evaluated via {@link Number#doubleValue()}.  Otherwise they are evaluated
+     * same class (i.e. double for the first object and long for the second object) both values are converted to
+     * {@link BigDecimal} so that it can be evaluated via {@link BigDecimal#compareTo()}.  Otherwise they are evaluated
      * via {@link Object#equals(Object)}.  Testing against {@link Number#doubleValue()} enables the compare
      * operations to be a bit more forgiving with respect to comparing different number types.
      */
@@ -40,7 +42,7 @@ public enum Compare implements BiPredicate<Object, Object> {
         public boolean test(final Object first, final Object second) {
             return null == first ? null == second : (first instanceof Number && second instanceof Number
                     && !first.getClass().equals(second.getClass())
-                    ? ((Number) first).doubleValue() == ((Number) second).doubleValue()
+                    ? big((Number) first).compareTo(big((Number) second)) == 0
                     : first.equals(second));
         }
 
@@ -55,8 +57,8 @@ public enum Compare implements BiPredicate<Object, Object> {
 
     /**
      * Evaluates if the first object is not equal to the second.  If both are of type {@link Number} but not of the
-     * same class (i.e. double for the first object and long for the second object) both values are cast to
-     * {@link Number} so that it can be evaluated via {@link Number#doubleValue()}.  Otherwise they are evaluated
+     * same class (i.e. double for the first object and long for the second object) both values are converted to
+     * {@link BigDecimal} so that it can be evaluated via {@link BigDecimal#equals()}.  Otherwise they are evaluated
      * via {@link Object#equals(Object)}.  Testing against {@link Number#doubleValue()} enables the compare
      * operations to be a bit more forgiving with respect to comparing different number types.
      */
@@ -77,9 +79,9 @@ public enum Compare implements BiPredicate<Object, Object> {
 
     /**
      * Evaluates if the first object is greater than the second.  If both are of type {@link Number} but not of the
-     * same class (i.e. double for the first object and long for the second object) both values are cast to
-     * {@link Number} so that it can be evaluated via {@link Number#doubleValue()}.  Otherwise they are evaluated
-     * via {@link Comparable#compareTo(Object)}.  Testing against {@link Number#doubleValue()} enables the compare
+     * same class (i.e. double for the first object and long for the second object) both values are converted to
+     * {@link BigDecimal} so that it can be evaluated via {@link BigDecimal#compareTo()}.  Otherwise they are evaluated
+     * via {@link Comparable#compareTo(Object)}.  Testing against {@link BigDecimal#compareTo()} enables the compare
      * operations to be a bit more forgiving with respect to comparing different number types.
      */
     gt {
@@ -87,7 +89,7 @@ public enum Compare implements BiPredicate<Object, Object> {
         public boolean test(final Object first, final Object second) {
             return null != first && null != second && (
                     first instanceof Number && second instanceof Number && !first.getClass().equals(second.getClass())
-                            ? ((Number) first).doubleValue() > ((Number) second).doubleValue()
+                            ? big((Number) first).compareTo(big((Number) second)) > 0
                             : ((Comparable) first).compareTo(second) > 0);
         }
 
@@ -102,9 +104,9 @@ public enum Compare implements BiPredicate<Object, Object> {
 
     /**
      * Evaluates if the first object is greater-equal to the second.  If both are of type {@link Number} but not of the
-     * same class (i.e. double for the first object and long for the second object) both values are cast to
-     * {@link Number} so that it can be evaluated via {@link Number#doubleValue()}.  Otherwise they are evaluated
-     * via {@link Comparable#compareTo(Object)}.  Testing against {@link Number#doubleValue()} enables the compare
+     * same class (i.e. double for the first object and long for the second object) both values are converted to
+     * {@link BigDecimal} so that it can be evaluated via {@link BigDecimal#compareTo()}.  Otherwise they are evaluated
+     * via {@link Comparable#compareTo(Object)}.  Testing against {@link BigDecimal#compareTo()} enables the compare
      * operations to be a bit more forgiving with respect to comparing different number types.
      */
     gte {
@@ -124,9 +126,9 @@ public enum Compare implements BiPredicate<Object, Object> {
 
     /**
      * Evaluates if the first object is less than the second.  If both are of type {@link Number} but not of the
-     * same class (i.e. double for the first object and long for the second object) both values are cast to
-     * {@link Number} so that it can be evaluated via {@link Number#doubleValue()}.  Otherwise they are evaluated
-     * via {@link Comparable#compareTo(Object)}.  Testing against {@link Number#doubleValue()} enables the compare
+     * same class (i.e. double for the first object and long for the second object) both values are converted to
+     * {@link BigDecimal} so that it can be evaluated via {@link BigDecimal#compareTo()}.  Otherwise they are evaluated
+     * via {@link Comparable#compareTo(Object)}.  Testing against {@link BigDecimal#compareTo()} enables the compare
      * operations to be a bit more forgiving with respect to comparing different number types.
      */
     lt {
@@ -134,7 +136,7 @@ public enum Compare implements BiPredicate<Object, Object> {
         public boolean test(final Object first, final Object second) {
             return null != first && null != second && (
                     first instanceof Number && second instanceof Number && !first.getClass().equals(second.getClass())
-                            ? ((Number) first).doubleValue() < ((Number) second).doubleValue()
+                            ? big((Number) first).compareTo(big((Number) second)) < 0
                             : ((Comparable) first).compareTo(second) < 0);
         }
 
@@ -149,9 +151,9 @@ public enum Compare implements BiPredicate<Object, Object> {
 
     /**
      * Evaluates if the first object is less-equal to the second.  If both are of type {@link Number} but not of the
-     * same class (i.e. double for the first object and long for the second object) both values are cast to
-     * {@link Number} so that it can be evaluated via {@link Number#doubleValue()}.  Otherwise they are evaluated
-     * via {@link Comparable#compareTo(Object)}.  Testing against {@link Number#doubleValue()} enables the compare
+     * same class (i.e. double for the first object and long for the second object) both values are converted to
+     * {@link BigDecimal} so that it can be evaluated via {@link BigDecimal#compareTo()}.  Otherwise they are evaluated
+     * via {@link Comparable#compareTo(Object)}.  Testing against {@link BigDecimal#compareTo()} enables the compare
      * operations to be a bit more forgiving with respect to comparing different number types.
      */
     lte {
@@ -174,4 +176,11 @@ public enum Compare implements BiPredicate<Object, Object> {
      */
     @Override
     public abstract Compare negate();
+
+    /**
+     * Convert Number to BigDecimal.
+     */
+    private static BigDecimal big(final Number n) {
+        return new BigDecimal(n.toString());
+    }
 }