View Javadoc
1   /*
2    * Copyright (c) 2010, 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 java.math.BigInteger;
29  import javax.xml.datatype.DatatypeConstants;
30  
31  
32  /**
33   * <p>Represent a subtype <code>xdt:yearMonthDuration</code> of a <code>Duration</code>
34   * as specified in <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
35   *   XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
36   *
37   *
38   * <p>The DurationYearMonth object represents a period of Gregorian time,
39   * with a lexical representation, "<em>PnYnM</em>" that contains only year and month components.
40   * </p>
41   *
42   *
43   * @author <a href="mailto:Vikram.Aroskar@Sun.COM">Vikram Aroskar</a>
44   * @author <a href="mailto:Huizhe.wang@oracle.com">Joe Wang</a>
45   * @version $Revision: 1.1 $, $Date: 2010/05/19 05:02:55 $
46  
47   * @see XMLGregorianCalendar#add(Duration)
48   */
49  
50  class DurationYearMonthImpl
51          extends DurationImpl {
52  
53  
54      /**
55       * <p>Constructs a new Duration object by specifying each field individually.</p>
56       *
57       * <p>All the parameters are optional as long as at least one field is present.
58       * If specified, parameters have to be zero or positive.</p>
59       *
60       * @param isPositive Set to <code>false</code> to create a negative duration. When the length
61       *   of the duration is zero, this parameter will be ignored.
62       * @param years of this <code>Duration</code>
63       * @param months of this <code>Duration</code>
64       *
65       * @throws IllegalArgumentException
66       *    If years, months parameters are all <code>null</code>. Or if any
67       *    of those parameters are negative.
68       */
69      public DurationYearMonthImpl(
70          boolean isPositive,
71          BigInteger years,
72          BigInteger months) {
73  
74          super(isPositive, years, months, null, null, null, null);
75          convertToCanonicalYearMonth();
76      }
77          /**
78           * <p>Construct a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified
79           * <code>year</code> and <code>month</code> as defined in
80           * <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
81           *   XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
82           *
83       * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
84       *
85       * @param isPositive Set to <code>false</code> to create a negative duration. When the length
86       *   of the duration is zero, this parameter will be ignored.
87           * @param year Year of <code>Duration</code>.
88           * @param month Month of <code>Duration</code>.
89           *
90           * @throws IllegalArgumentException If the values are not a valid representation of a
91           * <code>Duration</code>: if any of the fields (year, month) is negative.
92           */
93      protected DurationYearMonthImpl(
94          final boolean isPositive,
95          final int years,
96          final int months) {
97  
98          this(isPositive,
99              wrap(years),
100             wrap(months));
101 
102 
103     }
104 
105 
106         /**
107          * <p>Construct a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified milliseconds as defined in
108          * <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
109          *   XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
110          *
111          * <p>The datatype <code>xdt:yearMonthDuration</code> is a subtype of <code>xs:duration</code>
112          * whose lexical representation contains only year and month components.
113          * This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.</p>
114          *
115      * <p>Both values are set by computing their values from the specified milliseconds
116      * and are availabe using the <code>get</code> methods of  the created {@link Duration}.
117      * The values conform to and are defined by:</p>
118      * <ul>
119      *   <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
120      *   <li><a href="http://www.w3.org/TR/xmlschema-2/#isoformats">
121      *     W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
122      *   </li>
123      *   <li>{@link XMLGregorianCalendar}  Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
124      * </ul>
125      *
126          * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
127          * {@link java.util.Calendar#YEAR} = 1970,
128          * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
129          * {@link java.util.Calendar#DATE} = 1, etc.
130          * This is important as there are variations in the Gregorian Calendar,
131          * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
132          * so the result of {@link Duration#getMonths()} can be influenced.</p>
133          *
134      * <p>Any remaining milliseconds after determining the year and month are discarded.</p>
135          *
136          * @param durationInMilliseconds Milliseconds of <code>Duration</code> to create.
137          */
138     protected DurationYearMonthImpl(long durationInMilliseconds) {
139 
140         super(durationInMilliseconds);
141         convertToCanonicalYearMonth();
142         //Any remaining milliseconds after determining the year and month are discarded.
143         days = null;
144         hours = null;
145         minutes = null;
146         seconds = null;
147         signum = calcSignum((signum<0)?false:true);
148     }
149 
150 
151         /**
152          * <p>Construct a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> by parsing its <code>String</code> representation,
153          * "<em>PnYnM</em>", <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
154          *   XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
155          *
156          * <p>The datatype <code>xdt:yearMonthDuration</code> is a subtype of <code>xs:duration</code>
157          * whose lexical representation contains only year and month components.
158          * This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.</p>
159          *
160      * <p>Both values are set and availabe from the created {@link Duration}</p>
161          *
162      * <p>The XML Schema specification states that values can be of an arbitrary size.
163      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
164      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
165      * if implementation capacities are exceeded.</p>
166      *
167          * @param lexicalRepresentation Lexical representation of a duration.
168          *
169          * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code> expressed only in terms of years and months.
170          * @throws UnsupportedOperationException If implementation cannot support requested values.
171          * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
172          */
173     protected DurationYearMonthImpl(String lexicalRepresentation) {
174         super(lexicalRepresentation);
175         if (getDays() > 0 || getHours() > 0
176                 || getMinutes() > 0 || getSeconds() > 0) {
177             throw new IllegalArgumentException(
178                     "Trying to create an xdt:yearMonthDuration with an invalid"
179                     + " lexical representation of \"" + lexicalRepresentation
180                     + "\", data model requires PnYnM.");
181         }
182         convertToCanonicalYearMonth();
183     }
184 
185     /**
186      * The value space of xs:yearMonthDuration is the set of xs:integer month values.
187      * @return the value of yearMonthDuration
188      */
189     public int getValue() {
190         return getYears() * 12 + getMonths();
191     }
192 
193     private void convertToCanonicalYearMonth() {
194         while (getMonths() >= 12)
195         {
196             months = months.subtract(BigInteger.valueOf(12));
197             years = BigInteger.valueOf((long) getYears()).add(BigInteger.ONE);
198         }
199     }
200 }