com.sun.org.apache.xerces.internal.jaxp.datatype.DurationYearMonthImpl Maven / Gradle / Ivy
Show all versions of jaxp-ri Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.org.apache.xerces.internal.jaxp.datatype;
import java.math.BigInteger;
import javax.xml.datatype.DatatypeConstants;
/**
* Represent a subtype xdt:yearMonthDuration
of a Duration
* as specified in
* XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration.
*
*
* The DurationYearMonth object represents a period of Gregorian time,
* with a lexical representation, "PnYnM" that contains only year and month components.
*
*
*
* @author Vikram Aroskar
* @author Joe Wang
* @version $Revision: 1.2 $, $Date: 2010-11-01 04:40:07 $
* @see XMLGregorianCalendar#add(Duration)
*/
class DurationYearMonthImpl
extends DurationImpl {
/**
* Constructs a new Duration object by specifying each field individually.
*
* All the parameters are optional as long as at least one field is present.
* If specified, parameters have to be zero or positive.
*
* @param isPositive Set to false
to create a negative duration. When the length
* of the duration is zero, this parameter will be ignored.
* @param years of this Duration
* @param months of this Duration
*
* @throws IllegalArgumentException
* If years, months parameters are all null
. Or if any
* of those parameters are negative.
*/
public DurationYearMonthImpl(
boolean isPositive,
BigInteger years,
BigInteger months) {
super(isPositive, years, months, null, null, null, null);
convertToCanonicalYearMonth();
}
/**
* Construct a Duration
of type xdt:yearMonthDuration
using the specified
* year
and month
as defined in
*
* XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration.
*
* A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.
*
* @param isPositive Set to false
to create a negative duration. When the length
* of the duration is zero, this parameter will be ignored.
* @param year Year of Duration
.
* @param month Month of Duration
.
*
* @throws IllegalArgumentException If the values are not a valid representation of a
* Duration
: if any of the fields (year, month) is negative.
*/
protected DurationYearMonthImpl(
final boolean isPositive,
final int years,
final int months) {
this(isPositive,
wrap(years),
wrap(months));
}
/**
* Construct a Duration
of type xdt:yearMonthDuration
using the specified milliseconds as defined in
*
* XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration.
*
* The datatype xdt:yearMonthDuration
is a subtype of xs:duration
* whose lexical representation contains only year and month components.
* This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.
*
* Both 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:
*
* - ISO 8601:2000(E) Section 5.5.3.2 Alternative format
* -
* W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats
*
* - {@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation
*
*
* 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#getMonths()} can be influenced.
*
* Any remaining milliseconds after determining the year and month are discarded.
*
* @param durationInMilliseconds Milliseconds of Duration
to create.
*/
protected DurationYearMonthImpl(long durationInMilliseconds) {
super(durationInMilliseconds);
convertToCanonicalYearMonth();
//Any remaining milliseconds after determining the year and month are discarded.
days = null;
hours = null;
minutes = null;
seconds = null;
signum = calcSignum((signum<0)?false:true);
}
/**
* Construct a Duration
of type xdt:yearMonthDuration
by parsing its String
representation,
* "PnYnM",
* XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration.
*
* The datatype xdt:yearMonthDuration
is a subtype of xs:duration
* whose lexical representation contains only year and month components.
* This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.
*
* Both 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 years and months.
* @throws UnsupportedOperationException If implementation cannot support requested values.
* @throws NullPointerException If lexicalRepresentation
is null
.
*/
protected DurationYearMonthImpl(String lexicalRepresentation) {
super(lexicalRepresentation);
if (getDays() > 0 || getHours() > 0
|| getMinutes() > 0 || getSeconds() > 0) {
throw new IllegalArgumentException(
"Trying to create an xdt:yearMonthDuration with an invalid"
+ " lexical representation of \"" + lexicalRepresentation
+ "\", data model requires PnYnM.");
}
convertToCanonicalYearMonth();
}
/**
* The value space of xs:yearMonthDuration is the set of xs:integer month values.
* @return the value of yearMonthDuration
*/
public int getValue() {
return getYears() * 12 + getMonths();
}
private void convertToCanonicalYearMonth() {
while (getMonths() >= 12)
{
months = months.subtract(BigInteger.valueOf(12));
years = BigInteger.valueOf((long) getYears()).add(BigInteger.ONE);
}
}
}