Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* {@code true} or {@code false} first.
*
* @author Keith Donald
* @author Eugene Rabii
* @since 1.2.2
*/
@SuppressWarnings("serial")
Expand Down Expand Up @@ -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;
Copy link
Contributor Author

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

public int compare(Boolean left, Boolean right) {
int multiplier = trueLow ? -1 : 1;
return multiplier * Boolean.compare(left, right);
}


Expand All @@ -76,7 +78,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return getClass().hashCode() * (this.trueLow ? -1 : 1);
Copy link
Contributor Author

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

return Boolean.hashCode(trueLow);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,35 @@
* @author Keith Donald
* @author Chris Beams
* @author Phillip Webb
* @author Eugene Rabii
*/
class BooleanComparatorTests {

Copy link
Contributor Author

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

@Test
void shouldCompareWithTrueLow() {
Comparator<Boolean> c = new BooleanComparator(true);
assertThat(c.compare(true, false)).isEqualTo(-1);
assertThat(c.compare(true, false)).isLessThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
}

@Test
void shouldCompareWithTrueHigh() {
Comparator<Boolean> c = new BooleanComparator(false);
assertThat(c.compare(true, false)).isEqualTo(1);
assertThat(c.compare(true, false)).isGreaterThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
}

@Test
void shouldCompareFromTrueLow() {
Comparator<Boolean> c = BooleanComparator.TRUE_LOW;
assertThat(c.compare(true, false)).isEqualTo(-1);
assertThat(c.compare(true, false)).isLessThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
}

@Test
void shouldCompareFromTrueHigh() {
Comparator<Boolean> c = BooleanComparator.TRUE_HIGH;
assertThat(c.compare(true, false)).isEqualTo(1);
assertThat(c.compare(true, false)).isGreaterThan(0);
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE)).isEqualTo(0);
}

Expand Down