Groovy web console

subscribe to the feed Subscribe
to this
site

Beer are you ready?

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

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

public trait MapAbility {
    private static Map map;
    private String desc;
    
    static{
        initMap(values());
    }

    public static initMap(values) { 
        map = [:] as TreeMap
        values.each{ item -> 
            map.put(item.MapAbility__desc, item)
        }
    }  
    
    static getValue(desc) {
        map[desc]
    }
}

public enum ProgrammingLanguages implements MapAbility {
    JAVA('java'),
    DOTNET('dotnet')

    private ProgrammingLanguages(String desc) {
        this.MapAbility__desc = desc;
    }
}

public enum PipelineVersionsEnum implements MapAbility{
    v1_0('1.0'),
    v2_0('2.0')

    private PipelineVersionsEnum(String desc) {
        this.MapAbility__desc = desc;
    }
}