< 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      */


1664         }
1665         return Pfield.compareTo(Qfield);
1666     }
1667 
1668     private static int compareField(BigDecimal Pfield, BigDecimal Qfield) {
1669         // optimization. especially when both arguments are null.
1670         if (Pfield == Qfield) {
1671             return DatatypeConstants.EQUAL;
1672         }
1673 
1674         if (Pfield == null) {
1675             Pfield = DECIMAL_ZERO;
1676         }
1677 
1678         if (Qfield == null) {
1679             Qfield = DECIMAL_ZERO;
1680         }
1681 
1682         return Pfield.compareTo(Qfield);
1683     }
1684 
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 
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      *
1745      * <p>Returns a non-null valid XMLGregorianCalendar object that holds the value
1746      * indicated by the lexicalRepresentation parameter.</p>


   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      */


1665         }
1666         return Pfield.compareTo(Qfield);
1667     }
1668 
1669     private static int compareField(BigDecimal Pfield, BigDecimal Qfield) {
1670         // optimization. especially when both arguments are null.
1671         if (Pfield == Qfield) {
1672             return DatatypeConstants.EQUAL;
1673         }
1674 
1675         if (Pfield == null) {
1676             Pfield = DECIMAL_ZERO;
1677         }
1678 
1679         if (Qfield == null) {
1680             Qfield = DECIMAL_ZERO;
1681         }
1682 
1683         return Pfield.compareTo(Qfield);
1684     }











































1685 
1686     /**
1687      * <p>Constructs a new XMLGregorianCalendar object by
1688      * parsing its lexical string representation as defined in
1689      * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
1690      * <i>Lexical Representation</i>.</a></p>
1691      *
1692      * <p>The string representation may not have any leading and trailing whitespaces.</p>
1693      *
1694      * <p>The parsing is done field by field so that
1695      * the following holds for any lexically correct string x:</p>
1696      * <pre>
1697      * new XMLGregorianCalendar(x).toXMLFormat().equals(x)
1698      * </pre>
1699      * Except for the noted lexical/canonical representation mismatches
1700      * listed in <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-45">
1701      * XML Schema 1.0 errata, Section 3.2.7.2</a>.
1702      *
1703      * <p>Returns a non-null valid XMLGregorianCalendar object that holds the value
1704      * indicated by the lexicalRepresentation parameter.</p>


< prev index next >