简体   繁体   English

什么是实现compareObjects()的正确方法

[英]What is the correct way to implement compareObjects()

I have compareObjects method implemented as below 我有实现以下的compareObjects方法

public static int compareObjects(Comparable a, Comparable b){

        if (a == null && b == null){
            return 0;
        } else if (a == null && b != null){
            return -1;
        } else if (a != null && b == null){
            return 1;
        } else {
            return a.compareTo(b);
        }
    }

When I run this through findBugs, I get this suggestion on the line return a.compareTo(b) : 当我通过findBugs运行它时,我在return a.compareTo(b)的行上得到了这个建议:

There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. 有一条语句的分支,如果执行该分支,则保证将取消引用空值,这将在执行代码时生成NullPointerException。 Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception can't ever be executed; 当然,问题可能出在分支或语句不可行,并且永远不会执行空指针异常。 deciding that is beyond the ability of FindBugs. 认为这超出了FindBugs的能力。 Due to the fact that this value had been previously tested for nullness, this is a definite possibility. 由于此值之前已经过测试,因此这是绝对的可能性。

At this point a can never be null. 此时, a永远不能为空。 Why does FindBugs show me this suggestion? 为什么FindBugs向我显示此建议? How can I correct this; 我该如何纠正? what is the correct way to implement compareObjects() ? 什么是实现compareObjects()的正确方法?

It may be a limitation in FindBugs; 它可能是FindBugs中的一个限制; I agree that you've covered all the bases, but your null-check is split across two different conditions. 我同意您已经涵盖了所有基础,但是您的空值检查分为两个不同的条件。 Now these conditions happen to be complementary, so at least one of them will fire if a is null, but depending how sophisticated FindBugs is, it may not recognise this. 现在这些条件恰好是互补的,因此如果a为null,则至少其中一个会触发,但是根据FindBugs的复杂程度,它可能无法识别。

Two options here, then: 然后有两个选择:

  1. Just ignore the FindBugs warning. 只需忽略FindBugs警告。 Due to its nature it will raise some false positives from time to time, so don't feel like you have to rewrite your code to make it 100% happy if you don't think the rewrite is worthwhile on its own merits. 由于它的性质,它不时地带来一些误报,因此,如果您认为重写本身不值得,就不必像以前那样重写代码以使其100%满意。

    You can use the @SuppressWarnings annotation to actually communicate this to FindBugs, if you want the report to show a nice big zero at the end. 如果您希望报告最后显示一个不错的大零,则可以使用@SuppressWarnings批注将其实际传达给FindBugs。 See this question for an example. 有关示例,请参见此问题

  2. Restructure the condition so that the nullity check on a is more explicit, by nesting if blocks: 通过嵌套if块,重新构造条件,以使对a的无效检查更加明确:

     if (a == null) { return b == null ? 0 : -1; } return b == null ? 1 : a.compareTo(b); 

    Depending on your tastes and style that might be a better rewrite anyway, in that is more clearly says "if a is null, do this calculation and return it, otherwise do this calculation". 根据您的喜好和风格,无论如何都可能会更好地重写,因为更清楚地说:“如果a为null,请执行计算并返回它,否则请进行计算”。 You can of course change the ternary condition into another if-else block if you prefer that. 如果愿意,您当然可以将三元条件更改为另一个if-else块。

I think it might be because you don't need the extra && statements. 我认为这可能是因为您不需要多余的&&语句。 After the first if statement you already know that one of them is null. 在第一个if语句之后,您已经知道其中一个为null。

public static int compareObjects(Comparable a, Comparable b){

    if (a == null && b == null){
        return 0;
    } else if (a == null){
        return -1;
    } else if (b == null){
        return 1;
    } else {
        return a.compareTo(b);
    }
}

Looking at it again , try this code: 再次查看,请尝试以下代码:

if (a == null && b == null){
    return 0;
} 

if (a == null){
    return -1;
} 

if (b == null){
    return 1;
} 

return a.compareTo(b);

暂无
暂无

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

相关问题 实施此操作的正确方法是什么? - What is the correct way to implement this do while? 正确的方法来实现Java中以前的Mixin? - Correct way to implement what used to be a Mixin in Java? 用Java实现创建者类和类本身设计的正确方法是什么? - What is the correct way to implement a Creator Class and the Class itself design in Java? 实现连接池的正确方法 - Correct way to implement a connection pool 正确的方法是什么? - What is the correct way to do this? 模拟服务方法调用(来自内部 forEach 循环)时抛出 NullPointerException,实现此模拟的正确方法是什么? - NullPointerException thrown when Mocking a service method call (from an internal forEach loop), what is the correct way to implement this mock? 实现基于提供的通用对象实例化对象的工厂方法的正确方法是什么 - what is the correct way to implement a factory method that instantiates objects based on supplied generic object 实现二维数组中的值支持的模型类的最快,最简洁/正确的方法是什么? - What is the fastest and most concise/correct way to implement this model class backed by values in a 2-dimensional array? 实现AsyncTask的正确方法是什么?静态或非静态嵌套类? - What's the correct way to implement AsyncTask? static or non static nested class? 这是使用执行程序服务实现多线程的正确方法吗? - Is this the correct way to implement multithreading using the executor service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM