根据布尔属性可能具有的三种状态,我如何分叉三种不同情况?这个Java代码看起来简单:速度:三态布尔属性检查
public class Foo {
public Boolean getBool() { return null /* this would be dynamic in RL */; }
}
// somewhere in the servlet code:
if (foo.getBool() == null) {
resp.getWriter().print("not yet set");
} else if (foo.getBool()) {
resp.getWriter().print("set to TRUE");
} else {
resp.getWriter().print("set to FALSE");
}
速度似乎epically失败在这里,作为规范,没有空文字和布尔/非空平等支票只用于简单起见有些互换。当然,有两种解决方案可以避免这种困境(见下文),但是有一些简单/更清晰的方法吗?
只需添加一个额外的消气剂Foo类,像这样:
布尔isBoolSet(){返回getBool()= NULL;! }
和VTL代码将随后被:
#if(!$foo.boolSet)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end
取一些空值,象这样,
对象getTheNull(){返回NULL; }
,然后VTL会是什么样子:
#if($foo.bool == $foo.theNull)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end
嗯,我认为这将是少的速度效率,但最终这将是更安全和更清洁的使用enum类型... – 2010-12-09 22:11:04
会以何种方式是效率较低?几乎肯定会更有效率。 – 2010-12-09 22:15:21
好吧,我知道在像Android这样的平台中不推荐使用枚举类型,因为它比直接数值选项慢。我不确定在普通的Java环境中两者之间的速度差异,但可能它们是相似的,即使差异最终不是那么剧烈。 – 2010-12-10 01:08:15