Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - enum valueOf "override" naming convention

Say you have the following enum:

public enum Color {
    RED("R"), GREEN("G"), BLUE("B");
    private String shortName;

    private Color(String shortName) {
        this.shortName = shortName;
    }

    public static Color getColorByName(String shortName) {
        for (Color color : Color.values()) {
            if (color.shortName.equals(shortName)) {
                return color;
            }
        }
        throw new IllegalArgumentException("Illegal color name: " + shortName);
    }
}

Since enum is a special case, when you cannot just override the valueOf function, what is the naming convention for circumventing this and implementing valueOf(String name)?

getColorByName(String name)
getValueOf(String name)
permissiveValueOf(String name)
customValueOf(String name)
forName(String name)
getEnum(String name)
getColor(String name)

Later Edit: I see that Bloch in Effective Java 2nd ed. proposes something in the lines of getInstance() (Chapter 1, Item 1). Just to add another option.

like image 719
Alin Stoian Avatar asked Oct 03 '13 07:10

Alin Stoian


People also ask

Can I override valueOf in enum Java?

Since you cannot override valueOf method you have to define a custom method ( getEnum in the sample code below) which returns the value that you need and change your client to use this method instead.

How does enum valueOf work in Java?

valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Should enum be uppercase or lowercase?

Because they are constants, the names of an enum type's fields are in uppercase letters. You should use enum types any time you need to represent a fixed set of constants.

Is Java enum valueOf case sensitive?

valueOf method is case-sensitive and invalid String will result in IllegalArgumentException. In short, The string passed to the valueOf method of Java enum must be the same as the String returned by name() method like TrafficSigal.RED.name() returns RED and you should pass RED to valueOf() to get TrafficSignal.


2 Answers

You are definitely right, you cannot override Enum#valueOf() since it is a static method of Enum class.

I don't think there is a naming convention. As you have already pointed out, there are few examples in Java:

  • Color#getColor()
  • Class#forName()

I won't use getEnum, since you are not getting the Enum itself, but rather a value. Using forName() is not appropriate here, R is not the name of the red color.

I would rather go with:

  • fromString() since it is an opposite to toString();
  • getColor() for consistency with Java Standard Library.
like image 170
Guillaume Poussel Avatar answered Oct 04 '22 18:10

Guillaume Poussel


Do consider the following as well!

Color fromName(String name);
Color fromShortName(String shortName);
Color fromCode(String code);
like image 32
Ravindra HV Avatar answered Oct 04 '22 19:10

Ravindra HV