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