Author: niallp Date: Mon Jan 10 23:57:00 2011 New Revision: 1057417 URL: http://svn.apache.org/viewvc?rev=1057417&view=rev Log: LANG-636 ExtendedMessageFormat doesn't override equals(Object)
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java?rev=1057417&r1=1057416&r2=1057417&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java Mon Jan 10 23:57:00 2011 @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.Locale; import java.util.Map; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.Validate; /** @@ -69,6 +70,7 @@ import org.apache.commons.lang3.Validate */ public class ExtendedMessageFormat extends MessageFormat { private static final long serialVersionUID = -2362048321261811743L; + private static final int HASH_SEED = 31; private static final String DUMMY_PATTERN = ""; private static final String ESCAPED_QUOTE = "''"; @@ -254,6 +256,49 @@ public class ExtendedMessageFormat exten } /** + * Check if this extended message format is equal to another object. + * + * @param obj the object to compare to + * @return true if this object equals the other, otherwise false + */ + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null) { + return false; + } + if (!super.equals(obj)) { + return false; + } + if (ObjectUtils.notEqual(getClass(), obj.getClass())) { + return false; + } + ExtendedMessageFormat rhs = (ExtendedMessageFormat)obj; + if (ObjectUtils.notEqual(toPattern, rhs.toPattern)) { + return false; + } + if (ObjectUtils.notEqual(registry, rhs.registry)) { + return false; + } + return true; + } + + /** + * Return the hashcode. + * + * @return the hashcode + */ + @Override + public int hashCode() { + int result = super.hashCode(); + result = HASH_SEED * result + ObjectUtils.hashCode(registry); + result = HASH_SEED * result + ObjectUtils.hashCode(toPattern); + return result; + } + + /** * Get a custom format from a format description. * * @param desc String Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java?rev=1057417&r1=1057416&r2=1057417&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java Mon Jan 10 23:57:00 2011 @@ -265,6 +265,48 @@ public class ExtendedMessageFormatTest e } /** + * Test equals() and hashcode. + */ + public void testEqualsHashcode() { + Map<String, ? extends FormatFactory> registry = Collections.singletonMap("testfmt", new LowerCaseFormatFactory()); + Map<String, ? extends FormatFactory> otherRegitry = Collections.singletonMap("testfmt", new UpperCaseFormatFactory()); + + String pattern = "Pattern: {0,testfmt}"; + ExtendedMessageFormat emf = new ExtendedMessageFormat(pattern, Locale.US, registry); + + ExtendedMessageFormat other = null; + + // Same object + assertTrue("same, equals()", emf.equals(emf)); + assertTrue("same, hashcode()", emf.hashCode() == emf.hashCode()); + + // Equal Object + other = new ExtendedMessageFormat(pattern, Locale.US, registry); + assertTrue("equal, equals()", emf.equals(other)); + assertTrue("equal, hashcode()", emf.hashCode() == other.hashCode()); + + // Different Class + other = new OtherExtendedMessageFormat(pattern, Locale.US, registry); + assertFalse("class, equals()", emf.equals(other)); + assertTrue("class, hashcode()", emf.hashCode() == other.hashCode()); // same hashcode + + // Different pattern + other = new ExtendedMessageFormat("X" + pattern, Locale.US, registry); + assertFalse("pattern, equals()", emf.equals(other)); + assertFalse("pattern, hashcode()", emf.hashCode() == other.hashCode()); + + // Different registry + other = new ExtendedMessageFormat(pattern, Locale.US, otherRegitry); + assertFalse("registry, equals()", emf.equals(other)); + assertFalse("registry, hashcode()", emf.hashCode() == other.hashCode()); + + // Different Locale + other = new ExtendedMessageFormat(pattern, Locale.FRANCE, registry); + assertFalse("locale, equals()", emf.equals(other)); + assertTrue("locale, hashcode()", emf.hashCode() == other.hashCode()); // same hashcode + } + + /** * Test a built in format for the specified Locales, plus <code>null</code> Locale. * @param pattern MessageFormat pattern * @param args MessageFormat arguments @@ -394,4 +436,16 @@ public class ExtendedMessageFormatTest e .getDateInstance(DateFormat.DEFAULT, locale); } } + + /** + * Alternative ExtendedMessageFormat impl. + */ + private static class OtherExtendedMessageFormat extends ExtendedMessageFormat { + public OtherExtendedMessageFormat(String pattern, Locale locale, + Map<String, ? extends FormatFactory> registry) { + super(pattern, locale, registry); + } + + } + }