2017-10-12 51 views
-1

目标是将大小写字符串常量与驼峰大小写值组合在一起。 理想情况是:public enum Attribute {Measures, MeasuresLevel}。 但是它不符合命名约定:常量名称应该是大写的。 下面的解决方案看起来像一个重复数据:分组字符串常量与驼峰大小写值

public enum Attribute { 
    MEASURES("Measures"), 
    MEASURES_LEVEL("MeasuresLevel"); 

    private final String value; 

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

任何替代方案,建议都非常欢迎。谢谢。

+2

看看你是什么题? –

+0

为什么不遵循约定? – Valentun

回答

0

将这个到您的枚举

@Overwrite 
public String toString(){ 
    String[] share = this.name().split("_"); 
    String camel = ""; 
    for(String s : share) { 
     camel += s.charAt(0) + s.substring(1).toLowerCase(); 
    } 
    return camel; 
} 
1

许多图书馆提供的实用程序转换为驼峰,例如像Guava

Stream.of(Attribute.values()) 
    .map(attr -> attr.toString()) 
    .map(attr -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, attr)) 
    .forEach(System.out::println); 

已经在番石榴documentation

相关问题