View Javadoc
1   /*
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3    *
4    * This code is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License version 2 only, as
6    * published by the Free Software Foundation.  Oracle designates this
7    * particular file as subject to the "Classpath" exception as provided
8    * by Oracle in the LICENSE file that accompanied this code.
9    *
10   * This code is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13   * version 2 for more details (a copy is included in the LICENSE file that
14   * accompanied this code).
15   *
16   * You should have received a copy of the GNU General Public License version
17   * 2 along with this work; if not, write to the Free Software Foundation,
18   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19   *
20   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21   * or visit www.oracle.com if you need additional information or have any
22   * questions.
23   */
24  
25  /*
26   * Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.
27   */
28  
29  package com.sun.org.apache.xerces.internal.jaxp.datatype;
30  
31  
32  import java.math.BigInteger;
33  import java.math.BigDecimal;
34  import javax.xml.datatype.DatatypeConstants;
35  
36  /**
37   * <p>Represent a subtype <code>xdt:dayTimeDuration</code> of a <code>Duration</code>
38   * as specified in <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
39   *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
40   *
41   *
42   * <p>The DurationYearMonth object represents a period of Gregorian time,
43   * with a lexical representation, "<em>PnDTnHnMnS</em>" that contains only year and month components.
44   * </p>
45   *
46   *
47   * @author <a href="mailto:Vikram.Aroskar@Sun.COM">Vikram Aroskar</a>
48   * @author <a href="mailto:Huizhe.wang@oracle.com">Joe Wang</a>
49   * @version $Revision: 1.2 $, $Date: 2010/05/19 23:20:06 $
50  
51   * @see XMLGregorianCalendar#add(Duration)
52   */
53  
54  class DurationDayTimeImpl
55          extends DurationImpl {
56  
57      public DurationDayTimeImpl(
58          boolean isPositive,
59          BigInteger days,
60          BigInteger hours,
61          BigInteger minutes,
62          BigDecimal seconds) {
63  
64          super(isPositive, null, null, days, hours, minutes, seconds);
65          convertToCanonicalDayTime();
66      }
67  
68      public DurationDayTimeImpl(
69          boolean isPositive,
70          int days,
71          int hours,
72          int minutes,
73          int seconds) {
74  
75          this(
76              isPositive,
77              wrap(days),
78              wrap(hours),
79              wrap(minutes),
80              (seconds != DatatypeConstants.FIELD_UNDEFINED ? new BigDecimal(String.valueOf(seconds)) : null));
81      }
82  
83          /**
84           * <p>Construct a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> by parsing its <code>String</code> representation,
85           * "<em>PnDTnHnMnS</em>", <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
86           *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
87           *
88           * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
89           * whose lexical representation contains only day, hour, minute, and second components.
90           * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>;
91           *
92       * <p>All four values are set and availabe from the created {@link Duration}</p>
93           *
94       * <p>The XML Schema specification states that values can be of an arbitrary size.
95       * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
96       * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
97       * if implementation capacities are exceeded.</p>
98       *
99           * @param lexicalRepresentation Lexical representation of a duration.
100          *
101          * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code> expressed only in terms of days and time.
102          * @throws UnsupportedOperationException If implementation cannot support requested values.
103          * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
104          */
105     protected DurationDayTimeImpl(String lexicalRepresentation) {
106         super(lexicalRepresentation);
107 
108         if (getYears() > 0 || getMonths() > 0) {
109             throw new IllegalArgumentException(
110                     "Trying to create an xdt:dayTimeDuration with an invalid"
111                     + " lexical representation of \"" + lexicalRepresentation
112                     + "\", data model requires a format PnDTnHnMnS.");
113         }
114 
115         convertToCanonicalDayTime();
116     }
117         /**
118          * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified milliseconds as defined in
119          * <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
120          *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
121          *
122          * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
123          * whose lexical representation contains only day, hour, minute, and second components.
124          * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>;
125          *
126      * <p>All four values are set by computing their values from the specified milliseconds
127      * and are availabe using the <code>get</code> methods of  the created {@link Duration}.
128      * The values conform to and are defined by:</p>
129      * <ul>
130      *   <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
131      *   <li><a href="http://www.w3.org/TR/xmlschema-2/#isoformats">
132      *     W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
133      *   </li>
134      *   <li>{@link XMLGregorianCalendar}  Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
135      * </ul>
136          *
137          * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
138          * {@link java.util.Calendar#YEAR} = 1970,
139          * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
140          * {@link java.util.Calendar#DATE} = 1, etc.
141          * This is important as there are variations in the Gregorian Calendar,
142          * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
143          * so the result of {@link Duration#getDays()} can be influenced.</p>
144          *
145      * <p>Any remaining milliseconds after determining the day, hour, minute and second are discarded.</p>
146      *
147      * @param durationInMilliseconds Milliseconds of <code>Duration</code> to create.
148      *
149      * @return New <code>Duration</code> created with the specified <code>durationInMilliseconds</code>.
150      *
151      * @see <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
152      *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>
153      */
154     protected DurationDayTimeImpl(final long durationInMilliseconds) {
155             super(durationInMilliseconds);
156             convertToCanonicalDayTime();
157             // only day, hour, minute, and second should have values
158             years = null;
159             months = null;
160     }
161 
162 
163     /**
164      * The value space of xs:dayTimeDuration is the set of fractional second values.
165      * @return fractional second values
166      */
167     public float getValue() {
168         float sec = (seconds==null)?0:seconds.floatValue();
169         return (((((getDays() * 24) +
170                     getHours()) * 60) +
171                     getMinutes())*60) +
172                     sec;
173     }
174 
175     private void convertToCanonicalDayTime() {
176 
177         while (getSeconds() >= 60)
178         {
179             seconds = seconds.subtract(BigDecimal.valueOf(60));
180             minutes = BigInteger.valueOf((long) getMinutes()).add(BigInteger.ONE);
181         }
182 
183         while (getMinutes() >= 60)
184         {
185             minutes = minutes.subtract(BigInteger.valueOf(60));
186             hours = BigInteger.valueOf((long) getHours()).add(BigInteger.ONE);
187         }
188 
189         while (getHours() >= 24)
190         {
191             hours = hours.subtract(BigInteger.valueOf(24));
192             days = BigInteger.valueOf((long) getDays()).add(BigInteger.ONE);
193         }
194     }
195 
196 }