Skip to content

Commit ce55753

Browse files
author
duke
committedAug 10, 2020
Automatic merge of jdk:master into master
2 parents 2f5a53d + b5f785b commit ce55753

File tree

3 files changed

+91
-53
lines changed

3 files changed

+91
-53
lines changed
 

‎src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/datatype/XMLGregorianCalendarImpl.java

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -190,7 +190,7 @@
190190
* @author Sunitha Reddy
191191
* @see javax.xml.datatype.Duration
192192
* @since 1.5
193-
* @LastModified: June 2018
193+
* @LastModified: Aug 2020
194194
*/
195195

196196
public class XMLGregorianCalendarImpl
@@ -1682,49 +1682,6 @@ private static int compareField(BigDecimal Pfield, BigDecimal Qfield) {
16821682
return Pfield.compareTo(Qfield);
16831683
}
16841684

1685-
/**
1686-
* <p>Indicates whether parameter <code>obj</code> is "equal to" this one.</p>
1687-
*
1688-
* @param obj to compare.
1689-
*
1690-
* @return <code>true</code> when <code>compare(this,(XMLGregorianCalendar)obj) == EQUAL.</code>.
1691-
*/
1692-
public boolean equals(Object obj) {
1693-
1694-
if (obj == null || !(obj instanceof XMLGregorianCalendar)) {
1695-
return false;
1696-
}
1697-
if (obj == this) {
1698-
return true;
1699-
}
1700-
return compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL;
1701-
}
1702-
1703-
/**
1704-
* <p>Returns a hash code consistent with the definition of the equals method.</p>
1705-
*
1706-
* @return hash code of this object.
1707-
*/
1708-
public int hashCode() {
1709-
1710-
// Following two dates compare to EQUALS since in different timezones.
1711-
// 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
1712-
//
1713-
// Must ensure both instances generate same hashcode by normalizing
1714-
// this to UTC timezone.
1715-
int timezone = getTimezone();
1716-
if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
1717-
timezone = 0;
1718-
}
1719-
XMLGregorianCalendar gc = this;
1720-
if (timezone != 0) {
1721-
gc = this.normalizeToTimezone(getTimezone());
1722-
}
1723-
return gc.getYear() + gc.getMonth() + gc.getDay() +
1724-
gc.getHour() + gc.getMinute() + gc.getSecond();
1725-
}
1726-
1727-
17281685
/**
17291686
* <p>Constructs a new XMLGregorianCalendar object by
17301687
* parsing its lexical string representation as defined in

‎src/java.xml/share/classes/javax/xml/datatype/XMLGregorianCalendar.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
2828
import javax.xml.namespace.QName;
2929
import java.math.BigDecimal;
3030
import java.math.BigInteger;
31+
import java.util.Arrays;
3132
import java.util.TimeZone;
3233
import java.util.GregorianCalendar;
3334

@@ -688,10 +689,12 @@ public int getMillisecond() {
688689
*/
689690
@Override
690691
public boolean equals(Object obj) {
691-
692692
if (obj == null || !(obj instanceof XMLGregorianCalendar)) {
693693
return false;
694694
}
695+
if (obj == this) {
696+
return true;
697+
}
695698
return compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL;
696699
}
697700

@@ -716,12 +719,10 @@ public int hashCode() {
716719
if (timezone != 0) {
717720
gc = this.normalize();
718721
}
719-
return gc.getYear()
720-
+ gc.getMonth()
721-
+ gc.getDay()
722-
+ gc.getHour()
723-
+ gc.getMinute()
724-
+ gc.getSecond();
722+
723+
int[] elements = {gc.getYear(), gc.getMonth(), gc.getDay(), gc.getHour(),
724+
gc.getMinute(), gc.getSecond(), gc.getMillisecond()};
725+
return Arrays.hashCode(elements);
725726
}
726727

727728
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package datatype;
25+
26+
import javax.xml.datatype.DatatypeFactory;
27+
import javax.xml.datatype.XMLGregorianCalendar;
28+
import org.testng.annotations.Test;
29+
import org.testng.Assert;
30+
import org.testng.annotations.DataProvider;
31+
32+
/*
33+
* @test
34+
* @bug 8246816
35+
* @run testng datatype.HashCodeTest
36+
* @summary Test hashCode generation.
37+
*/
38+
public class HashCodeTest {
39+
/*
40+
DataProvider: for testHashCode
41+
Data: datetime1, datetime2, flag indicating if their hashCodes are equal
42+
*/
43+
@DataProvider(name = "testHashCode")
44+
public Object[][] getData() {
45+
46+
return new Object[][]{
47+
// the reported case: identical hash codes before the patch
48+
{"2020-04-24T12:53:00+02:00", "2020-06-04T06:58:17.727Z", false},
49+
// a case mentioned in the dev note of hashCode() implementation
50+
{"2000-01-15T12:00:00-05:00", "2000-01-15T13:00:00-04:00", true},
51+
/**
52+
* Comparing with a datetime that needs to be normalized.
53+
* Before the patch, XMLGregorianCalendarImpl called the normalizeToTimezone
54+
* method that will set UNDEFINED fractional second to zero.
55+
*/
56+
{"2000-01-01T03:19:04Z", "1999-12-31T23:49:04-03:30", true},
57+
// another case mentioned in the javadoc of XMLGregorianCalendar::normalize()
58+
{"2000-03-04T23:00:00+03:00", "2000-03-04T20:00:00Z", true},
59+
};
60+
}
61+
62+
@Test(dataProvider = "testHashCode")
63+
public final void testHashCode(String dt1, String dt2, boolean equal) throws Exception {
64+
DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
65+
XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar(dt1);
66+
XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar(dt2);
67+
68+
// identical hash codes before the patch
69+
int hashCode1 = cal1.hashCode();
70+
int hashCode2 = cal2.hashCode();
71+
72+
if (equal) {
73+
Assert.assertTrue(cal1.equals(cal2));
74+
Assert.assertEquals(hashCode1, hashCode2);
75+
} else {
76+
Assert.assertFalse(cal1.equals(cal2));
77+
Assert.assertNotEquals(hashCode1, hashCode2);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)
Please sign in to comment.