有很多可能性,但是您可以这样做(我删除了String状态,因为它等于枚举的名称,所以不添加任何值):
public enum RecordStatusEnum { CREATED(Condition.A, Condition.B), INSERTED(Condition.C), FAILED(); private Set<Condition> successConditions; RecordStatusEnum(Condition... successConditions) { this.successConditions = EnumSet.copyOf(Arrays.asList(successConditions)); } public boolean isSuccess(Condition c) { return successConditions.contains(c); }}
编辑:
具有两组条件的示例:
public enum RecordStatusEnum { CREATED(EnumSet.of(Condition.A, Condition.B), EnumSet.of(Condition.C)), INSERTED(EnumSet.of(Condition.C), EnumSet.of(Condition.B), FAILED(EnumSet.noneOf(Condition.class),EnumSet.noneOf(Condition.class)); private Set<Condition> successConditions; private Set<Condition> modificationConditions; RecordStatusEnum(Set<Condition> successConditions, Set<Condition> modificationConditions) { this.successConditions = successConditions; this.modificationConditions = modificationConditions; } public boolean isSuccess(Condition c) { return successConditions.contains(c); } public boolean canBeModified(Condition c) { return modificationConditions.contains(c); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)