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 
  50  * @see XMLGregorianCalendar#add(Duration)
  51  */
  52 
  53 class DurationDayTimeImpl
  54         extends DurationImpl {
  55 
  56     public DurationDayTimeImpl(
  57         boolean isPositive,
  58         BigInteger days,
  59         BigInteger hours,
  60         BigInteger minutes,
  61         BigDecimal seconds) {
  62 
  63         super(isPositive, null, null, days, hours, minutes, seconds);
  64         convertToCanonicalDayTime();
  65     }
  66 
  67     public DurationDayTimeImpl(
  68         boolean isPositive,
  69         int days,
  70         int hours,
  71         int minutes,
  72         int seconds) {
  73 
  74         this(
  75             isPositive,
  76             wrap(days),
  77             wrap(hours),
  78             wrap(minutes),
  79             (seconds != DatatypeConstants.FIELD_UNDEFINED ? new BigDecimal(String.valueOf(seconds)) : null));
  80     }
  81 
  82         /**
  83          * <p>Construct a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> by parsing its <code>String</code> representation,
  84          * "<em>PnDTnHnMnS</em>", <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
  85          *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
  86          *
  87          * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
  88          * whose lexical representation contains only day, hour, minute, and second components.
  89          * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
  90          *
  91      * <p>All four values are set and availabe from the created {@link Duration}</p>
  92          *
  93      * <p>The XML Schema specification states that values can be of an arbitrary size.
  94      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
  95      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
  96      * if implementation capacities are exceeded.</p>
  97      *
  98          * @param lexicalRepresentation Lexical representation of a duration.
  99          *
 100          * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code> expressed only in terms of days and time.
 101          * @throws UnsupportedOperationException If implementation cannot support requested values.
 102          * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
 103          */
 104     protected DurationDayTimeImpl(String lexicalRepresentation) {
 105         super(lexicalRepresentation);
 106 
 107         if (getYears() > 0 || getMonths() > 0) {
 108             throw new IllegalArgumentException(
 109                     "Trying to create an xdt:dayTimeDuration with an invalid"
 110                     + " lexical representation of \"" + lexicalRepresentation
 111                     + "\", data model requires a format PnDTnHnMnS.");
 112         }
 113 
 114         convertToCanonicalDayTime();
 115     }
 116         /**
 117          * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified milliseconds as defined in
 118          * <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
 119          *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
 120          *
 121          * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
 122          * whose lexical representation contains only day, hour, minute, and second components.
 123          * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
 124          *
 125      * <p>All four values are set by computing their values from the specified milliseconds
 126      * and are availabe using the <code>get</code> methods of  the created {@link Duration}.
 127      * The values conform to and are defined by:</p>
 128      * <ul>
 129      *   <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
 130      *   <li><a href="http://www.w3.org/TR/xmlschema-2/#isoformats">
 131      *     W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
 132      *   </li>
 133      *   <li>{@link XMLGregorianCalendar}  Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
 134      * </ul>
 135          *
 136          * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
 137          * {@link java.util.Calendar#YEAR} = 1970,
 138          * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
 139          * {@link java.util.Calendar#DATE} = 1, etc.
 140          * This is important as there are variations in the Gregorian Calendar,
 141          * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
 142          * so the result of {@link Duration#getDays()} can be influenced.</p>
 143          *
 144      * <p>Any remaining milliseconds after determining the day, hour, minute and second are discarded.</p>
 145      *
 146      * @param durationInMilliseconds Milliseconds of <code>Duration</code> to create.
 147      *
 148      * @return New <code>Duration</code> created with the specified <code>durationInMilliseconds</code>.
 149      *
 150      * @see <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
 151      *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>
 152      */
 153     protected DurationDayTimeImpl(final long durationInMilliseconds) {
 154             super(durationInMilliseconds);
 155             convertToCanonicalDayTime();
 156             // only day, hour, minute, and second should have values
 157             years = null;
 158             months = null;
 159     }
 160 
 161 
 162     /**
 163      * The value space of xs:dayTimeDuration is the set of fractional second values.
 164      * @return fractional second values
 165      */
 166     public float getValue() {
 167         float sec = (seconds==null)?0:seconds.floatValue();
 168         return (((((getDays() * 24) +
 169                     getHours()) * 60) +
 170                     getMinutes())*60) +
 171                     sec;
 172     }
 173 
 174     private void convertToCanonicalDayTime() {
 175 
 176         while (getSeconds() >= 60)
 177         {
 178             seconds = seconds.subtract(BigDecimal.valueOf(60));
 179             minutes = BigInteger.valueOf((long) getMinutes()).add(BigInteger.ONE);
 180         }
 181 
 182         while (getMinutes() >= 60)
 183         {
 184             minutes = minutes.subtract(BigInteger.valueOf(60));
 185             hours = BigInteger.valueOf((long) getHours()).add(BigInteger.ONE);
 186         }
 187 
 188         while (getHours() >= 24)
 189         {
 190             hours = hours.subtract(BigInteger.valueOf(24));
 191             days = BigInteger.valueOf((long) getDays()).add(BigInteger.ONE);
 192         }
 193     }
 194 
 195 }