java.lang.Boolean非常适合处理三态逻辑,因为它确切地有三个状态:Boolean.TRUE(是这种情况),Boolean.FALSE(不是这种情况)和null(我们不知道情况如何)。使用switch语句来处理这个问题是一个很好的设计,例如在这个构造函数中:
public class URN {
private String value = null;
public URN (String value, Boolean mode){
switch (mode){
case TRUE:
if(!isValidURN(value))
throw new MalformedURLException("The string could not be parsed.");
this.value = value;
break;
case FALSE:
this.value = value.concat(checkByteFor(value));
break;
case null:
if(isValidURN(value))
this.value = value;
else
this.value = value.concat(checkByteFor(value));
break;
}
return;
}
不幸的是,Java不允许这样做,会抱怨“无法切换到布尔类型的值”。实现这个功能会导致混淆的控制流和不友好的代码:
public URN (String value, Boolean mode){
Boolean valid = null;
if (!Boolean.FALSE.equals(mode)){
valid = isValidURN(value);
if (Boolean.TRUE.equals(mode) && !valid)
throw new MalformedURLException("The string could not be parsed.");
if(Boolean.TRUE.equals(valid)) {
this.value = value;
return;
} }
this.value = value.concat(checkByteFor(value));
}
以友好的方式实现需要实现一个枚举类(在实际应用中比这个例子更复杂,因为必须重写.equals()方法,使Trinary.NULL.equals(null)变为true),并进行转换:
private enum Trinary {TRUE, FALSE, NULL};
public URN (String value, Boolean toConvert, String x){
Trinary mode;
if(toConvert == null)
mode = Trinary.NULL;
else
mode = toConvert.equals(Boolean.TRUE) ? Trinary.TRUE : Trinary.FALSE;
switch (mode){
case TRUE:
if(!isValidURN(value)) throw new MalformedURLException("The string could not be parsed.");
this.value = value;
break;
case FALSE:
this.value = value.concat(checkByteFor(value));
break;
case NULL:
if(isValidURN(value))
this.value = value;
else
this.value = value.concat(checkByteFor(value));
break;
}
return;
}
在我看来,这是一种更好、更易读的解决方案,但原始方法代码大小的另一半仅用于转换,这让人感到烦恼,在现实生活中你必须关心具有相同语义的两个不同空值。有没有更好的方法来做到这一点?
NULL
等于null
,并且一开始就不要使用null
。 - Louis Wasserman