Skip to content

Commit

Permalink
Introduce PredicateIsEqualEnums
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah committed Jul 8, 2024
1 parent 8c3756c commit 2c79bde
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.function.Predicate.isEqual;
import static java.util.function.Predicate.not;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
Expand Down Expand Up @@ -43,6 +44,21 @@ boolean after(T a, T b) {
}
}

/**
* Prefer reference-based equality for enums over {@link Predicate#isEqual(Object)} comparison.
*/
static final class PredicateIsEqualEnums<T extends Enum<T>> {
@BeforeTemplate
Predicate<T> before(T a) {
return isEqual(a);
}

@AfterTemplate
Predicate<T> after(T a) {
return v -> v == a;
}
}

/** Prefer {@link Object#equals(Object)} over the equivalent lambda function. */
// XXX: As it stands, this rule is a special case of what `MethodReferenceUsage` tries to achieve.
// If/when `MethodReferenceUsage` becomes production ready, we should simply drop this check.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.function.Predicate.isEqual;
import static java.util.function.Predicate.not;

import com.google.common.collect.BoundType;
Expand All @@ -14,7 +15,7 @@
final class EqualityRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Objects.class, Optional.class, not(null));
return ImmutableSet.of(Objects.class, Optional.class, isEqual(null), not(null));
}

ImmutableSet<Boolean> testPrimitiveOrReferenceEquality() {
Expand All @@ -33,6 +34,10 @@ boolean testEqualsPredicate() {
return Stream.of("foo").anyMatch(s -> "bar".equals(s));
}

boolean testPredicateIsEqualEnums() {
return Stream.of(RoundingMode.UP).anyMatch(isEqual(RoundingMode.DOWN));
}

boolean testDoubleNegation() {
return !!Boolean.TRUE;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.function.Predicate.isEqual;
import static java.util.function.Predicate.not;

import com.google.common.collect.BoundType;
Expand All @@ -14,7 +15,7 @@
final class EqualityRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Objects.class, Optional.class, not(null));
return ImmutableSet.of(Objects.class, Optional.class, isEqual(null), not(null));
}

ImmutableSet<Boolean> testPrimitiveOrReferenceEquality() {
Expand All @@ -33,6 +34,10 @@ boolean testEqualsPredicate() {
return Stream.of("foo").anyMatch("bar"::equals);
}

boolean testPredicateIsEqualEnums() {
return Stream.of(RoundingMode.UP).anyMatch(v -> v == RoundingMode.DOWN);
}

boolean testDoubleNegation() {
return Boolean.TRUE;
}
Expand Down

0 comments on commit 2c79bde

Please sign in to comment.