# Replace equals() on Enum Constants
# Description
Replaces occurrences of equals()
on Enum constants with an identity comparison (==
).
In the case the equals relation is wrapped with an boolean negation the result will be an not equals (!=
).
# Benefits
Technically, equals and ==
are the same. ==
however is null safe and thus can help avoid programming errors.
# Tags
# Code Changes
# Enum in right-hand-side
Pre
public boolean isRoundUp(RoundingMode roundingMode) {
return roundingMode.equals(RoundingMode.UP);
}
Post
public boolean isRoundUp(RoundingMode roundingMode) {
return roundingMode == RoundingMode.UP;
}
# Enum in left-hand-side
Pre
public boolean isRoundUp(RoundingMode roundingMode) {
return RoundingMode.UP.equals(roundingMode);
}
Post
public boolean isRoundUp(RoundingMode roundingMode) {
return RoundingMode.UP == roundingMode;
}
# Enum in infix expression
Pre
public boolean isRoundUp(RoundingMode roundingMode) {
return !RoundingMode.UP.equals(roundingMode);
}
Post
public boolean isRoundUp(RoundingMode roundingMode) {
return !(RoundingMode.UP == roundingMode);
}
You Want To Have Those Changes Done Automatically?
The automatic application of this rule is supported in the following jSparrow versions: