速度:三态布尔属性检查
如何根据布尔属性可能具有的所有三种状态来分叉三种不同的情况? 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");
}
Velocity 在这里似乎特别失败,因为规范没有 null 文字,并且为了简单起见,布尔/非空相等检查在某种程度上是可以互换的。当然,有两种解决方案可以避免这种困境(见下文),但是有一些直接/更干净的方法吗?
只需向 Foo 类添加一个额外的 getter,如下所示:
boolean isBoolSet() {return getBool() != null;只需
代码将是:
#if(!$foo.boolSet)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end
获取一些空值,就像这样,
Object getTheNull() {返回 null; }
VTL 将如下所示:
#if($foo.bool == $foo.theNull)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end
How do I fork three different cases depending on all the three states that Boolean property may have? Java code for this looks simple:
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");
}
Velocity seems to epically fail here, as the spec has no null literal and boolean/not-null equality checks are somewhat fungible for the sake of simplicity. Of course, there're two solutions to avoid this dilemma (see below), but is there some straightforward/cleaner way?
just add an extra getter to the Foo class, like so:
boolean isBoolSet() {return getBool() != null; }
and the VTL code would then be:
#if(!$foo.boolSet)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end
fetch some null value, like so,
Object getTheNull() {return null; }
and the VTL then would look like:
#if($foo.bool == $foo.theNull)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用现代版本的 Velocity,则可以仅使用 $null 或 $anyReferenceThatHasNoValue。您还可以使用 #elseif 来简化事情:
但说真的,无论您如何分割它,这都是一个黑客。您应该使用枚举。
If you use a modern version of Velocity, you can just use $null or $anyReferenceThatHasNoValue. You can also use #elseif to simplify things:
But seriously, this is a hack any way you slice it. You should be using an enum.
每当我试图把一些不合适的东西塞进去时,我总是会后悔没有一开始就转移到更合适的状态。
给自己一个枚举,然后你可以明确地说它还没有准备好。明确是好的。在注释上添加几行代码非常棒。
另一个例子是,如果您想创建一个新类或在现有类中放入更多代码,您可能需要 2 或 3 个新类。
就去做吧。
Any time I've tried to shoehorn something where it didn't fit, I've always ended up regretting not just moving to the more appropriate state in the first place.
Give yourself an enum, then you can explicitly say that it's NOT_READY. Explicit is good. A few extra lines of code over comments is awesome.
Another example of this is--if you ever wonder about creating a new class or putting more code in an existing class, you probably need 2 or 3 new classes.
Just go ahead and do it.
相当于
我猜这只是关于深夜编码和有点陈旧/简约的 Velocity 用户指南......
is equivalent to
I guess it's all just about late night coding and somewhat stale/minimalistic Velocity User Guide...
为什么不在构造函数中初始化布尔值?这样它就永远不会为空
Why dont you initialise your Boolean inside the constructor? That way it will never be null