简体   繁体   中英

How do I define a Java enum programmatically?

I need to define an Enum type in Java. However, the values in the enum depends on a parameter.

public class EnumTest {

    private Language lang;

    public enum Language {English, Spanish, Chinese, German, Japanese;}

    public enum Entity {

        // ??? 
        entityA("student"),
        entityB("faculty"),
        entityC("staff");

        private String name;

        Entity(String name) {
            this.name = name;
        }
    }

    public EnumTest(Language lang) {
        this.lang = lang;
    }

    public static void main(String[] args) {
        EnumTest testA = new EnumTest(Langauge.English);
        EnumTest testB = new EnumTest(Language.Spanish);
    }
}

For example, the instantiation of entityA, entityB, entityC will not be "student", "faculty", and "staff", if the parameter is not Langauge.English. It will be the corresponding words translated from English in other languages. So, the definition of the enum Entity depends on the parameter Lang.

How can I achieve?

You could store the different translations within a field inside the enum.

public enum Entity {

    EntityA("student", "translation1", "translation2", "translation3", "translation4"),
    EntityB("faculty", "translation1", "translation2", "translation3", "translation4"),
    EntityC("staff", "translation1", "translation2", "translation3", "translation4");

    public String[] names;

    private Entity(String english, String spanish, String chinese, String german, String japanese) {
        names = new String[]{english, spanish, chinese, german, japanese};
    }
}

You could then get the appropriate translation with a method like the following:

public String getTranslation(Entity entity, Language lang){
    return entity.names[lang.ordinal()]; // ordinal returns the id of the enum instance. The first declared has 0, the second has 1 and so on
}

So, the below code

getTranslation(Entity.EntityA, Language.Spanish)

would return "translation1".

The major advantage of enum types in Java is the ability for the compiler to know and have access to all instances at compile time . To define or modify enum values at runtime, even in tests, is to undermine Java enums and the guarantees and optimizations they support (eg serialization, VM-wide singleton properties, and well-defined ordinal and values[] ).

A few alternatives you might consider:

  • Keep Entity as a enum, and shortly after runtime load a long-lived Map<Entity, String> that keeps the localized name.

  • Limit access to the "Entity" constructor (to make the class instance-controlled) and then provide instances through a method or series of methods.

  • Have all localized strings on the enum take some sort of table instance (eg Properties or Map) that it uses to extract the right name based on an externally-kept key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM