Skip to content

Commit

Permalink
Implements code improvements
Browse files Browse the repository at this point in the history
- Extracts `thisAsDouble` & `otherAsDouble` variables to avoid double functions calls.

- Adds a comment to improve the code readability.
  • Loading branch information
MaicolAntali committed Oct 26, 2023
1 parent d67e4b8 commit f91c2dc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions gson/src/main/java/com/google/gson/JsonPrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,14 @@ public boolean equals(Object obj) {
: this.getAsNumber().longValue() == other.getAsNumber().longValue();
}
if (value instanceof Number && other.value instanceof Number) {
double thisAsDouble = this.getAsDouble();
double otherAsDouble = other.getAsDouble();

return (this.value instanceof BigDecimal && other.value instanceof BigDecimal)
// Uses compareTo to ignore scale of values, e.g. `0` and `0.00` should be considered equal
? this.getAsBigDecimal().compareTo(other.getAsBigDecimal()) == 0
: this.getAsDouble() == other.getAsDouble()
|| (Double.isNaN(this.getAsDouble()) && Double.isNaN(other.getAsDouble()));
: thisAsDouble == otherAsDouble
|| (Double.isNaN(thisAsDouble) && Double.isNaN(otherAsDouble));
}
return value.equals(other.value);
}
Expand Down

0 comments on commit f91c2dc

Please sign in to comment.