ICU-6396 Fixed problems in hashCode and equals in DateIntervalInfo.PatternInfo.

X-SVN-Rev: 24302
This commit is contained in:
Yoshito Umaoka 2008-06-26 20:44:45 +00:00
parent 7f702fcd0c
commit c64bfb45dc
2 changed files with 33 additions and 6 deletions

View File

@ -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());
}
}
}

View File

@ -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;
}
}