From 6d36a54999ab867ae7cff9e5f85ce85f1e2617be Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 20 Jan 2011 05:06:54 +0000 Subject: [PATCH] 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 --- src/main/java/com/threerings/util/Name.java | 35 +++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/threerings/util/Name.java b/src/main/java/com/threerings/util/Name.java index c56ad13f2..4c6a849e5 100644 --- a/src/main/java/com/threerings/util/Name.java +++ b/src/main/java/com/threerings/util/Name.java @@ -112,13 +112,18 @@ public class Name extends SimpleStreamableObject @Override 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 oc = other.getClass(); // 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 if (c == oc || c.getName().equals(oc.getName())) { - return getNormal().equals(((Name)other).getNormal()); + return getNormal().equals(oname.getNormal()); } } return false; @@ -127,6 +132,12 @@ public class Name extends SimpleStreamableObject // from interface Comparable 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 oc = other.getClass(); if (c == oc || c.getName().equals(oc.getName())) { @@ -150,6 +161,26 @@ public class Name extends SimpleStreamableObject 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. */ protected String _name;