On Fri, 2005-12-16 at 20:22 +0100, Wolfgang Baer wrote:
+ /**
+ * Tests if the given object is equal to this object.
+ * The objects are considered equal if both are of the same
+ * Media subclass, not null and the values are equal.
+ *
+ * @param obj the object to test
+ *
+ * @return <code>true</code> if both objects are equal,
+ * <code>false</code> otherwise.
+ */
+ public boolean equals(Object obj)
+ {
+ if ((obj instanceof MediaName && this instanceof MediaName)
+ || (obj instanceof MediaTray && this instanceof MediaTray)
+ || (obj instanceof MediaSizeName && this instanceof MediaSizeName))
+ {
+ return ((Media) obj).getValue() == this.getValue();
+ }
+
+ return false;
+ }
Wouldn't it be a bit more robust to write this as:
return (obj.getClass() == this.getClass()
&& ((Media) obj).getValue() == this.getValue());
Then you also accept any future subclasses.