Java: Clean way of avoiding NullPointerException in equals checks

丶灬走出姿态 提交于 2019-12-21 03:17:08

问题


I have an address object that I want to create an equals method for. I could have made this quite simple by doing something like the following (shortened a bit):

public boolean equals(Object obj) 
{
    if (this == obj)
        return true;

    if (obj == null)
        return false;

    if (getClass() != obj.getClass())
        return false;

    Address other = (Address) obj;

    return this.getStreet().equals(other.getStreet())
        && this.getStreetNumber().equals(other.getStreetNumber())
        && this.getStreetLetter().equals(other.getStreetLetter())
        && this.getTown().equals(other.getTown());
}

Problem is, some of these might be null. I will in other words get a NullPointerException if there is no street letter in this address.

How can I write this in a clean way while taking null values into account?


回答1:


You can use a helper method like

public static boolean isEqual(Object o1, Object o2) {
    return o1 == o2 || (o1 != null && o1.equals(o2));
}



回答2:


Google Guava provides Objects.equal(Object, Object) which checks for equality while taking into consideration that either of the parameters might be null:

...
return Objects.equal(this.getStreet(), other.getStreet())
    && Objects.equal(this.getStreetNumber(), other.getStreetNumber())
    && Objects.equal(this.getStreetLetter(), other.getStreetLetter())
    && Objects.equal(this.getTown(), other.getTown());

It's also worth pointing out that Objects has other helper methods for implementing hashCode() and toString().




回答3:


You could do the following:

public boolean equals(Object obj) 
{
    if (this == obj) {
        return true;
    }

    if (obj == null) {
        return false;
    }

    if (getClass() != obj.getClass()) {
        return false;
    }

    Address other = (Address) obj;

    return equals(this.getStreet(),other.getStreet())
        && equals(this.getStreetNumber(), other.getStreetNumber())
        && equals(this.getStreetLetter(), other.getStreetLetter())
        && equals(this.getTown(), other.getTown());
}

private boolean equals(Object control, Object test) {
    if(null == control) {
        return null == test;
    }
    return control.equals(test);
}

Java 7 introduced built-in support for this use case with the java.util.Objects class see:

  • java.utils.Objects.equals(Object, Object)
  • java.utils.Objects.deepEquals(Object, Object)



回答4:


I have a helper class Checker w/ a static method:

 public static boolean isEquals(final Object o1, final Object o2) {
        return o1 == null ? o2 == null : o1.equals(o2);
 }

so, in the equals method,

 return Checker.isEquals(this.getStreet(), other.getStreet())
        && Checker.isEquals(this.getStreetNumber(), other.getStreetNumber())
        && Checker.isEquals(this.getStreetLetter(), other.getStreetLetter())
        && Checker.isEquals(this.getTown(), other.getTown());



回答5:


There is no really clean way to do that; the best option is probably to have your IDE generate the code for you. Eclipse can do it via the Source -> Generate hashCode() and equals() context menu.




回答6:


You can use Objects.equal from Googles guava or the EqualsBuilder from apache commons




回答7:


I'd consider defining some of the equals methods as static class methods, like say for the Street objects. This way you don't ever attempt to call the .equals() method on a null.

A sample function might look like:

public static boolean equals(Object one, Object two)

Also, it's good practice to put checks like

if (obj == null)
   return false;

at the very beginning of a function.




回答8:


Apache Commons Lang provides the EqualsBuilder helper class for equality comparissons. There is also one for hash codes.

return new EqualsBuilder()
.append(this.getStreet(), other.getStreet())
.append(this.getStreetNumber(), other.getStreetNumber()
.append(this.getStreetLetter(), other.getStreetLetter())
.append(this.getTown(), other.getTown())).isEquals();


来源:https://stackoverflow.com/questions/5661936/java-clean-way-of-avoiding-nullpointerexception-in-equals-checks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!