在我们的整个项目中,我们都有这种枚举。它们工作得很好,但我们不确定它们。
特别是使用 getDocumentType(String) 方法。
有没有办法避免对所有 Enums 字段进行迭代?
public enum DocumentType {
UNKNOWN("Unknown"),
ANY("Any"),
ASSET(Asset.class.getSimpleName()),
MEDIA(Media.class.getSimpleName()),
MEDIA35MM(Media.class.getSimpleName() + " 35mm");
private String label;
private DocumentType(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public static DocumentType getDocumentType(String label){
for(DocumentType documentType : DocumentType.values()){
if(documentType.getLabel().equals(label)){
return documentType;
}
}
return UNKNOWN;
}
}
编辑:检查 newacct 响应。她也很好。