Groovy web console

subscribe to the feed Subscribe
to this
site

Smart Enums

Published 3 months ago by VE
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
println ProgrammingLanguages.getProgrammingLanguage('java') 
println ProgrammingLanguages.getProgrammingLanguage('dotnet')
println ProgrammingLanguages.getProgrammingLanguage('non esisto') 

println PipelineVersionsEnum.getVersion('1.0') 
println PipelineVersionsEnum.getVersion('2.0')
println PipelineVersionsEnum.getVersion('3.0') 

public enum ProgrammingLanguages{
    JAVA('java'),
    DOTNET('dotnet')
 
    final String desc;
    static final Map map 
 
    static {
        map = [:] as TreeMap
        values().each{ programmingLanguage -> 
            map.put(programmingLanguage.desc, programmingLanguage )
        }
    }
 
    private ProgrammingLanguages(String desc) {
        this.desc = desc;
    }
 
    static getProgrammingLanguage(desc) {
        map[desc]
    }
}

public enum PipelineVersionsEnum{
    v1_0('1.0'),
    v2_0('2.0')
 
    final String desc;
    static final Map map 
 
    static {
        map = [:] as TreeMap
        values().each{ version -> 
            map.put(version .desc, version)
        }
    }
 
    private PipelineVersionsEnum(String desc) {
        this.desc = desc;
    }
 
    static getVersion(desc) {
        map[desc]
    }
}