< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2004, 2018, 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 com.sun.org.apache.xerces.internal.jaxp.datatype;
  27 
  28 import com.sun.org.apache.xerces.internal.util.DatatypeMessageFormatter;
  29 import java.io.IOException;
  30 import java.io.ObjectInputStream;
  31 import java.io.Serializable;
  32 import java.math.BigDecimal;
  33 import java.math.BigInteger;
  34 import java.math.RoundingMode;

  35 import java.util.Calendar;
  36 import java.util.Date;
  37 import java.util.GregorianCalendar;
  38 import java.util.Locale;
  39 import java.util.TimeZone;
  40 import javax.xml.datatype.DatatypeConstants;
  41 import javax.xml.datatype.Duration;
  42 import javax.xml.datatype.XMLGregorianCalendar;
  43 import javax.xml.namespace.QName;
  44 import jdk.xml.internal.SecuritySupport;
  45 
  46 /**
  47  * <p>Representation for W3C XML Schema 1.0 date/time datatypes.
  48  * Specifically, these date/time datatypes are
  49  * {@link DatatypeConstants#DATETIME dateTime},
  50  * {@link DatatypeConstants#TIME time},
  51  * {@link DatatypeConstants#DATE date},
  52  * {@link DatatypeConstants#GYEARMONTH gYearMonth},
  53  * {@link DatatypeConstants#GMONTHDAY gMonthDay},
  54  * {@link DatatypeConstants#GYEAR gYear},


 173  *
 174  * <p>The following operations are defined for this class:
 175  * <ul>
 176  *   <li>factory methods to create instances</li>
 177  *   <li>accessors/mutators for independent date/time fields</li>
 178  *   <li>conversion between this class and W3C XML Schema 1.0 lexical representation</li>
 179  *   <li>conversion between this class and <code>java.util.GregorianCalendar</code></li>
 180  *   <li>partial order relation comparator method, {@link #compare(XMLGregorianCalendar)}</li>
 181  *   <li>{@link #equals(Object)} defined relative to {@link #compare(XMLGregorianCalendar)}.</li>
 182  *   <li> addition operation with {@link javax.xml.datatype.Duration}.
 183  * instance as defined in <a href="http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes">
 184  * W3C XML Schema 1.0 Part 2, Appendix E, <i>Adding durations to dateTimes</i></a>.</li>
 185  * </ul>
 186  * </p>
 187  *
 188  * @author Kohsuke Kawaguchi
 189  * @author Joseph Fialli
 190  * @author Sunitha Reddy
 191  * @see javax.xml.datatype.Duration
 192  * @since 1.5
 193  * @LastModified: June 2018
 194  */
 195 
 196 public class XMLGregorianCalendarImpl
 197         extends XMLGregorianCalendar
 198         implements Serializable, Cloneable {
 199 
 200     /** Backup values **/
 201     transient private BigInteger orig_eon;
 202     transient private int orig_year = DatatypeConstants.FIELD_UNDEFINED;
 203     transient private int orig_month = DatatypeConstants.FIELD_UNDEFINED;
 204     transient private int orig_day = DatatypeConstants.FIELD_UNDEFINED;
 205     transient private int orig_hour = DatatypeConstants.FIELD_UNDEFINED;
 206     transient private int orig_minute = DatatypeConstants.FIELD_UNDEFINED;
 207     transient private int orig_second = DatatypeConstants.FIELD_UNDEFINED;
 208     transient private BigDecimal orig_fracSeconds;
 209     transient private int orig_timezone = DatatypeConstants.FIELD_UNDEFINED;
 210 
 211     /**
 212      * <p>Eon of this <code>XMLGregorianCalendar</code>.</p>
 213      */


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 
1728     /**
1729      * <p>Constructs a new XMLGregorianCalendar object by
1730      * parsing its lexical string representation as defined in
1731      * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
1732      * <i>Lexical Representation</i>.</a></p>
1733      *
1734      * <p>The string representation may not have any leading and trailing whitespaces.</p>
1735      *
1736      * <p>The parsing is done field by field so that
1737      * the following holds for any lexically correct string x:</p>
1738      * <pre>
1739      * new XMLGregorianCalendar(x).toXMLFormat().equals(x)
1740      * </pre>
1741      * Except for the noted lexical/canonical representation mismatches
1742      * listed in <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-45">
1743      * XML Schema 1.0 errata, Section 3.2.7.2</a>.
1744      *


   1 /*
   2  * Copyright (c) 2004, 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 com.sun.org.apache.xerces.internal.jaxp.datatype;
  27 
  28 import com.sun.org.apache.xerces.internal.util.DatatypeMessageFormatter;
  29 import java.io.IOException;
  30 import java.io.ObjectInputStream;
  31 import java.io.Serializable;
  32 import java.math.BigDecimal;
  33 import java.math.BigInteger;
  34 import java.math.RoundingMode;
  35 import java.util.Arrays;
  36 import java.util.Calendar;
  37 import java.util.Date;
  38 import java.util.GregorianCalendar;
  39 import java.util.Locale;
  40 import java.util.TimeZone;
  41 import javax.xml.datatype.DatatypeConstants;
  42 import javax.xml.datatype.Duration;
  43 import javax.xml.datatype.XMLGregorianCalendar;
  44 import javax.xml.namespace.QName;
  45 import jdk.xml.internal.SecuritySupport;
  46 
  47 /**
  48  * <p>Representation for W3C XML Schema 1.0 date/time datatypes.
  49  * Specifically, these date/time datatypes are
  50  * {@link DatatypeConstants#DATETIME dateTime},
  51  * {@link DatatypeConstants#TIME time},
  52  * {@link DatatypeConstants#DATE date},
  53  * {@link DatatypeConstants#GYEARMONTH gYearMonth},
  54  * {@link DatatypeConstants#GMONTHDAY gMonthDay},
  55  * {@link DatatypeConstants#GYEAR gYear},


 174  *
 175  * <p>The following operations are defined for this class:
 176  * <ul>
 177  *   <li>factory methods to create instances</li>
 178  *   <li>accessors/mutators for independent date/time fields</li>
 179  *   <li>conversion between this class and W3C XML Schema 1.0 lexical representation</li>
 180  *   <li>conversion between this class and <code>java.util.GregorianCalendar</code></li>
 181  *   <li>partial order relation comparator method, {@link #compare(XMLGregorianCalendar)}</li>
 182  *   <li>{@link #equals(Object)} defined relative to {@link #compare(XMLGregorianCalendar)}.</li>
 183  *   <li> addition operation with {@link javax.xml.datatype.Duration}.
 184  * instance as defined in <a href="http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes">
 185  * W3C XML Schema 1.0 Part 2, Appendix E, <i>Adding durations to dateTimes</i></a>.</li>
 186  * </ul>
 187  * </p>
 188  *
 189  * @author Kohsuke Kawaguchi
 190  * @author Joseph Fialli
 191  * @author Sunitha Reddy
 192  * @see javax.xml.datatype.Duration
 193  * @since 1.5
 194  * @LastModified: Aug 2020
 195  */
 196 
 197 public class XMLGregorianCalendarImpl
 198         extends XMLGregorianCalendar
 199         implements Serializable, Cloneable {
 200 
 201     /** Backup values **/
 202     transient private BigInteger orig_eon;
 203     transient private int orig_year = DatatypeConstants.FIELD_UNDEFINED;
 204     transient private int orig_month = DatatypeConstants.FIELD_UNDEFINED;
 205     transient private int orig_day = DatatypeConstants.FIELD_UNDEFINED;
 206     transient private int orig_hour = DatatypeConstants.FIELD_UNDEFINED;
 207     transient private int orig_minute = DatatypeConstants.FIELD_UNDEFINED;
 208     transient private int orig_second = DatatypeConstants.FIELD_UNDEFINED;
 209     transient private BigDecimal orig_fracSeconds;
 210     transient private int orig_timezone = DatatypeConstants.FIELD_UNDEFINED;
 211 
 212     /**
 213      * <p>Eon of this <code>XMLGregorianCalendar</code>.</p>
 214      */


1704     /**
1705      * <p>Returns a hash code consistent with the definition of the equals method.</p>
1706      *
1707      * @return hash code of this object.
1708      */
1709     public int hashCode() {
1710 
1711         // Following two dates compare to EQUALS since in different timezones.
1712         // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
1713         //
1714         // Must ensure both instances generate same hashcode by normalizing
1715         // this to UTC timezone.
1716         int timezone = getTimezone();
1717         if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
1718             timezone = 0;
1719         }
1720         XMLGregorianCalendar gc = this;
1721         if (timezone != 0) {
1722             gc = this.normalizeToTimezone(getTimezone());
1723         }
1724 
1725         int[] elements = {gc.getYear(), gc.getMonth(), gc.getDay(), gc.getHour(), 
1726             gc.getMinute(), gc.getSecond(), gc.getMillisecond()};
1727         return Arrays.hashCode(elements);
1728     }
1729 
1730 
1731     /**
1732      * <p>Constructs a new XMLGregorianCalendar object by
1733      * parsing its lexical string representation as defined in
1734      * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
1735      * <i>Lexical Representation</i>.</a></p>
1736      *
1737      * <p>The string representation may not have any leading and trailing whitespaces.</p>
1738      *
1739      * <p>The parsing is done field by field so that
1740      * the following holds for any lexically correct string x:</p>
1741      * <pre>
1742      * new XMLGregorianCalendar(x).toXMLFormat().equals(x)
1743      * </pre>
1744      * Except for the noted lexical/canonical representation mismatches
1745      * listed in <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-45">
1746      * XML Schema 1.0 errata, Section 3.2.7.2</a>.
1747      *


< prev index next >