1 /*
2 * Copyright (c) 2003, 2017, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.xml.datatype;
27
28 import javax.xml.namespace.QName;
29 import java.math.BigDecimal;
30 import java.math.BigInteger;
31 import java.util.TimeZone;
32 import java.util.GregorianCalendar;
33
34 /**
35 * <p>Representation for W3C XML Schema 1.0 date/time datatypes.
36 * Specifically, these date/time datatypes are
37 * {@link DatatypeConstants#DATETIME},
38 * {@link DatatypeConstants#TIME},
39 * {@link DatatypeConstants#DATE},
40 * {@link DatatypeConstants#GYEARMONTH},
41 * {@link DatatypeConstants#GMONTHDAY},
42 * {@link DatatypeConstants#GYEAR},
43 * {@link DatatypeConstants#GMONTH}, and
44 * {@link DatatypeConstants#GDAY}
45 * defined in the XML Namespace
46 * {@code "http://www.w3.org/2001/XMLSchema"}.
47 * These datatypes are normatively defined in
48 * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">W3C XML Schema 1.0 Part 2, Section 3.2.7-14</a>.
49 *
50 * <p>The table below defines the mapping between XML Schema 1.0
699 * Returns a hash code consistent with the definition of the equals method.
700 *
701 * @return hash code of this object.
702 */
703 @Override
704 public int hashCode() {
705
706 // Following two dates compare to EQUALS since in different timezones.
707 // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
708 //
709 // Must ensure both instances generate same hashcode by normalizing
710 // this to UTC timezone.
711 int timezone = getTimezone();
712 if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
713 timezone = 0;
714 }
715 XMLGregorianCalendar gc = this;
716 if (timezone != 0) {
717 gc = this.normalize();
718 }
719 return gc.getYear()
720 + gc.getMonth()
721 + gc.getDay()
722 + gc.getHour()
723 + gc.getMinute()
724 + gc.getSecond();
725 }
726
727 /**
728 * Return the lexical representation of {@code this} instance.
729 * The format is specified in
730 * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
731 * <i>Lexical Representation</i>".</a>
732 *
733 * <p>Specific target lexical representation format is determined by
734 * {@link #getXMLSchemaType()}.
735 *
736 * @return XML, as {@code String}, representation of this {@code XMLGregorianCalendar}
737 *
738 * @throws IllegalStateException if the combination of set fields
739 * does not match one of the eight defined XML Schema builtin date/time datatypes.
740 */
741 public abstract String toXMLFormat();
742
743 /**
744 * Return the name of the XML Schema date/time type that this instance
|
1 /*
2 * Copyright (c) 2003, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.xml.datatype;
27
28 import javax.xml.namespace.QName;
29 import java.math.BigDecimal;
30 import java.math.BigInteger;
31 import java.util.Arrays;
32 import java.util.TimeZone;
33 import java.util.GregorianCalendar;
34
35 /**
36 * <p>Representation for W3C XML Schema 1.0 date/time datatypes.
37 * Specifically, these date/time datatypes are
38 * {@link DatatypeConstants#DATETIME},
39 * {@link DatatypeConstants#TIME},
40 * {@link DatatypeConstants#DATE},
41 * {@link DatatypeConstants#GYEARMONTH},
42 * {@link DatatypeConstants#GMONTHDAY},
43 * {@link DatatypeConstants#GYEAR},
44 * {@link DatatypeConstants#GMONTH}, and
45 * {@link DatatypeConstants#GDAY}
46 * defined in the XML Namespace
47 * {@code "http://www.w3.org/2001/XMLSchema"}.
48 * These datatypes are normatively defined in
49 * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">W3C XML Schema 1.0 Part 2, Section 3.2.7-14</a>.
50 *
51 * <p>The table below defines the mapping between XML Schema 1.0
700 * Returns a hash code consistent with the definition of the equals method.
701 *
702 * @return hash code of this object.
703 */
704 @Override
705 public int hashCode() {
706
707 // Following two dates compare to EQUALS since in different timezones.
708 // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
709 //
710 // Must ensure both instances generate same hashcode by normalizing
711 // this to UTC timezone.
712 int timezone = getTimezone();
713 if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
714 timezone = 0;
715 }
716 XMLGregorianCalendar gc = this;
717 if (timezone != 0) {
718 gc = this.normalize();
719 }
720
721 BigInteger ey = gc.getEonAndYear();
722 int result = 31 + ((ey == null) ? 0 : ey.hashCode());
723 int[] elements = {gc.getMonth(), gc.getDay(), gc.getHour(),
724 gc.getMinute(), gc.getSecond()};
725 result = 31 * result + Arrays.hashCode(elements);
726 BigDecimal fs = gc.getFractionalSecond();
727 result = 31 * result + ((fs == null) ? 0 : fs.hashCode());
728 return result;
729 }
730
731 /**
732 * Return the lexical representation of {@code this} instance.
733 * The format is specified in
734 * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
735 * <i>Lexical Representation</i>".</a>
736 *
737 * <p>Specific target lexical representation format is determined by
738 * {@link #getXMLSchemaType()}.
739 *
740 * @return XML, as {@code String}, representation of this {@code XMLGregorianCalendar}
741 *
742 * @throws IllegalStateException if the combination of set fields
743 * does not match one of the eight defined XML Schema builtin date/time datatypes.
744 */
745 public abstract String toXMLFormat();
746
747 /**
748 * Return the name of the XML Schema date/time type that this instance
|