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

Suggest introducing an attribute for enum values #493

Closed
Luro02 opened this issue Apr 17, 2024 · 0 comments
Closed

Suggest introducing an attribute for enum values #493

Luro02 opened this issue Apr 17, 2024 · 0 comments
Labels
enhancement New feature or request new-lint A new lint.

Comments

@Luro02
Copy link
Collaborator

Luro02 commented Apr 17, 2024

What it does

In Java, one can not only have a simple enumeration like

enum Color {
  RED,
  GREEN,
  BLUE;
}

but also have attributes:

enum Color {
  RED(0xff0000),
  GREEN(0x00ff00),
  BLUE(0x0000ff);

  private int value;

  Color(int value) {
    this.value = value;
  }
}

Sometimes this is not used, like here:

private static int toNumber(Color color) {
    return switch (color) {
      case RED -> 0xff0000;
      case GREEN -> 0x00ff00;
      case BLUE -> 0x0000ff;
    };
}

which is unnecessary, because the constant values on the right could be attributes.

The same is for the inverse where the constant is on the left and the enum on the right.

Note: Those cases should be ignored by the ClosedSetShouldBeEnum check.

Alternatively one could suggest a constant map in the form Map<Enum, Constant> or Map<Constant, Enum>.

Lint Name

CONSTANT_SHOULD_BE_ENUM_ATTRIBUTE

Category

oop

Example

For example see above.

private static final int MOV_R_CASE = 1;
private static final int MOV_I_CASE = 2;
private static final int ADD_CASE = 3;
private static final int ADD_R_CASE = 4;
private static final int JMP_CASE = 5;
private static final int JMZ_CASE = 6;
private static final int CMP_CASE = 7;
private static final int SWAP_CASE = 8;
private static final int TOTAL_COMMANDS = 9;
[...]
return switch (pId) {
                case MOV_R_CASE -> AiCommandType.MOV_R;
                case MOV_I_CASE -> AiCommandType.MOV_I;
                case ADD_CASE -> AiCommandType.ADD;
                case ADD_R_CASE -> AiCommandType.ADD_R;
                case JMP_CASE -> AiCommandType.JMP;
                case JMZ_CASE -> AiCommandType.JMZ;
                case CMP_CASE -> AiCommandType.CMP;
                case SWAP_CASE -> AiCommandType.SWAP;
                default -> AiCommandType.STOP;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new-lint A new lint.
Projects
None yet
Development

No branches or pull requests

1 participant