Apache Commons Lang3 Hashcode, Equals and ToString including Enums

淺唱寂寞╮ 提交于 2020-01-03 09:09:12

问题


We have few datatypes defined for our service response and request objects in a model. Recently we found a need of implementing ToString, HashCode and Equals on all such types to make use of these over comparison and assertions. Confirming from few source like What issues should be considered when overriding equals and hashCode in Java?, Right way to implement equals contract etc we followed implementing toString, equals and hashcode using org.apache.commons.lang3.builder.EqualsBuilder, HashCodeBuilder and ToStringBuilder as follows -


Response.java

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class Response {

    private Integer value;
    private Currency currency;
    private Object edited;

    public Response() {
    }

    public Response(Integer value, Currency currency, Object edited) {
        this.value = value;
        this.currency = currency;
        this.edited = edited;
    }

    public Currency getCurrency() {
        return currency;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public Object getEdited() {
        return edited;
    }

    public void setEdited(Object edited) {
        this.edited = edited;
    }

    @Override public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Response Response = (Response) o;
        return new EqualsBuilder().append(value, Response.value).append(currency, Response.currency)
                                  .append(edited, Response.edited).isEquals();
    }

    @Override public int hashCode() {
        return new HashCodeBuilder(17, 37).append(value).append(currency).append(edited).toHashCode();
    }

    @Override public String toString() {
        return "Response{" + "value=" + value + ", currency=" + currency + ", edited=" + edited + '}';
    }
}

Currency.java

public enum Currency {
    INR
}

On implementing these using the default library version, there is a thought around enums that comes to our mind -

Is it correct to use the default hashcode and equals from the library when a datatype might contain parameters including enums as well? Is there a library(within commons would be great) support to implementing a correct optimized solution to overriding implementation of hashcode and equals?

On a side note does the library implementation needs an improvement here or is it correct to what exists?


Edit: Have added the implementation over an Object field(edited) in the class as well. The concern there being same if I override the hashCode and equals implementation for these as well.

Do I end up using an Object's hashcode which is different for different instances as it is mostly the memory mapped address?

Edit 2: I can also see a concern raised on the inconsistent implementation on HashCode for Enum values on JIRA

来源:https://stackoverflow.com/questions/40028231/apache-commons-lang3-hashcode-equals-and-tostring-including-enums

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