From c64bfb45dcfa87c159892ffa8db3816a1e56c0e3 Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Thu, 26 Jun 2008 20:44:45 +0000 Subject: [PATCH] ICU-6396 Fixed problems in hashCode and equals in DateIntervalInfo.PatternInfo. X-SVN-Rev: 24302 --- .../test/format/DateIntervalFormatTest.java | 24 +++++++++++++++++-- .../com/ibm/icu/text/DateIntervalInfo.java | 15 ++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/icu4j/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java b/icu4j/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java index 21a3bddf32..01bc3c94da 100644 --- a/icu4j/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java +++ b/icu4j/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java @@ -19,11 +19,11 @@ import java.util.Locale; import com.ibm.icu.impl.Utility; import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.DateIntervalFormat; +import com.ibm.icu.text.DateIntervalInfo; import com.ibm.icu.text.SimpleDateFormat; import com.ibm.icu.util.Calendar; import com.ibm.icu.util.DateInterval; -import com.ibm.icu.text.DateIntervalInfo; -import com.ibm.icu.text.DateIntervalFormat; import com.ibm.icu.util.ULocale; public class DateIntervalFormatTest extends com.ibm.icu.dev.test.TestFmwk { @@ -982,4 +982,24 @@ public class DateIntervalFormatTest extends com.ibm.icu.dev.test.TestFmwk { } } } + + /* + * Ticket#6396 DateIntervalInfo of ICU4J 4.0d3 throw NullPointerException + */ + public void TestT6396() { + DateIntervalInfo dii = new DateIntervalInfo(new ULocale("th_TH")); + try { + // ticket#6396 reported toString() throws NullPointerException + String diiStr = dii.toString(); + logln("new DateIntervalInfo(new ULocale(\"th_TH\")).toString() - " + diiStr); + + // equals also had the similar problem + DateIntervalInfo dii1 = (DateIntervalInfo)dii.clone(); + if (!dii.equals(dii1)) { + errln("FAIL: Cloned DateIntervalInfo is not equal to the source"); + } + } catch (Exception e) { + errln("FAIL: Exception - " + e.getClass().getName()); + } + } } diff --git a/icu4j/src/com/ibm/icu/text/DateIntervalInfo.java b/icu4j/src/com/ibm/icu/text/DateIntervalInfo.java index a8a8082258..98500db96a 100644 --- a/icu4j/src/com/ibm/icu/text/DateIntervalInfo.java +++ b/icu4j/src/com/ibm/icu/text/DateIntervalInfo.java @@ -15,6 +15,7 @@ import java.util.HashSet; import com.ibm.icu.impl.ICUResourceBundle; import com.ibm.icu.impl.ICUCache; import com.ibm.icu.impl.SimpleCache; +import com.ibm.icu.impl.Utility; import com.ibm.icu.util.Calendar; import com.ibm.icu.util.ULocale; import com.ibm.icu.util.Freezable; @@ -227,8 +228,8 @@ public class DateIntervalInfo implements Cloneable, Freezable, Serializable { public boolean equals(Object a) { if ( a instanceof PatternInfo ) { PatternInfo patternInfo = (PatternInfo)a; - return fIntervalPatternFirstPart.equals(patternInfo.fIntervalPatternFirstPart) && - fIntervalPatternSecondPart.equals(patternInfo.fIntervalPatternSecondPart) && + return Utility.objectEquals(fIntervalPatternFirstPart, patternInfo.fIntervalPatternFirstPart) && + Utility.objectEquals(fIntervalPatternSecondPart, fIntervalPatternSecondPart) && fFirstDateInPtnIsLaterDate == patternInfo.fFirstDateInPtnIsLaterDate; } return false; @@ -240,8 +241,14 @@ public class DateIntervalInfo implements Cloneable, Freezable, Serializable { * @provisional This API might change or be removed in a future release. */ public int hashCode() { - return fIntervalPatternFirstPart.hashCode() + - fIntervalPatternSecondPart.hashCode(); + int hash = fIntervalPatternFirstPart != null ? fIntervalPatternFirstPart.hashCode() : 0; + if (fIntervalPatternSecondPart != null) { + hash ^= fIntervalPatternSecondPart.hashCode(); + } + if (fFirstDateInPtnIsLaterDate) { + hash ^= -1; + } + return hash; } }