速度:三态布尔属性检查

发布于 2024-10-06 13:59:40 字数 1069 浏览 9 评论 0原文

如何根据布尔属性可能具有的所有三种状态来分叉三种不同的情况? 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 文字,并且为了简单起见,布尔/非空相等检查在某种程度上是可以互换的。当然,有两种解决方案可以避免这种困境(见下文),但是有一些直接/更干净的方法吗?

  1. 只需向 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
  1. 获取一些空值,就像这样,

    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?

  1. 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
  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的味道 2024-10-13 13:59:40

如果您使用现代版本的 Velocity,则可以仅使用 $null 或 $anyReferenceThatHasNoValue。您还可以使用 #elseif 来简化事情:

#if($foo.bool == $null)
  not yet set  
#elseif($foo.bool)
    set to TRUE
#else
    set to FALSE
#end

但说真的,无论您如何分割它,这都是一个黑客。您应该使用枚举。

If you use a modern version of Velocity, you can just use $null or $anyReferenceThatHasNoValue. You can also use #elseif to simplify things:

#if($foo.bool == $null)
  not yet set  
#elseif($foo.bool)
    set to TRUE
#else
    set to FALSE
#end

But seriously, this is a hack any way you slice it. You should be using an enum.

巷子口的你 2024-10-13 13:59:40

每当我试图把一些不合适的东西塞进去时,我总是会后悔没有一开始就转移到更合适的状态。

给自己一个枚举,然后你可以明确地说它还没有准备好。明确是好的。在注释上添加几行代码非常棒。

另一个例子是,如果您想创建一个新类或在现有类中放入更多代码,您可能需要 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.

猫七 2024-10-13 13:59:40
!$foo.bool && $foo.bool != false

相当于

$foo.bool == $null (in Velocity 1.6 onwards)

我猜这只是关于深夜编码和有点陈旧/简约的 Velocity 用户指南......

!$foo.bool && $foo.bool != false

is equivalent to

$foo.bool == $null (in Velocity 1.6 onwards)

I guess it's all just about late night coding and somewhat stale/minimalistic Velocity User Guide...

缘字诀 2024-10-13 13:59:40

为什么不在构造函数中初始化布尔值?这样它就永远不会为空

Why dont you initialise your Boolean inside the constructor? That way it will never be null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文