[英]How to match Nullable String variable with its corresponding Enum value in Java?
我将尝试简化上下文。 我必须改进那种代码。
请考虑这个枚举:
public enum ErrorColor {
YELLOW_VALUE_IS_MISSING("The value for field yellow is missing"),
RED_VALUE_IS_MISSING("The value for field red is missing"),
BLUE_VALUE_IS_MISSING("The value for field blue is missing"),
GREEN_VALUE_IS_MISSING("The value for field green is missing"),
PURPLE_VALUE_IS_MISSING("The value for field purple is missing");
ErrorColor(String message) {this.message = message;}
private String message;
public String getMessage() {return message;}
}
现在它的成员在另一个 class 中使用:
private String defineColorLevel(String color, String yellow,
String red, String blue, String green,
String purple) throws Exception {
switch(color){
case "yellow" : {
if (yellow == null)
throw new Exception(ErrorColor.YELLOW_VALUE_IS_MISSING.getMessage());
return yellow;
}
case "red" : {
if (red == null)
throw new Exception(ErrorColor.RED_VALUE_IS_MISSING.getMessage());
return red;
}
case "blue" : {
if (blue == null)
throw new Exception(ErrorColor.BLUE_VALUE_IS_MISSING.getMessage());
return blue;
}
case "green" : {
if (green == null)
throw new Exception(ErrorColor.GREEN_VALUE_IS_MISSING.getMessage());
return green;
}
case "purple" : {
if (purple== null)
throw new Exception(ErrorColor.PURPLE_VALUE_IS_MISSING.getMessage());
return purple;
}
default: throw new Exception("UnexpectedError");
}
}
实际上,我有超过 15 个带有if
语句的案例,而 Sonar 一点也不高兴......
您能否建议一种避免这种冗余的方法?
我试过类似的方法:
private String throwErrorIfValueIsNullOrReturnIt(String value) {
if (value == null)
throw new Exception(Error.colorIsMissing(value));
return value;
}
使用枚举中的相应方法来构建消息但是...当值为null
时,我无法使用它来匹配相应的缺失输入,也无法构建消息。
显然,我不能使用String
变量的名称。 我可以解决这个问题,还是应该让 Sonar 哭泣?
基本上,您需要结合颜色类型"yellow"
、 "red"
、 "blue"
等及其value
以避免冗余逻辑。
为此,您可以定义 class 或 Java 16 条记录。 我将使用后一个选项 go:
public record ColorValue(String value, ColorType type) {}
ColorType
是您的enum在哪里。 是的,您可以使用相同的枚举来对colors 的类型进行分类并保留错误消息。 因此ColorType
是更具描述性的名称。
public enum ColorType {
YELLOW("The value for field yellow is missing"),
RED("The value for field red is missing"),
BLUE("The value for field blue is missing"),
... etc.
// the rest code without changes
}
负责执行验证的方法可能如下所示:
private String throwErrorIfValueIsNullOrReturnIt(ColorValue color) throws MyException {
if (color.value() == null)
throw new MyException(color.type().getMessage());
return color.value();
}
注意:避免使用Exception
类型,尝试使用适合您的情况的最具体的异常子类型,或者创建自定义异常。
错误消息仅在颜色名称上有所不同。 我会创建一个 function 来动态构建错误消息:
public class ErrorMessageBuilder implements Function<String, String> {
@Override
public String apply(String colorName) {
return String.format("The value for field %s is missing", colorName);
}
}
您可以在适当的地方缓存 function,它是无状态的。 然后将defineColorLevel
简化为只有 2 个参数, color
和value
。
private String defineColorLevel(String color, String value) throws Exception {
ErrorMessageBuilder errorMessageBuilder = new ErrorMessageBuilder();
//validate color somehow, if needed at all
if (value == null) {
throw new Exception(errorMessageBuilder.apply(color));
}
return value;
}
此外,为了防止无效的 colors,您可以定义一个具有有效enum
的枚举,或者一个Set
,或者其他取决于具体用例的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.