Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

Commit 4ac70d1

Browse files
chore: Review comments, share code between equals and compare
1 parent 8ffe5b2 commit 4ac70d1

File tree

4 files changed

+15
-27
lines changed

4 files changed

+15
-27
lines changed

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonDecimal.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,8 @@ public String toString() {
117117
}
118118

119119
public boolean equals(Object o) {
120-
if (o instanceof PythonInteger other) {
121-
return value.compareTo(new BigDecimal(other.value)) == 0;
122-
} else if (o instanceof PythonFloat other) {
123-
return value.doubleValue() == other.value;
124-
} else if (o instanceof PythonDecimal other) {
125-
return value.compareTo(other.value) == 0;
120+
if (o instanceof PythonNumber number) {
121+
return compareTo(number) == 0;
126122
} else {
127123
return false;
128124
}

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonFloat.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ private static PythonLikeType registerMethods() throws NoSuchMethodException {
6565
case "-inf", "-infinity" -> "-Infinity";
6666
default -> str.value;
6767
};
68-
Double.valueOf("2");
6968
return new PythonFloat(Double.parseDouble(literal));
7069
} catch (NumberFormatException e) {
7170
throw new ValueError("invalid literal for float(): %s".formatted(value));
@@ -232,14 +231,10 @@ public String toString() {
232231

233232
@Override
234233
public boolean equals(Object o) {
235-
if (o instanceof Number) {
236-
return ((Number) o).doubleValue() == value;
237-
} else if (o instanceof PythonFloat) {
238-
return ((PythonFloat) o).value == value;
239-
} else if (o instanceof PythonInteger) {
240-
return ((PythonInteger) o).getValue().doubleValue() == value;
241-
} else if (o instanceof PythonDecimal other) {
242-
return new BigDecimal(value).equals(other.value);
234+
if (o instanceof Number number) {
235+
return number.doubleValue() == value;
236+
} else if (o instanceof PythonNumber number) {
237+
return compareTo(number) == 0;
243238
} else {
244239
return false;
245240
}

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonInteger.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,10 @@ public byte asByte() {
276276

277277
@Override
278278
public boolean equals(Object o) {
279-
if (o instanceof Number) {
280-
return value.equals(BigInteger.valueOf(((Number) o).longValue()));
281-
} else if (o instanceof PythonInteger other) {
282-
return other.value.equals(value);
283-
} else if (o instanceof PythonFloat other) {
284-
return value.doubleValue() == other.value;
285-
} else if (o instanceof PythonDecimal other) {
286-
return new BigDecimal(value).equals(other.value);
279+
if (o instanceof Number number) {
280+
return value.equals(BigInteger.valueOf(number.longValue()));
281+
} else if (o instanceof PythonNumber number) {
282+
return compareTo(number) == 0;
287283
} else {
288284
return false;
289285
}

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric/PythonNumber.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ default int compareTo(PythonNumber pythonNumber) {
2424
if (value instanceof BigInteger self) {
2525
if (otherValue instanceof BigInteger other) {
2626
return self.compareTo(other);
27-
} else {
28-
return Double.compare(value.longValue(), otherValue.doubleValue());
27+
} else if (otherValue instanceof BigDecimal other) {
28+
return new BigDecimal(self).compareTo(other);
2929
}
3030
}
3131
if (value instanceof BigDecimal self) {
3232
if (otherValue instanceof BigDecimal other) {
3333
return self.compareTo(other);
34-
} else {
35-
return Double.compare(value.doubleValue(), otherValue.doubleValue());
34+
} else if (otherValue instanceof BigInteger other) {
35+
return self.compareTo(new BigDecimal(other));
3636
}
3737
}
38+
// If comparing against a float, convert both arguments to float
3839
return Double.compare(value.doubleValue(), otherValue.doubleValue());
3940
}
4041

0 commit comments

Comments
 (0)