/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * Copyright (c) 2009 by Oracle Corporation. All Rights Reserved. */ package com.sun.org.apache.xerces.internal.jaxp.datatype; import java.math.BigInteger; import java.math.BigDecimal; import javax.xml.datatype.DatatypeConstants; /** *

Represent a subtype xdt:dayTimeDuration of a Duration * as specified in * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration.

* * *

The DurationYearMonth object represents a period of Gregorian time, * with a lexical representation, "PnDTnHnMnS" that contains only year and month components. *

* * * @author Vikram Aroskar * @author Joe Wang * @see XMLGregorianCalendar#add(Duration) */ class DurationDayTimeImpl extends DurationImpl { public DurationDayTimeImpl( boolean isPositive, BigInteger days, BigInteger hours, BigInteger minutes, BigDecimal seconds) { super(isPositive, null, null, days, hours, minutes, seconds); convertToCanonicalDayTime(); } public DurationDayTimeImpl( boolean isPositive, int days, int hours, int minutes, int seconds) { this( isPositive, wrap(days), wrap(hours), wrap(minutes), (seconds != DatatypeConstants.FIELD_UNDEFINED ? new BigDecimal(String.valueOf(seconds)) : null)); } /** *

Construct a Duration of type xdt:dayTimeDuration by parsing its String representation, * "PnDTnHnMnS", * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration.

* *

The datatype xdt:dayTimeDuration is a subtype of xs:duration * whose lexical representation contains only day, hour, minute, and second components. * This datatype resides in the namespace http://www.w3.org/2003/11/xpath-datatypes.

* *

All four values are set and availabe from the created {@link Duration}

* *

The XML Schema specification states that values can be of an arbitrary size. * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values. * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits * if implementation capacities are exceeded.

* * @param lexicalRepresentation Lexical representation of a duration. * * @throws IllegalArgumentException If lexicalRepresentation is not a valid representation of a Duration expressed only in terms of days and time. * @throws UnsupportedOperationException If implementation cannot support requested values. * @throws NullPointerException If lexicalRepresentation is null. */ protected DurationDayTimeImpl(String lexicalRepresentation) { super(lexicalRepresentation); if (getYears() > 0 || getMonths() > 0) { throw new IllegalArgumentException( "Trying to create an xdt:dayTimeDuration with an invalid" + " lexical representation of \"" + lexicalRepresentation + "\", data model requires a format PnDTnHnMnS."); } convertToCanonicalDayTime(); } /** *

Create a Duration of type xdt:dayTimeDuration using the specified milliseconds as defined in * * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration.

* *

The datatype xdt:dayTimeDuration is a subtype of xs:duration * whose lexical representation contains only day, hour, minute, and second components. * This datatype resides in the namespace http://www.w3.org/2003/11/xpath-datatypes.

* *

All four values are set by computing their values from the specified milliseconds * and are availabe using the get methods of the created {@link Duration}. * The values conform to and are defined by:

* * *

The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e., * {@link java.util.Calendar#YEAR} = 1970, * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY}, * {@link java.util.Calendar#DATE} = 1, etc. * This is important as there are variations in the Gregorian Calendar, * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY} * so the result of {@link Duration#getDays()} can be influenced.

* *

Any remaining milliseconds after determining the day, hour, minute and second are discarded.

* * @param durationInMilliseconds Milliseconds of Duration to create. * * @return New Duration created with the specified durationInMilliseconds. * * @see * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration */ protected DurationDayTimeImpl(final long durationInMilliseconds) { super(durationInMilliseconds); convertToCanonicalDayTime(); // only day, hour, minute, and second should have values years = null; months = null; } /** * The value space of xs:dayTimeDuration is the set of fractional second values. * @return fractional second values */ public float getValue() { float sec = (seconds==null)?0:seconds.floatValue(); return (((((getDays() * 24) + getHours()) * 60) + getMinutes())*60) + sec; } private void convertToCanonicalDayTime() { while (getSeconds() >= 60) { seconds = seconds.subtract(BigDecimal.valueOf(60)); minutes = BigInteger.valueOf((long) getMinutes()).add(BigInteger.ONE); } while (getMinutes() >= 60) { minutes = minutes.subtract(BigInteger.valueOf(60)); hours = BigInteger.valueOf((long) getHours()).add(BigInteger.ONE); } while (getHours() >= 24) { hours = hours.subtract(BigInteger.valueOf(24)); days = BigInteger.valueOf((long) getDays()).add(BigInteger.ONE); } } }