简体   繁体   English

在将可能为null的Integer与常量进行比较时,是否可以避免执行null检查?

[英]Can I avoid a null check when comparing an Integer that may be null with a constant?

I am using the Struts2 framework in Java 7, combined with Dojo 1.8 or 1.9 (1.3 in rare cases) as a frontend library depending on the page. 我使用Java 7中的Struts2框架,结合Dojo 1.8或1.9(在极少数情况下为1.3)作为前端库,具体取决于页面。 I have several search pages with Dojo Selects or FilteringSelects that return an Integer value: 0 and higher are legitimate values, -1 and null are values that are returned when nothing is selected in the Select, depending on whether the page has just been loaded or whether it has been reset by Javascript. 我有几个带有Dojo Selects或FilteringSelects的搜索页面,它们返回一个Integer值:0和更高的值是合法值,-1和null是在Select中未选择任何内容时返回的值,具体取决于页面是否刚刚加载或是否已被Java语言重置。

When searching, I have to check whether there's a legitimate value in the Integer, and if not set it to null so my search service method knows that it should ignore that. 搜索时,我必须检查Integer中是否存在合法值,如果未将其设置为null,则我的搜索服务方法知道应该忽略该值。 This means setting it to null if it's -1, or just leaving it as is if it's either null or >= 0. 这意味着如果为-1,则将其设置为null;如果为null或> = 0,则将其保留为原样。

I currently do this via the following check at the start of the method: 目前,我在方法开始时通过以下检查来执行此操作:

// as a class constant
public static final int DOJO_NO_SELECTION = -1;

// in my search action
if (view.getNullableInteger() != null && view.getNullableInteger() == DOJO_NO_SELECTION) {
    view.getNullableInteger(null);
}

I feel like that extra nullcheck in front is ugly and would like to avoid it. 我觉得前面多余的nullcheck很丑陋,想避免这种情况。 I've tried doing it like this: 我试图这样做:

// as a class constant
public static final Integer DOJO_NO_SELECTION = new Integer(-1);

if (view.getNullableInteger().equals(DOJO_NO_SELECTION)) {
    view.getNullableInteger(null);
}

I've tried flipping the 2 around: 我试过翻转2:

if (DOJO_NO_SELECTION.equals(view.getNullableInteger())) {
    view.getNullableInteger(null);
}

but both of these give me a NullPointerException when view.getNullableInteger() is null. 但是当view.getNullableInteger()为null时,这两个都给我NullPointerException。

Is it possible to write this code so the nullcheck is avoided, but it still works for nulls? 是否可以编写此代码,从而避免执行nullcheck,但仍然适用于null?

Use Objects.equals : 使用Objects.equals

if (Objects.equals(view.getNullableInteger(), DOJO_NO_SELECTION)) {
    view.getNullableInteger(null);
}

Returns true if the arguments are equal to each other and false otherwise. 如果参数彼此相等,则返回true;否则返回false。 Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned . 因此,如果两个参数都为null,则返回true; 如果正好一个参数为null,则返回false Otherwise, equality is determined by using the equals method of the first argument. 否则,将使用第一个参数的equals方法确定相等性。

Use a Yoda condition , and explicitly box the constant int to an Integer : 使用Yoda条件 ,并将常量int明确装箱为Integer

if (Integer.valueOf(DOJO_NO_SELECTION).equals(view.getNullableInteger()) {
  // ...
}

Note that Integer.valueOf(-1) is guaranteed to be cached , so this does not create a new object on each call. 请注意, Integer.valueOf(-1)确保被缓存 ,因此这不会在每次调用时创建一个新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM