zolakt
zolakt

Reputation: 531

Java enum valueOf() with multiple values?

I have a problem in Java using Enums. I have read the documentation about assigning value parameters to Enums. But, my question is what about multiple values, is it possible?

This what I would like to achieve: I have an Enum for languages. Each language is represented by its name and some shorter aliases (not always, and not always the same number of aliases)

Here is an example:

public enum Language{
English("english", "eng", "en", "en_GB", "en_US"),
German("german", "de", "ge"),
Croatian("croatian", "hr", "cro"),
Russian("russian")
}

Can I just define an Enum like this and get the right enum values by calling Language.valueOf() ???

Upvotes: 52

Views: 94379

Answers (6)

YoYo
YoYo

Reputation: 9405

Mapping a string to a enum value, is typically what the valueOf static method is doing for you. So if you want to accomplish this with the use of synonyms, you will have to develop something similar. Because we cannot override a static method, and I do not think we want to in this case, we will name it differently: fromString should be appropriate.

public enum Language { 
  ENGLISH("eng", "en", "en_GB", "en_US"),   
  GERMAN("de", "ge"),   
  CROATIAN("hr", "cro"),   
  RUSSIAN("ru"),
  BELGIAN("be",";-)");

  static final private Map<String,Language> ALIAS_MAP = new HashMap<String,Language>(); 
  static { 
    for (Language language:Language.values()) { 
      // ignoring the case by normalizing to uppercase
      ALIAS_MAP.put(language.name().toUpperCase(),language); 
      for (String alias:language.aliases)
        ALIAS_MAP.put(alias.toUpperCase(),language);
    } 
  } 

  static public boolean has(String value) { 
    // ignoring the case by normalizing to uppercase
    return ALIAS_MAP.containsKey(value.toUpperCase());
  } 

  static public Language fromString(String value) { 
    if (value == null) throw new NullPointerException("alias null"); 
    Language language = ALIAS_MAP.get(value.toUpperCase()); 
    if (language == null)
      throw new IllegalArgumentException("Not an alias: "+value); 
    return language; 
  } 

  private List<String> aliases; 
  private Language(String... aliases) { 
    this.aliases = Arrays.asList(aliases); 
  } 
} 

As a benefit of this type of implementation we can, as demonstrated, also easily implement the has static method to test if a given alias is part of the enum value set. At the same time, we applied some good naming conventions:

  • the enum values go in uppercase, to indicate that they are in actuality static finals (singleton instances).
  • at the same time, we also put all the other static finals all caps.

Note that we do not have to repeat the name of the enum value itself: we always consider it's own name automatically (gets added to the ALIAS_MAP), and on top we normalize everything to uppercase to make it case insensitive.

Seems big, but while using the enum, it looks pretty:

public void main() {
  Language myLanguage = Language.fromString("en_GB");
  if (myLanguage == Language.ENGLISH) {
    System.out.println("Yes, I know, you understand English!");
  }
} 

The backing container for the aliases is a Map, a HashMap to be more specific. The HashMap provides a fast access path to the aliases, and is also easy to grow. Whenever we think about 'indexing' something, likely a HashMap should be our first choice.

Note that for transparent use, we store both the name of the enum constant itself (retrieved through the name() method), and all the aliases. We could have implemented this differently by first attempting to do the lookup using the built-in valueOf static method. It is a design choice, but we would potentially have to deal with additional exceptions etc.

Upvotes: 18

CsBalazsHungary
CsBalazsHungary

Reputation: 813

Alternatively add a method, so your enums and constructor can be more clean if the structure is more complicated:

public Language getLanguage(String code){
  switch(code){
    case "en":
    case "en_GB":
    ...
    case "eng":
      return ENGLISH;
    case "rus":
    case "russian":
      return RUSSIAN;
  }
}

Upvotes: 0

Flavio
Flavio

Reputation: 11977

This is probably similar to what you're trying to achieve.

public enum Language{
    English("english", "eng", "en", "en_GB", "en_US"),
    German("german", "de", "ge"),
    Croatian("croatian", "hr", "cro"),
    Russian("russian");

    private final List<String> values;

    Language(String ...values) {
        this.values = Arrays.asList(values);
    }

    public List<String> getValues() {
        return values;
    }
}

Remember enums are a class like the others; English("english", "eng", "en", "en_GB", "en_US") is calling the enum constructor.

You could then retrieve the enum value corresponding to a string through a search method (you can put it as a static method in the enum again).

public static Language find(String name) {
    for (Language lang : Language.values()) {
        if (lang.getValues().contains(name)) {
            return lang;
        }
    }
    return null;
}

Upvotes: 72

user207421
user207421

Reputation: 310884

Normalize the data:

public enum LanguageCode
{
  ENGLISH,
  GERMAN,
  CROATIAN,
  RUSSIAN,
  // ...
}
// (add whatever initialization you want to that

Then

public enum Language{
  english(ENGLISH),
  eng(ENGLISH),
  en(ENGLISH),
  en_GB(ENGLISH),
  // ...
}

etc.

Upvotes: 2

zolakt
zolakt

Reputation: 531

Wow, really quick reply :) Thanks guys.

Andy is right. I want to call Language.valueOf("eng") or Language.valueOf("english") and get Language.English as return.

I already have a utility function that does this, but it's not very nice. Switch function that checks string values and return appropriate enum instance.

But there is a lot of code rewriting (I have cca 30-40 languages). And if I want to add a language, I have to add it to enum, and implement a new check in the utility class.

I'll try Flavios approach. Just one question. Your constructor, shouldn't it be?

Language(List<String> values) {
    this.values = Arrays.asList(values);
}

Upvotes: 1

Andy
Andy

Reputation: 8949

In short, no.

The parameter for the valueOf() method must be only the String of the enum constant type. So it cannot vary, or lookup possible values. See the JavaDoc.

You need to write your own utility method to return the proper enum type for the given values.

Upvotes: 3

Related Questions