Author: niallp Date: Tue Jan 11 00:28:01 2011 New Revision: 1057427 URL: http://svn.apache.org/viewvc?rev=1057427&view=rev Log: Port LANG-636 to LANG 2.x Branch - ExtendedMessageFormat doesn't override equals(Object)
Modified: commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/ExtendedMessageFormat.java commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/ExtendedMessageFormatTest.java Modified: commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt?rev=1057427&r1=1057426&r2=1057427&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt (original) +++ commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt Tue Jan 11 00:28:01 2011 @@ -40,6 +40,7 @@ BUG FIXES IN 2.6 * [LANG-629] - CharSet: make the underlying set synchronized * [LANG-635] - CompareToBuilder: fix passing along compareTransients to the reflectionCompare method + * [LANG-636] - ExtendedMessageFormat doesn't override equals(Object) * [LANG-645] - FastDateFormat: fix to properly include the locale when formatting a Date * [LANG-638] - NumberUtils: createNumber() throws a StringIndexOutOfBoundsException when argument containing "e" and "E" is passed in * [LANG-607] - StringUtils methods do not handle Unicode 2.0+ supplementary characters correctly Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/ExtendedMessageFormat.java URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/ExtendedMessageFormat.java?rev=1057427&r1=1057426&r2=1057427&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/ExtendedMessageFormat.java (original) +++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/ExtendedMessageFormat.java Tue Jan 11 00:28:01 2011 @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.Locale; import java.util.Map; +import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.Validate; /** @@ -69,6 +70,7 @@ import org.apache.commons.lang.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 = "''"; @@ -248,6 +250,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 + * @since 2.6 + */ + 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 + * @since 2.6 + */ + 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/branches/LANG_2_X/src/site/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml?rev=1057427&r1=1057426&r2=1057427&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml (original) +++ commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml Tue Jan 11 00:28:01 2011 @@ -37,6 +37,7 @@ <action type="add" issue="LANG-482">StrSubstitutor: support substitution in variable names</action> <action type="fix" issue="LANG-629">CharSet: make the underlying set synchronized</action> <action type="fix" issue="LANG-635">CompareToBuilder: fix passing along compareTransients to the reflectionCompare method</action> + <action type="fix" issue="LANG-636">ExtendedMessageFormat doesn't override equals(Object)</action> <action type="fix" issue="LANG-645">FastDateFormat: fix to properly include the locale when formatting a Date</action> <action type="fix" issue="LANG-638">NumberUtils: createNumber() throws a StringIndexOutOfBoundsException when argument containing "e" and "E" is passed in</action> <action type="fix" issue="LANG-607">StringUtils methods do not handle Unicode 2.0+ supplementary characters correctly</action> Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/ExtendedMessageFormatTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/ExtendedMessageFormatTest.java?rev=1057427&r1=1057426&r2=1057427&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/ExtendedMessageFormatTest.java (original) +++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/ExtendedMessageFormatTest.java Tue Jan 11 00:28:01 2011 @@ -263,6 +263,50 @@ public class ExtendedMessageFormatTest e } /** + * Test equals() and hashcode. + */ + public void testEqualsHashcode() { + Map registry = new HashMap(); + registry.put("testfmt", new LowerCaseFormatFactory()); + Map otherRegitry = new HashMap(); + otherRegitry.put("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 @@ -388,4 +432,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 registry) { + super(pattern, locale, registry); + } + + } + }