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
671 * @return {@code this} {@code XMLGregorianCalendar} normalized to UTC.
672 */
673 public abstract XMLGregorianCalendar normalize();
674
675 /**
676 * Compares this calendar to the specified object. The result is
677 * {@code true} if and only if the argument is not null and is an
678 * {@code XMLGregorianCalendar} object that represents the same
679 * instant in time as this object.
680 *
681 * @param obj to compare.
682 *
683 * @return {@code true} when {@code obj} is an instance of
684 * {@code XMLGregorianCalendar} and
685 * {@link #compare(XMLGregorianCalendar obj)}
686 * returns {@link DatatypeConstants#EQUAL},
687 * otherwise {@code false}.
688 */
689 @Override
690 public boolean equals(Object obj) {
691
692 if (obj == null || !(obj instanceof XMLGregorianCalendar)) {
693 return false;
694 }
695 return compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL;
696 }
697
698 /**
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
672 * @return {@code this} {@code XMLGregorianCalendar} normalized to UTC.
673 */
674 public abstract XMLGregorianCalendar normalize();
675
676 /**
677 * Compares this calendar to the specified object. The result is
678 * {@code true} if and only if the argument is not null and is an
679 * {@code XMLGregorianCalendar} object that represents the same
680 * instant in time as this object.
681 *
682 * @param obj to compare.
683 *
684 * @return {@code true} when {@code obj} is an instance of
685 * {@code XMLGregorianCalendar} and
686 * {@link #compare(XMLGregorianCalendar obj)}
687 * returns {@link DatatypeConstants#EQUAL},
688 * otherwise {@code false}.
689 */
690 @Override
691 public boolean equals(Object obj) {
692 if (obj == null || !(obj instanceof XMLGregorianCalendar)) {
693 return false;
694 }
695 if (obj == this) {
696 return true;
697 }
698 return compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL;
699 }
700
701 /**
702 * Returns a hash code consistent with the definition of the equals method.
703 *
704 * @return hash code of this object.
705 */
706 @Override
707 public int hashCode() {
708
709 // Following two dates compare to EQUALS since in different timezones.
710 // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
711 //
712 // Must ensure both instances generate same hashcode by normalizing
713 // this to UTC timezone.
714 int timezone = getTimezone();
715 if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
716 timezone = 0;
717 }
718 XMLGregorianCalendar gc = this;
719 if (timezone != 0) {
720 gc = this.normalize();
721 }
722
723 int[] elements = {gc.getYear(), gc.getMonth(), gc.getDay(), gc.getHour(),
724 gc.getMinute(), gc.getSecond(), gc.getMillisecond()};
725 return Arrays.hashCode(elements);
726 }
727
728 /**
729 * Return the lexical representation of {@code this} instance.
730 * The format is specified in
731 * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
732 * <i>Lexical Representation</i>".</a>
733 *
734 * <p>Specific target lexical representation format is determined by
735 * {@link #getXMLSchemaType()}.
736 *
737 * @return XML, as {@code String}, representation of this {@code XMLGregorianCalendar}
738 *
739 * @throws IllegalStateException if the combination of set fields
740 * does not match one of the eight defined XML Schema builtin date/time datatypes.
741 */
742 public abstract String toXMLFormat();
743
744 /**
745 * Return the name of the XML Schema date/time type that this instance
|