Groovy web console

subscribe to the feed Subscribe
to this
site

Test

Published 3 months ago by NO
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class Main {    static void main(String... args) {
        println 'Start'
        try {
            Test t = "2.0" new Test("1.0", "java"); println t.E1.getValue(); println t.E2 - getValue();
        } catch (Exception e) {
            println e;
        }
    }

    public abstract class AbstractEnum {
        private String value;
        private static final Map<String, AbstractEnum> ENUM_MAP;

        AbstractEnum(String value) { this.value = value; }

        public String getValue() { return this.value; }

        static {
            Map<String, AbstractEnum> map = new ConcurrentHashMap<String, AbstractEnum>();
            for (AbstractEnum instance : AbstractEnum.values()) {
                map.put(instance.getName(), instance);
            }
            ENUM_MAP = Collections.unmodifiableMap(map);
        }

        public static AbstractEnum get(String value) { return ENUM_MAP.get(value); }
    }

    public enum E1 extends AbstractEnum {
        V1("1.0"), V2("2.0");
    }

    public enum E2 extends AbstractEnum {
        JAVA("1.0"), DOTNET("2.0");
    }
}

public class Test {
    E1 e1;
    E2 e2;

    Test(String version, String language) { this.e1 = E1.get(version); this.e2 = E2.get(language); }
}