1 /*
   2  * Copyright (c) 2009, 2017 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 
  29 import java.math.BigInteger;
  30 import java.math.BigDecimal;
  31 import javax.xml.datatype.DatatypeConstants;
  32 
  33 /**
  34  * <p>Represent a subtype <code>xdt:dayTimeDuration</code> of a <code>Duration</code>
  35  * as specified in <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
  36  *   XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
  37  *
  38  *
  39  * <p>The DurationYearMonth object represents a period of Gregorian time,
  40  * with a lexical representation, "<em>PnDTnHnMnS</em>" that contains only year and month components.
  41  * </p>
  42  *
  43  *
  44  * @author Vikram Aroskar


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