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
Use Comparable instead of dedicated implementations #25478
Conversation
@wind57 Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@wind57 Thank you for signing the Contributor License Agreement! |
@@ -63,8 +64,9 @@ public BooleanComparator(boolean trueLow) { | |||
|
|||
|
|||
@Override | |||
public int compare(Boolean v1, Boolean v2) { | |||
return (v1 ^ v2) ? ((v1 ^ this.trueLow) ? 1 : -1) : 0; |
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.
some time ago I had an answer about this here : https://stackoverflow.com/questions/55598277/is-there-a-useful-difference-between-p-q-and-p-q-for-booleans/55601399#55601399 and it turns out that the performance of XOR vs plain !=
is same. This also makes it far easier to read
@@ -76,7 +78,7 @@ public boolean equals(@Nullable Object other) { | |||
|
|||
@Override | |||
public int hashCode() { | |||
return getClass().hashCode() * (this.trueLow ? -1 : 1); |
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.
since java-8
there is a way to get the hashCode without calling into getClass
@@ -29,34 +29,35 @@ | |||
* @author Keith Donald | |||
* @author Chris Beams | |||
* @author Phillip Webb | |||
* @author Eugene Rabii | |||
*/ | |||
class BooleanComparatorTests { | |||
|
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.
tests have changed because the documentation of Boolean::compare
does not guarantee -1, 0, 1
; but it does less than 1, 0, greater than 1
Will provide a broader PR, against the entire package. |
@wind57 please do not do that. If you want to update your PR, you can push more changes to your |
@@ -29,49 +29,44 @@ | |||
|
|||
/** | |||
* Return a {@link Comparable} adapter. |
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.
I've used Comparator::naturalOrder
and Comparator::nullLast/nullFirst
instead of NullSafeComparator
@snicoll thank you, I am still new, not sure about the process yet. re-opened. |
This commit deprecates ComparableComparator and NullSafeComparator in favor of the available convenient implementation in the JDK. See gh-25478
@wind57 thanks very much for making your first contribution to the Spring Framework. I've polished your change and deprecated those two implementations as we can now replace them by the convenience method in the JDK. |
No description provided.