# Replace equals() on Enum Constants

# Properties

Property Value
Rule ID EnumsWithoutEquals
First seen in jSparrow version 2.2.0
Minimum Java version 5
Remediation cost 2 min

# 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.

# 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);
}

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow versions:

# Tags

1
default
You & jSparrow
default

Hey there! May I help you? 😊