1

I have an enum in Java 8 with Lombok's @Getter and @AllArgsConstructor for using additional properties for the enum value:

@Getter
@AllArgsConstructor
public enum MyEnum {
    RED(1),
    GREEN(2),
    BLUE(3),
    PURPLE(4);
    
    private final int ordinal;
    
    public String getDisplayName() {
        switch (ordinal) {
            case 1:
                return "1st color";
            case 2:
                return "2nd color";
            case 3:
                return "3rd color";
            default:
                return "another color";
        }
    }
}

What I don't like about this solution: getDisplayName() is called quite often, thus every call runs the switch-case statement.

Is it possible to add another property like displayName which values are set by a function analogous to getDisplayName()?

Something like this (pseudo-code):

@Getter
@AllArgsConstructor
public enum MyEnum {
    RED(1, setDisplayName()),
    GREEN(2, setDisplayName()),
    BLUE(3, setDisplayName()),
    PURPLE(4, setDisplayName());
    
    private final int ordinal;
    private String displayName;
    
    private void setDisplayName() {
        switch (ordinal) {
            case 1:
                displayName = "1st color";
            case 2:
                displayName = "2nd color";
            case 3:
                displayName = "3rd color";
            default:
                displayName = "another color";
        }
    }
}
6
  • 7
    "which values are set by a function analogous to getDisplayName()" why not just have a second instance variable called displayName and add that string to the constructor? So that you can do something like public enum MyEnum { RED(1, "1st color"), GREEN(2, "2nd Color").... Commented Mar 17, 2021 at 21:02
  • @FedericoklezCulloca Yes, that would be a way I could go for a relatively small amount of enum values. If the enum grows in size, I'd like to avoid this. Commented Mar 17, 2021 at 21:05
  • 1
    Ah, now I think I see the problem. You're worried about the values you don't have in the switch statements and how they would go to default? Commented Mar 17, 2021 at 21:08
  • 2
    In that case I would go with a constant for the default values. I mean, if your alternative was to add a call to a method for each of the enum values I don't see much difference :) either that or you could remove @AllArgsConstructor and provide your own one-argument constructor that does the switch part and assigns the correct value to displayName Commented Mar 17, 2021 at 21:12
  • 3
    @RobertStrauch, not to be picky, but ordinal is a terrible choice for that constant field. It leads to confusion because Enums have already a field name ordinal. Commented Mar 17, 2021 at 21:19

3 Answers 3

2

If you're not married to lombok I would just provide my own constructor and let it do the heavy lifting

@Getter
public enum MyEnum {
    RED(1),
    GREEN(2),
    BLUE(3),
    PURPLE(4);
    
    private final int o;
    private final String display;
    
    private MyEnum (int o) {
        this.o = o;
        switch (o) {
            case 1:
                display = "1st color";
                break;
            case 2:
                display = "2nd color";
                break;
            case 3:
                display = "3rd color";
                break;
            default:
                display = "another color";
        }
    }
}

Also changed ordinal to o as per @hfontanez suggestion.

Sign up to request clarification or add additional context in comments.

Comments

1

You could do something like this:

@Getter
@AllArgsConstructor
public enum MyEnum {
    RED(1, setDisplayName(1)),
    GREEN(2, setDisplayName(2)),
    BLUE(3, setDisplayName(3)),
    PURPLE(4, setDisplayName(4));

    private final int ordinal;
    private String displayName;

    private static String setDisplayName(int ordinal) {
        switch (ordinal) {
            case 1:
                return "1st color";
            case 2:
                return "2nd color";
            case 3:
                return "3rd color";
            default:
                return "another color";
        }
    }
}

The setDisplayName method may even be in another class.

Comments

1

you can just call the method in the constructor

@Getter
public enum MyEnum {
    RED(1),
    GREEN(2),
    BLUE(3),
    PURPLE(4);
    
    private final int ordinal;
    private final int displayName;

    MyEnum(int ordinal) {
      this.ordinal = ordinal;
      this.displayName = getDisplayName(ordinal);
    }

    
    public static String getDisplayName(int ordinal) {
        switch (ordinal) {
            case 1:
                return "1st color";
            case 2:
                return "2nd color";
            case 3:
                return "3rd color";
            default:
                return "another color";
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.