11

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.

Alin Stoian
  • 1,122
  • 1
  • 12
  • 24
  • 5
    http://en.wikipedia.org/wiki/Method_overriding#Java Overriding method keeps the name. – Eel Lee Oct 03 '13 at 07:45
  • Even overloading keeps the same name, but varies the signature. This sounds like we're talking about entirely different names... – Jon Skeet Oct 03 '13 at 07:46
  • 5
    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. – Alin Stoian Oct 03 '13 at 07:52
  • 2
    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? – Elad Tabak Oct 06 '13 at 10:26
  • Correct, Elad. A factory method that will return exactly that. I wouldn't mind that "foo" be the convention, but I doubt it. – Alin Stoian Oct 06 '13 at 16:50
  • 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). – Ted Hopp Oct 06 '13 at 17:07
  • I wonder how Java class [RoundingMode](http://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html) manages to override valueOf. For me it seems they solved the problem, did they? – NoDataDumpNoContribution Oct 08 '13 at 13:20
  • I actually like your `getColorByName` or simply `byName` – skirsch Oct 08 '13 at 19:28

3 Answers3

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.
Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
  • 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) – Alin Stoian Oct 06 '13 at 16:51
  • "fromBlah" makes sense to me. For example, see `Response.Status.fromStatusCode`. – jtoberon Oct 08 '13 at 13:11
  • 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. – matt helliwell Oct 08 '13 at 15:31
  • Why not use `colorOf(String colorName)`? – Jan Mar 19 '15 at 10:56
2

Do consider the following as well!

Color fromName(String name);
Color fromShortName(String shortName);
Color fromCode(String code);
Ravindra HV
  • 2,558
  • 1
  • 17
  • 26
1

I would use the pair:

Color fromValue(String value)
String toValue()

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

alterfox
  • 1,675
  • 3
  • 22
  • 37