-
Bug
-
Resolution: Fixed
-
P4
-
11.0.7
-
b11
-
x86_64
-
windows_10
A DESCRIPTION OF THE PROBLEM :
javax.xml.datatype.XMLGregorianCalendar.hashCode() and com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.hashCode() simply sum up the fields of calendar (and not even the milliseconds) it produces far too many identical hashes.
Of course the contract of a hashCode() function is not violated, still collisions occur very frequently.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar("2020-04-24T12:53:00+02:00");
XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar("2020-06-04T06:58:17.727Z");
// This will produce identical hash codes
int hashCode1 = cal1.hashCode();
int hashCode2 = cal2.hashCode();
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Use a proper hash code implementation, in the worst case I think even XMLGregorianCalendar.normalize().toString().hashCode() is acceptable or use org.apache.commons.lang.builder.HashCodeBuilder to hash the individual fields.
ACTUAL -
Frequent collisions
---------- BEGIN SOURCE ----------
DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar("2020-04-24T12:53:00+02:00");
XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar("2020-06-04T06:58:17.727Z");
// This will produce identical hash codes
int hashCode1 = cal1.hashCode();
int hashCode2 = cal2.hashCode();
assertNotEquals(
"Version should be different because XMLGregorianCalendars are different. cal1: " + cal1 + " cal2: " + cal2,
hashCode1, hashCode2);
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
XMLGregorianCalendar.normalize().toString().hashCode()
javax.xml.datatype.XMLGregorianCalendar.hashCode() and com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.hashCode() simply sum up the fields of calendar (and not even the milliseconds) it produces far too many identical hashes.
Of course the contract of a hashCode() function is not violated, still collisions occur very frequently.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar("2020-04-24T12:53:00+02:00");
XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar("2020-06-04T06:58:17.727Z");
// This will produce identical hash codes
int hashCode1 = cal1.hashCode();
int hashCode2 = cal2.hashCode();
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Use a proper hash code implementation, in the worst case I think even XMLGregorianCalendar.normalize().toString().hashCode() is acceptable or use org.apache.commons.lang.builder.HashCodeBuilder to hash the individual fields.
ACTUAL -
Frequent collisions
---------- BEGIN SOURCE ----------
DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar("2020-04-24T12:53:00+02:00");
XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar("2020-06-04T06:58:17.727Z");
// This will produce identical hash codes
int hashCode1 = cal1.hashCode();
int hashCode2 = cal2.hashCode();
assertNotEquals(
"Version should be different because XMLGregorianCalendars are different. cal1: " + cal1 + " cal2: " + cal2,
hashCode1, hashCode2);
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
XMLGregorianCalendar.normalize().toString().hashCode()