I don't think this will break anything: provide a means for names to override

the default comparison methods in a symmetric way (so that it doesn't matter
which of the two objects' equals/compareTo methods was called).


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6465 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2011-01-20 05:06:54 +00:00
parent 92c1225d0e
commit 6d36a54999
+33 -2
View File
@@ -112,13 +112,18 @@ public class Name extends SimpleStreamableObject
@Override @Override
public boolean equals (Object other) public boolean equals (Object other)
{ {
if (other != null) { if (other instanceof Name) {
Name oname = (Name)other;
Boolean override = overrideEquals(oname);
if (override != null || (override = oname.overrideEquals(this)) != null) {
return override;
}
Class<?> c = getClass(); Class<?> c = getClass();
Class<?> oc = other.getClass(); Class<?> oc = other.getClass();
// we have to be of the same derived class but we don't want to // we have to be of the same derived class but we don't want to
// wig out if the classes were loaded from different class loaders // wig out if the classes were loaded from different class loaders
if (c == oc || c.getName().equals(oc.getName())) { if (c == oc || c.getName().equals(oc.getName())) {
return getNormal().equals(((Name)other).getNormal()); return getNormal().equals(oname.getNormal());
} }
} }
return false; return false;
@@ -127,6 +132,12 @@ public class Name extends SimpleStreamableObject
// from interface Comparable<Name> // from interface Comparable<Name>
public int compareTo (Name other) public int compareTo (Name other)
{ {
Integer override = overrideCompareTo(other);
if (override != null) {
return override;
} else if ((override = other.overrideCompareTo(this)) != null) {
return -override;
}
Class<?> c = getClass(); Class<?> c = getClass();
Class<?> oc = other.getClass(); Class<?> oc = other.getClass();
if (c == oc || c.getName().equals(oc.getName())) { if (c == oc || c.getName().equals(oc.getName())) {
@@ -150,6 +161,26 @@ public class Name extends SimpleStreamableObject
return name; return name;
} }
/**
* Gives this name a chance to override the default equality comparison in a symmetric fashion.
*
* @return the result of the comparison, or null for no override.
*/
protected Boolean overrideEquals (Name other)
{
return null;
}
/**
* Gives this name a chance to override the default comparison in a symmetric fashion.
*
* @return the result of the comparison, or null for no override.
*/
protected Integer overrideCompareTo (Name other)
{
return null;
}
/** The raw name text. */ /** The raw name text. */
protected String _name; protected String _name;