0

ok this is really a simple question but I can't understand why my code does not work properly.

In a third part library I'm using, at a certain point something like this is done:

Object value = someValue;
Object compareValue = someOtherValue;
if(value.equals(compareValue)) 
 // do something

now, my objects are instances of the same class, that override equals with the following contract:

 @Override
public boolean equals(Object obj) {

the jvm anyway call the equals defined by the object class, giving me an unwanted behavior. How can I fix this? I repeat that the calling code is an external library that i can't modify.

edit: this is the full code of my class:

public class MissionPriorityResolutionCriteria implements ResolutionCriteria {
private Satellite prioritySatellite;

public MissionPriorityResolutionCriteria(Satellite prioritySatellite) {
    this.prioritySatellite = prioritySatellite;
}



@Override
public int hashCode() {
    int hash = 5;
    hash = 53 * hash + (this.prioritySatellite != null ? this.prioritySatellite.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final MissionPriorityResolutionCriteria other = (MissionPriorityResolutionCriteria) obj;
    if (this.prioritySatellite != other.prioritySatellite && (this.prioritySatellite == null || !this.prioritySatellite.equals(other.prioritySatellite))) {
        return false;
    }
    return true;
}

public Satellite getPrioritySatellite() {
    return prioritySatellite;
}

public void setPrioritySatellite(Satellite prioritySatellite) {
    this.prioritySatellite = prioritySatellite;
}

public boolean apply(SRASolution s) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

I know that he is calling the Object.equals because I stepped with the debugger... The equals implementation I'm using is generated by netbeans.

fer.marino
  • 497
  • 1
  • 5
  • 20
  • 3
    If you've overridden equals properly, that can't happen. – Kayaman Jan 21 '14 at 09:54
  • 5
    Please share your equals implementation... – Frank Jan 21 '14 at 09:55
  • http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java Have you overiden it correctly? – Pratik Shelar Jan 21 '14 at 09:59
  • How do you know `Object.equals()` is called? Are you certain `value` and `compareValue` are instance of your class? – Xavi López Jan 21 '14 at 10:00
  • 1
    it was a bulding problem. Some glitch happened with a complex mix of maven and netbeans. That means that the code that I was debugging was an outdated version that does not contain the equals method. Sorry for wasting your time. – fer.marino Jan 21 '14 at 12:43

3 Answers3

1

it was a bulding problem. Some glitch happened with a complex mix of maven and netbeans. That means that the code that I was debugging was an outdated version that does not contain the equals method.

Sorry for wasting your time.

fer.marino
  • 497
  • 1
  • 5
  • 20
0

Override equals method and implement your own code. You can implement your own equals method using instanceof operator which actually check the type of instance variables are same or not. This is the code used in comparison of two strings.

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
      if (anObject instanceof String) {
           String anotherString = (String)anObject;
           int n = count;
           if (n == anotherString.count) {
               char v1[] = value;
                char v2[] = anotherString.value;
                int i = offset;
               int j = anotherString.offset;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++])
                        return false;
                }
               return true;
            }
        }
       return false;
    }
Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
  • Checking `obj.getClass() == MyClass.class` is often better than `instanceof`, because equality needs to be symmetric: http://stackoverflow.com/questions/596462 – yshavit Jan 21 '14 at 10:03
0

jvm anyway call the equals defined by the object class

How did you know that the JVM called equals defined by the object class?

Assuming that is called Object.eqauls: Even if you have overridden equals method, an default implementation provided by IDE such as eclipse, internally gives a call to super class equals() method, which in your case could be Object.eqauls.

The IDE auto-generated code will look something like this:

@Override
public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    return super.equals(obj);
}

As a solution, replace the call to the super.equals() with your own logic for comparison.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38