Java - enum valueOf "override" naming convention [closed]
Asked Answered
F

3

12

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.

Frisbie answered 3/10, 2013 at 7:43 Comment(9)
en.wikipedia.org/wiki/Method_overriding#Java Overriding method keeps the name.Brunk
Even overloading keeps the same name, but varies the signature. This sounds like we're talking about entirely different names...Reprise
Guys... we are talking about enums. You cannot override valueOf() and values(). I'll change the title to point this out. But please remove your down votes and give me an answer.Frisbie
Let me see I understand: you are asking about how to name a method, let's call it "foo" for now, that will get a String (either "R", "G" or "B") and will return the correct Color.RED/Color.Blue/Color.Green?Absentee
Correct, Elad. A factory method that will return exactly that. I wouldn't mind that "foo" be the convention, but I doubt it.Frisbie
I'd suggest a name like valueOfAlias(String) that evokes the same functionality as valueOf() while indicating a difference. I seriously doubt that there's an existing naming convention for this (although you could create one for yourself), and there certainly isn't a convention that is widely known (which would be the main value for a convention).Geoid
I wonder how Java class RoundingMode manages to override valueOf. For me it seems they solved the problem, did they?Idaliaidalina
I actually like your getColorByName or simply byNameGoosy
I just call my function get() and let Java function overloading handle the rest.Hixson
L
19

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:

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.
Legal answered 6/10, 2013 at 15:17 Comment(4)
The problem is that I was not able to find any examples in the Java specification of an Enum. Color and Class are classes and not enums. Thus you could also add a valueOf method, but you cannot do a switch(color)Frisbie
"fromBlah" makes sense to me. For example, see Response.Status.fromStatusCode.Marlyn
There is no standard and its not very common so you aren't likely to find any strong convention either. Unless you want to start messing around with cglib(!) fromString is probably the best. Certainly no-one is going to be confused when they read it.Nunatak
Why not use colorOf(String colorName)?Recalcitrate
K
2

Do consider the following as well!

Color fromName(String name);
Color fromShortName(String shortName);
Color fromCode(String code);
Kittle answered 9/10, 2013 at 19:37 Comment(0)
N
1

I would use the pair:

Color fromValue(String value)
String toValue()

This is something that I've found most suitable in my enums.

Naphthol answered 8/10, 2013 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.