'. '

Equals

From APIDesign

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
Writing equals method in [[OOP]] languages can be tricky. The {{JDK|java/lang|Object}}.equals documentation suggest that the relation should be [[wikipedia:Symmetric_relation|symetric]] but that is hard to enforce. Anyway implementation should at least try. However how should one get ready for subclasses? E.g. make sure instance of following class:
+
Writing [[equals]] method in [[OOP]] languages can be tricky. The {{JDK|java/lang|Object}}.[[equals]] documentation suggests that the relation should be [[wikipedia:Symmetric_relation|symetric]] but that is hard to enforce. Anyway implementation should at least try. However how should one handle (potentially unknown) subclasses? E.g. make sure instance of following class:
<source lang="java">
<source lang="java">
Line 36: Line 36:
</source>
</source>
-
This is approach suitable for [[wikipedia:Algebraic_types|algebraic types]], but in [[OOP]] we might want intervals of length zero to be equal to the ''Date'' object with the same beginning. Then it comes to question: ''Who knows more?'' (also discussed in [[SuperVsInner]] essay). In [[Java]] it is safe to assume that ''subclasses know more'' - as such it should be the subclass who handles the ''equals'':
+
This is approach suitable for [[wikipedia:Algebraic_types|algebraic types]], but in [[OOP]] we might want some subclasses of ''Date'' to still be equal to plain ''Date'' instances. For example intervals of length zero may need to be equal to the ''Date'' object with the same beginning ''time''.
 +
 
 +
Then it comes to question: ''Who knows more?'' As discussed in [[SuperVsInner]] essay in [[Java]] it is safe to assume that ''subclasses know more'' - as such it should be the subclass who handles the ''equals'' call:
<source lang="java">
<source lang="java">
class Date {
class Date {
-
long time;
+
private long time;
public boolean equals(Object o) {
public boolean equals(Object o) {
Line 55: Line 57:
class Interval extends Date {
class Interval extends Date {
-
int length;
+
private int length;
public boolean equals(Object o) {
public boolean equals(Object o) {

Revision as of 08:49, 6 September 2013

Writing equals method in OOP languages can be tricky. The Object.equals documentation suggests that the relation should be symetric but that is hard to enforce. Anyway implementation should at least try. However how should one handle (potentially unknown) subclasses? E.g. make sure instance of following class:

class Date {
  long time; 
 
  public boolean equals(Object o) {
    if (o instanceof Date) {
      return ((Date)o).time == time;
    }
    return false;
  }
}

does not return true when compared to a subclass like:

class Interval extends Date {
  int length; 
}
 
assert !new Date(323).equals(new Interval(323, 10));

One way to do it is to restrict the equals only to own type. E.g.:

public boolean equals(Object o) {
    if (o != null && o.getClass() == Date.class) {
      return ((Date)o).time == time;
    }
    return false;
  }
}

This is approach suitable for algebraic types, but in OOP we might want some subclasses of Date to still be equal to plain Date instances. For example intervals of length zero may need to be equal to the Date object with the same beginning time.

Then it comes to question: Who knows more? As discussed in SuperVsInner essay in Java it is safe to assume that subclasses know more - as such it should be the subclass who handles the equals call:

class Date {
  private long time; 
 
  public boolean equals(Object o) {
    if (o instanceof Date) {
      if (o.getClass() != getClass() && getClass().isAssignableFrom(o.getClass())) {
        return o.equals(this); // [1]
      } else {
        return time == ((Date)o).time;
      }
    }
    return false;
  }
}
 
class Interval extends Date {
  private int length; 
 
  public boolean equals(Object o) {
    if (o instanceof Interval) {
      if (o.getClass() != getClass() && getClass().isAssignableFrom(o.getClass())) {
        return o.equals(this); // [1]
      } else {
        return time == ((Interval)o).time && length == ((Interval)o).length;
      }
    }
    return length == 0 && super.equals(o); // [2]
  }  
}
 
class SerializableDate extends Date implements java.io.Serializable {
}
 
assert !new Date(323).equals(new Interval(323, 10));
assert new Date(323).equals(new Interval(323, 0));
assert new Date(323).equals(new SerializableDate(323));

The check [1] makes sure the comparing is always done by subclass. Then the subclass can either do nothing (like the SerializableDate) and inherits working equals from the superclass, or the subclass can use its special knowledge (like Interval at line [2]) and be equal to super class in some special conditions (here when the length is zero).

Personal tools
buy