lundi 20 juillet 2015

Overriding hashcode with OR condition

I am writing a pojo , where I am overriding hashcode and equals , But my condition for making objects equal is having a OR condition .In this case how to write hashcode ???

For example, I have a pojo, having three fields like aaa,bbb,ccc and condition of treating equal is , aaa must be equal and either bbb or ccc should be equal.I wrote this in equals overriding section but what to write in hashcode in this case ???

public class POJO {

private String aaa;
private String bbb;
private String ccc;

///How to use or condition here ???????
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((aaa == null) ? 0 : aaa.hashCode());
    result = prime * result + ((bbb == null) ? 0 : bbb.hashCode());
    return result;
}
//my condition is aaa and (bbb or ccc) should be equal
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    POJO other = (POJO) obj;
    if (aaa == null) {
        if (other.aaa != null)
            return false;
    } else if (!aaa.equals(other.aaa))
        return false;
    if (bbb == null || ccc == null) {
        if (other.bbb != null || other.ccc != null)
            return false;
   //This is the main condition
    } else if (!bbb.equals(other.bbb) || !ccc.equals(other.ccc))
        return false;
    return true;
}
public String getAaa() {
    return aaa;
}
public void setAaa(String aaa) {
    this.aaa = aaa;
}
public String getBbb() {
    return bbb;
}
public void setBbb(String bbb) {
    this.bbb = bbb;
}
public String getCcc() {
    return ccc;
}
public void setCcc(String ccc) {
    this.ccc = ccc;
}




}

Aucun commentaire:

Enregistrer un commentaire