java 枚举值校验_Java枚举类型校验

原代码

package com.merryyou.enmu;

/**

*

* Created on 2016/10/31 0031.

*

* @author zlf

* @since 1.0

*/

public class OriDemo {

public static final String ADD = "add";

public static final String INSERT = "insert";

public static final String UPDATE = "update";

public static final String DEL = "deltete";

public static void main(String[] args) {

// test("add");

test("111");//参数不符合要求的时候也可以执行

}

public static void test(String str){

switch (str){

case ADD:

System.out.println("add");

break;

case INSERT:

System.out.println("insert");

break;

case UPDATE:

System.out.println("update");

break;

case DEL:

System.out.println("del");

break;

default:

System.out.println("参数异常");

}

}

}

使用枚举之后

package com.merryyou.enmu;

import java.util.HashMap;

import java.util.Map;

/**

* Created on 2016/10/31 0031.

*

* @author zlf

* @since 1.0

*/

public enum Command {

ADD("add"),

INSERT("insert"),

UPDATE("update"),

DEL("delete");

private String commandStr;

Command(String str) {

this.commandStr = str;

}

private static final Map stringToCommand = new HashMap();

static {

for(Command item : values()){

stringToCommand.put(item.toString(), item);

}

}

public static Command fromString(String commandStr){

return stringToCommand.get(commandStr);

}

@Override

public String toString() {

return this.commandStr;

}

}

package com.merryyou.enmu;

/**

* Created on 2016/10/31 0031.

*

* @author zlf

* @since 1.0

*/

public class CurDemo {

public static void main(String[] args) {

test(Command.ADD);

}

public static void test(Command command) {

switch (command){

case ADD:

System.out.println("add");

break;

case INSERT:

System.out.println("insert");

break;

case UPDATE:

System.out.println("update");

break;

case DEL:

System.out.println("del");

break;

}

}

}

好的,如果需要允许 null 校验,可以在 valueOf() 方法的基础上进行扩展。具体来说,可以在枚举类型中添加一个 UNKNOWN 枚举,用于表示输入为 null 或无法识别的。示例代码如下: ```java public enum Fruit { APPLE, BANANA, ORANGE, UNKNOWN; public static Fruit fromString(String input) { if (input == null) { return UNKNOWN; } try { return Fruit.valueOf(input.toUpperCase()); } catch (IllegalArgumentException ex) { return UNKNOWN; } } } public class EnumValidationDemo { public static void main(String[] args) { String input1 = "APPLE"; Fruit fruit1 = Fruit.fromString(input1); System.out.println(fruit1); String input2 = null; Fruit fruit2 = Fruit.fromString(input2); System.out.println(fruit2); } } ``` 在上面的代码中,我们在 Fruit 枚举类型中添加了一个 UNKNOWN 枚举,用于表示输入为 null 或无法识别的。然后,我们定义了一个静态方法 fromString(),该方法接受一个字符串参数,并返回对应的 Fruit 枚举。在 fromString() 方法中,我们首先判断输入是否为 null,如果是,则返回 UNKNOWN 枚举。否则,我们尝试使用 valueOf() 方法将输入转换为 Fruit 枚举,如果转换失败,则返回 UNKNOWN 枚举。在主函数中,我们分别使用 input1 和 input2 来测试 fromString() 方法,其中 input1 的为 "APPLE",input2 的为 null。运行程序后,输出结果分别为 APPLE 和 UNKNOWN,符合预期。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值