-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support to BigDecimal
in JsonPrimitive#equals
#2364
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6b051a8
Adds support to `BigDecimal`
MaicolAntali fdc3ca4
Adds test
MaicolAntali 3aee514
Implements review comments
MaicolAntali 500535e
Changes to follow the google-style-guide
MaicolAntali e0788ec
implements review comment
MaicolAntali 3c364b9
Merge branch 'master' into fix-904
MaicolAntali 85cab67
Merge branch 'main' into fix-904
MaicolAntali d67e4b8
Fixes the `OperatorPrecedence` warn
MaicolAntali f91c2dc
Implements code improvements
MaicolAntali 98bec86
Implements `BigDecimal` check in the `JsonPrimitive.equals()`
MaicolAntali 01a9fa7
Merge branch 'main' into fix-904
MaicolAntali c15fb57
Formats the code with `spotless:apply`
MaicolAntali d59f69b
Merge branch 'main' into fix-904
MaicolAntali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -321,4 +321,35 @@ public void testDeepCopy() { | |||||||||||
JsonPrimitive a = new JsonPrimitive("a"); | ||||||||||||
assertThat(a).isSameInstanceAs(a.deepCopy()); // Primitives are immutable! | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Test | ||||||||||||
public void testBigDecimalEquals() { | ||||||||||||
JsonPrimitive small = new JsonPrimitive(1.0); | ||||||||||||
JsonPrimitive large = new JsonPrimitive(2.0); | ||||||||||||
assertThat(small.equals(large)).isFalse(); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assert probably is overkill because is already tested here: gson/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java Lines 246 to 250 in 3adead6
It's here to be consistent with the code in the issue #904 |
||||||||||||
|
||||||||||||
BigDecimal doubleMax = BigDecimal.valueOf(Double.MAX_VALUE); | ||||||||||||
JsonPrimitive smallBD = new JsonPrimitive(doubleMax.add(new BigDecimal("100.0"))); | ||||||||||||
JsonPrimitive largeBD = new JsonPrimitive(doubleMax.add(new BigDecimal("200.0"))); | ||||||||||||
assertThat(smallBD.equals(largeBD)).isFalse(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Test | ||||||||||||
public void testBigDecimalEqualsZero() { | ||||||||||||
assertThat( | ||||||||||||
new JsonPrimitive(new BigDecimal("0.0")) | ||||||||||||
.equals(new JsonPrimitive(new BigDecimal("0.00")))) | ||||||||||||
.isTrue(); | ||||||||||||
|
||||||||||||
assertThat( | ||||||||||||
new JsonPrimitive(new BigDecimal("0.00")) | ||||||||||||
.equals(new JsonPrimitive(Double.valueOf("0.00")))) | ||||||||||||
.isTrue(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Test | ||||||||||||
public void testEqualsDoubleNaNAndBigDecimal() { | ||||||||||||
assertThat(new JsonPrimitive(Double.NaN).equals(new JsonPrimitive(new BigDecimal("1.0")))) | ||||||||||||
.isFalse(); | ||||||||||||
} | ||||||||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As side note / for future reference: I originally proposed using
compareTo
when this code was also comparingBigDecimal
with non-BigDecimal
, see #2364 (comment).However, using
compareTo
instead ofequals
here has another advantage: It ensuresJsonPrimitive.equals
is transitive (as required byObject.equals
), at least when wrapping aBigDecimal
.Now let
x = new JsonPrimitive(new BigDecimal("0"))
y = new JsonPrimitive(0.0d)
z = new JsonPrimitive(new BigDecimal("0.00"))
Using
compareTo
makes sure thatequals
is transitive. Whereas usingBigDecimal.equals
here would cause:x equals y
andy equals z
(because they fall back todouble
comparison), butx !equals z
, which would not be transitive.