使用Java中的ENUMS验证值组合的最佳方法是什么? [英] What is the best approach for validating a combination of values using ENUMS in Java?

查看:283
本文介绍了使用Java中的ENUMS验证值组合的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过定义如下的ENUM来验证从数据库检索的记录的状态

I am validating the status of a record retrieved from the DB by defining an ENUM as below

public enum RecordStatusEnum {
    CREATED("CREATED"),
    INSERTED("INSERTED"),   
    FAILED("FAILED");

private String recordStatusValue;

    RecordStatusEnum (String status) {
        recordStatusValue= status;
    }

    public boolean isSuccess() {
        return (this.equals(CREATED) || this.equals(INSERTED));
    }

}

方法isSuccess()正在用于检查检索到的记录的状态(从员工的列状态)

The method isSuccess() is being used to check the status of the retrieved record ( column status from employee)

if (!(employee.getStatus().isSuccess())) {
            // return error
        }

根据新的要求,有一套条件介绍说A,B和C;对于他们来说,在Employee表'条件'中有一列。

As per the new requirement, there are a set of conditions introduced say A,B and C; and for them there is a column in the Employee table 'condition'.

所以我需要检索状态和条件,看看它是否属于具有两者组合的集合。

So I need to retrieve the status as well as the condition and see if it belongs to a set which has the combination of both.

例如:isSuccess()应该检查以下内容:

For eg : isSuccess() should check if in the following:

CREATED and A 
CREATED and B
INSERTED and C

必须实现这一点,我添加一个新的组合,轻松地将INSERTED和B列入列表。

This must be achieved such that it is easy for me to add a new combination say 'INSERTED and B' into the list easily.

上述问题的最佳方法是什么?

What is the best approach for the above problem?

注意:在实际的业务场景中,有许多不同的组合有更多的状态和检查(例如isFailed()canBeModified()等)

Note : in the actual business scenario there are a whole lot more statuses and checks (eg isFailed() canBeModified() etc) with many different combinations

即使不使用ENUMS,也可以建议任何方法。我提到了ENUMS,因为我不想偏离现有的实现。

And any method can be suggested even if it doesn't use ENUMS. I mentioned ENUMS, because I dont want to deviate much from the existing implementation

推荐答案

许多可能性,但你可以这样做(我删除了String状态,它不会添加任何值,因为它等于枚举的名称):

There are many possibilities, but you could do like this (I removed the String status, which doesn't add any value since it's equal to the name of the enum):

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);
    }
}

这篇关于使用Java中的ENUMS验证值组合的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆