简体   繁体   English

Java检查字段的特定值

[英]java check for particular value for a field

I have requirement to check for a particular field only particular values are allowed, how to do that best in java. 我要求检查仅允许特定值的特定字段,如何在Java中做到最好。

For example: 例如:

For a "Status" field only allowed values are "eq" and "in" For a "Date" field only allowed values are "gt","ge" and "eq" 对于“状态”字段,仅允许值为“ eq”和“中”对于“日期”字段,仅允许值为“ gt”,“ ge”和“ eq”

My method will look something like this, 我的方法看起来像这样,

boolean checkFieldAndOperatorMatching(String field, String operator);

for example, if some one passes 例如,如果有人通过

 checkFieldAndOperatorMatching("status", "eq"); //correct
 checkFieldAndOperatorMatching("status", "in"); //correct

 checkFieldAndOperatorMatching("status", "gt"); // not correct

 checkFieldAndOperatorMatching("date", "gt"); //correct
 checkFieldAndOperatorMatching("date", "ge"); //correct
 checkFieldAndOperatorMatching("date", "eq"); //correct

 checkFieldAndOperatorMatching("date", "in"); // not correct

Using Properties, you can do similar(change to suit your requirements) to below: 使用属性,您可以执行以下类似操作(更改以适合您的要求):

    public boolean checkFieldAndOperatorMatching(String field, String operator) {

    // can be loaded once
    // make it static or use a factory method
    // which returns same Properties instance every time if already loaded
    Properties properties = new Properties();

    try (FileInputStream fin = new FileInputStream("D:\\fields.properties");) {

        properties.load(fin);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String validValues = properties.getProperty(field);

    return Arrays.asList(validValues.split(",")).contains(operator);
}

where, fields.properties contains: 其中, fields.properties包含:

date=gt,ge,eq
status=eq,in

You can create Enums for Date and Status , which contains the permitted values:- 您可以为DateStatus创建Enums ,其中包含允许的值:-

enum Status {
    eq,in
}
enum Date {
    gt,ge,eq
}

And then iterate through enum to check if the values passed in parameter exist in enums:- 然后遍历枚举,以检查传递给参数的值是否存在于枚举中:

static boolean checkFieldAndOperatorMatching(String field, String operator){

    if(field.equals("Status")){ //if field is Status

        for (Status s : Status.values()) {

             if(operator.equals(s.toString())){

                 return true;
             }

             }
             return false;
        }

     if(field.equals("Date")){ //if field is Date

         for (Date d : Date.values()) {
             if(operator.equals(d.toString())){

                 return true;
             }

         }
             return false;
      }

 }

if you have many fields for validation, you can do the following 如果您有许多要验证的字段,则可以执行以下操作

  • Use DB to store the key-values and caches those in Collections to validate (IF you have grow-able number of fields) 使用数据库来存储键值并缓存集合中的键值以进行验证(如果您的字段数量可以增加)

  • Use Properties file to represent $field = $values ( comma separated) and read them in your validate method (If you have mostly static list of fields with constant values 使用属性文件表示$ field = $ values(用逗号分隔),然后在您的validate方法中读取它们(如果您的静态列表中的字段大部分是常量,则为常量)

  • Use enums if its completely static values. 如果枚举是完全静态的,请使用它。 I agree that it will make you write extra lines of code, but is one of the good practice to have constant field values in enums. 我同意这会使您编写额外的代码行,但这是在枚举中具有恒定字段值的一种良好实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM