From 4ac70d1f14ed09860cc648d999aa1649b1861e1d Mon Sep 17 00:00:00 2001 From: Christopher Chianelli Date: Fri, 12 Jul 2024 08:36:09 -0400 Subject: [PATCH] chore: Review comments, share code between equals and compare --- .../jpyinterpreter/types/numeric/PythonDecimal.java | 8 ++------ .../jpyinterpreter/types/numeric/PythonFloat.java | 13 ++++--------- .../jpyinterpreter/types/numeric/PythonInteger.java | 12 ++++-------- .../jpyinterpreter/types/numeric/PythonNumber.java | 9 +++++---- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonDecimal.java b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonDecimal.java index 6083b05..9429195 100644 --- a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonDecimal.java +++ b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonDecimal.java @@ -117,12 +117,8 @@ public String toString() { } public boolean equals(Object o) { - if (o instanceof PythonInteger other) { - return value.compareTo(new BigDecimal(other.value)) == 0; - } else if (o instanceof PythonFloat other) { - return value.doubleValue() == other.value; - } else if (o instanceof PythonDecimal other) { - return value.compareTo(other.value) == 0; + if (o instanceof PythonNumber number) { + return compareTo(number) == 0; } else { return false; } diff --git a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonFloat.java b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonFloat.java index 0ecce3c..7bf79dc 100644 --- a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonFloat.java +++ b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonFloat.java @@ -65,7 +65,6 @@ private static PythonLikeType registerMethods() throws NoSuchMethodException { case "-inf", "-infinity" -> "-Infinity"; default -> str.value; }; - Double.valueOf("2"); return new PythonFloat(Double.parseDouble(literal)); } catch (NumberFormatException e) { throw new ValueError("invalid literal for float(): %s".formatted(value)); @@ -232,14 +231,10 @@ public String toString() { @Override public boolean equals(Object o) { - if (o instanceof Number) { - return ((Number) o).doubleValue() == value; - } else if (o instanceof PythonFloat) { - return ((PythonFloat) o).value == value; - } else if (o instanceof PythonInteger) { - return ((PythonInteger) o).getValue().doubleValue() == value; - } else if (o instanceof PythonDecimal other) { - return new BigDecimal(value).equals(other.value); + if (o instanceof Number number) { + return number.doubleValue() == value; + } else if (o instanceof PythonNumber number) { + return compareTo(number) == 0; } else { return false; } diff --git a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonInteger.java b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonInteger.java index ceaa887..df6f3fe 100644 --- a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonInteger.java +++ b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonInteger.java @@ -276,14 +276,10 @@ public byte asByte() { @Override public boolean equals(Object o) { - if (o instanceof Number) { - return value.equals(BigInteger.valueOf(((Number) o).longValue())); - } else if (o instanceof PythonInteger other) { - return other.value.equals(value); - } else if (o instanceof PythonFloat other) { - return value.doubleValue() == other.value; - } else if (o instanceof PythonDecimal other) { - return new BigDecimal(value).equals(other.value); + if (o instanceof Number number) { + return value.equals(BigInteger.valueOf(number.longValue())); + } else if (o instanceof PythonNumber number) { + return compareTo(number) == 0; } else { return false; } diff --git a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonNumber.java b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonNumber.java index 5681925..3c5cb4f 100644 --- a/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonNumber.java +++ b/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonNumber.java @@ -24,17 +24,18 @@ default int compareTo(PythonNumber pythonNumber) { if (value instanceof BigInteger self) { if (otherValue instanceof BigInteger other) { return self.compareTo(other); - } else { - return Double.compare(value.longValue(), otherValue.doubleValue()); + } else if (otherValue instanceof BigDecimal other) { + return new BigDecimal(self).compareTo(other); } } if (value instanceof BigDecimal self) { if (otherValue instanceof BigDecimal other) { return self.compareTo(other); - } else { - return Double.compare(value.doubleValue(), otherValue.doubleValue()); + } else if (otherValue instanceof BigInteger other) { + return self.compareTo(new BigDecimal(other)); } } + // If comparing against a float, convert both arguments to float return Double.compare(value.doubleValue(), otherValue.doubleValue()); }