Top Description Fields Constructors Methods
org.apache.xerces.jaxp.datatype

pack-priv Class DurationImpl

extends Duration
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.IOException, .Serializable, java.math.BigDecimal, .BigInteger, java.util.Calendar, .Date, .GregorianCalendar, javax.xml.datatype.DatatypeConstants, .Duration, .XMLGregorianCalendar, org.apache.xerces.util.DatatypeMessageFormatter

Immutable representation of a time span as defined in the W3C XML Schema 1.0 specification.

A Duration object represents a period of Gregorian time, which consists of six fields (years, months, days, hours, minutes, and seconds) plus a sign (+/-) field.

The first five fields have non-negative (>=0) integers or null (which represents that the field is not set), and the seconds field has a non-negative decimal or null. A negative sign indicates a negative duration.

This class provides a number of methods that make it easy to use for the duration datatype of XML Schema 1.0 with the errata.

Order relationship

Duration objects only have partial order, where two values A and B maybe either:

  1. A<B (A is shorter than B)
  2. A>B (A is longer than B)
  3. A==B (A and B are of the same duration)
  4. A<>B (Comparison between A and B is indeterminate)

For example, 30 days cannot be meaningfully compared to one month. The compare(Duration) method implements this relationship.

See the isLongerThan(Duration) method for details about the order relationship among Duration objects.

Operations over Duration

This class provides a set of basic arithmetic operations, such as addition, subtraction and multiplication. Because durations don't have total order, an operation could fail for some combinations of operations. For example, you cannot subtract 15 days from 1 month. See the javadoc of those methods for detailed conditions where this could happen.

Also, division of a duration by a number is not provided because the Duration class can only deal with finite precision decimal numbers. For example, one cannot represent 1 sec divided by 3.

However, you could substitute a division by 3 with multiplying by numbers such as 0.3 or 0.333.

Range of allowed values

Because some operations of Duration rely on Calendar even though Duration can hold very large or very small values, some of the methods may not work correctly on such Durations. The impacted methods document their dependency on Calendar.

Authors
Kohsuke Kawaguchi, Joseph Fialli
Version
$Id: DurationImpl.java 1747631 2016-06-10 04:44:24Z mukulg $
See Also
XMLGregorianCalendar#add(Duration)

Field Summary

Modifier and TypeField and Description
private final BigInteger
days

Days of this Duration.

private static final BigDecimal[]
FACTORS

1 unit of FIELDS[i] is equivalent to FACTORS[i] unit of FIELDS[i+1].

private static final DatatypeConstants.Field[]
FIELDS

Internal array of value Fields.

private final BigInteger
hours

Hours of this Duration.

private final BigInteger
minutes

Minutes of this Duration.

private final BigInteger
months

Months of this Duration.

private final BigDecimal
seconds

Seconds of this Duration.

private static final long
serialVersionUID

Stream Unique Identifier.

private final int
signum

Indicates the sign.

private static final XMLGregorianCalendar[]
TEST_POINTS

Four constants defined for the comparison of durations.

private final BigInteger
years

Years of this Duration.

private static final BigDecimal
ZERO

BigDecimal value of 0.

Constructor Summary

AccessConstructor and Description
protected
DurationImpl(boolean
Set to false to create a negative duration. When the length of the duration is zero, this parameter will be ignored.
isPositive
,
BigInteger
of this Duration
years
,
BigInteger
of this Duration
months
,
BigInteger
of this Duration
days
,
BigInteger
of this Duration
hours
,
BigInteger
of this Duration
minutes
,
BigDecimal
of this Duration
seconds
)

Constructs a new Duration object by specifying each field individually.

protected
DurationImpl(final boolean isPositive, final int years, final int months, final int days, final int hours, final int minutes, final int seconds)

Constructs a new Duration object by specifying each field individually.

protected
DurationImpl(final long
The length of the duration in milliseconds.
durationInMilliSeconds
)

Constructs a new Duration object by specifying the duration in milliseconds.

protected
DurationImpl(String
Lexical representation of a duration.
lexicalRepresentation
)

Constructs a new Duration object by parsing its string representation "PnYnMnDTnHnMnS" as defined in XML Schema 1.0 section 3.2.6.1.

Method Summary

Modifier and TypeMethod and Description
public Duration

Returns:

non-null valid Duration object.
add
(final Duration
Duration to add to this Duration
rhs
)

Implements abstract javax.xml.datatype.Duration.add.

Computes a new duration whose value is this+rhs.

public void
addTo(Calendar
A calendar object whose value will be modified.
calendar
)

Implements abstract javax.xml.datatype.Duration.addTo.

Adds this duration to a Calendar object.
public void
addTo(Date
A date object whose value will be modified.
date
)

Overrides javax.xml.datatype.Duration.addTo.

Adds this duration to a Date object.
private static void
alignSigns(BigDecimal[] buf, int start, int end)

private int

Returns:

1 if positive, else -1.
calcSignum
(boolean
Sign.
isPositive
)

TODO: Javadoc

public int

Returns:

the relationship between this Durationand duration parameter as DatatypeConstants#LESSER, DatatypeConstants#EQUAL, DatatypeConstants#GREATER or DatatypeConstants#INDETERMINATE.
compare
(Duration
to compare
rhs
)

Implements abstract javax.xml.datatype.Duration.compare.

Partial order relation comparison with this Duration instance.

private int

Returns:

INDETERMINATE if the order relationship between date1 and date2 is indeterminate. EQUAL if the order relation between date1 and date2 is EQUAL. If the strict parameter is true, return LESS_THAN if date1 is less than date2 and return GREATER_THAN if date1 is greater than date2. If the strict parameter is false, return LESS_THAN if date1 is less than OR equal to date2 and return GREATER_THAN if date1 is greater than OR equal to date2
compareDates
(Duration
Unnormalized duration
duration1
,
Duration
Unnormalized duration
duration2
)

Compares 2 given durations.

private int
compareResults(int resultA, int resultB)

private static long
public int

Returns:

Days of this Duration.
getDays
()

Overrides javax.xml.datatype.Duration.getDays.

Obtains the value of the DAYS field as an integer value, or 0 if not present.
public Number

Returns:

If the specified field is present, this method returns a non-null non-negative Number object that represents its value. If it is not present, return null. For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method returns a BigInteger object. For SECONDS, this method returns a BigDecimal.
getField
(DatatypeConstants.Field
one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, MINUTES, or SECONDS.)
field
)

Implements abstract javax.xml.datatype.Duration.getField.

Gets the value of a field.
private BigDecimal

Returns:

non-null valid BigDecimal.
getFieldAsBigDecimal
(DatatypeConstants.Field
Field to get value for.
f
)

Gets the value of the field as a BigDecimal.

public int

Returns:

Hours of this Duration.
getHours
()

Overrides javax.xml.datatype.Duration.getHours.

Obtains the value of the HOURS field as an integer value, or 0 if not present.
private int

Returns:

int value of field or 0 if field is not set.
getInt
(DatatypeConstants.Field
To get value for.
field
)

Return the requested field value as an int.

public int

Returns:

Minutes of this Duration.
getMinutes
()

Overrides javax.xml.datatype.Duration.getMinutes.

Obtains the value of the MINUTES field as an integer value, or 0 if not present.
public int

Returns:

Months of this Duration.
getMonths
()

Overrides javax.xml.datatype.Duration.getMonths.

Obtains the value of the MONTHS field as an integer value, or 0 if not present.
public int

Returns:

seconds in the integer value. The fraction of seconds will be discarded (for example, if the actual value is 2.5, this method returns 2)
getSeconds
()

Overrides javax.xml.datatype.Duration.getSeconds.

Obtains the value of the SECONDS field as an integer value, or 0 if not present.
public int

Returns:

-1 if this duration is negative, 0 if the duration is zero, and 1 if the duration is postive.
getSign
()

Implements abstract javax.xml.datatype.Duration.getSign.

Returns the sign of this duration in -1,0, or 1.
public long

Returns:

milliseconds between startInstant and startInstant plus this Duration
getTimeInMillis
(final Calendar
The length of a month/year varies. The startInstant is used to disambiguate this variance. Specifically, this method returns the difference between startInstant and startInstant+duration
startInstant
)

Overrides javax.xml.datatype.Duration.getTimeInMillis.

Returns the length of the duration in milli-seconds.

public long

Returns:

milliseconds between startInstant and startInstant plus this Duration
getTimeInMillis
(final Date
The length of a month/year varies. The startInstant is used to disambiguate this variance. Specifically, this method returns the difference between startInstant and startInstant+duration.
startInstant
)

Overrides javax.xml.datatype.Duration.getTimeInMillis.

Returns the length of the duration in milli-seconds.

public int

Returns:

If the YEARS field is present, return its value as an integer by using the Number#intValue() method. If the YEARS field is not present, return 0.
getYears
()

Overrides javax.xml.datatype.Duration.getYears.

Obtains the value of the YEARS field as an integer value, or 0 if not present.
public int
hashCode()

Implements abstract javax.xml.datatype.Duration.hashCode.

Returns a hash code consistent with the definition of the equals method.
private static boolean

Returns:

true if ch is a digit, else false.
isDigit
(char
char to test.
ch
)

TODO: Javadoc

private static boolean

Returns:

true if ch is a digit or a period, else false.
isDigitOrPeriod
(char
to test.
ch
)

TODO: Javadoc

public boolean

Returns:

true if the field is present. false if not.
isSet
(DatatypeConstants.Field
one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, MINUTES, or SECONDS.)
field
)

Implements abstract javax.xml.datatype.Duration.isSet.

Checks if a field is set.
public Duration

Returns:

New Duration that is factortimes longer than this Duration.
multiply
(int
Factor times longer of new Duration to create.
factor
)

Overrides javax.xml.datatype.Duration.multiply.

Computes a new duration whose value is factor times longer than the value of this duration.

public Duration

Returns:

returns a non-null valid Duration object
multiply
(BigDecimal
to multiply by
factor
)

Implements abstract javax.xml.datatype.Duration.multiply.

Computes a new duration whose value is factor times longer than the value of this duration.
public Duration

Returns:

always return a non-null valid Duration object.
negate
()

Implements abstract javax.xml.datatype.Duration.negate.

Returns a new Duration object whose value is -this.
public Duration

Returns:

Duration of years and months of this Duration as days.
normalizeWith
(Calendar
Calendar reference point.
startTimeInstant
)

Implements abstract javax.xml.datatype.Duration.normalizeWith.

Converts the years and months fields into the days field by using a specific time instant as the reference point.

private static void
organizeParts(String
TODO: ???
whole
,
String[]
TODO: ???
parts
,
int[]
TODO: ???
partsIndex
,
int
TODO: ???
len
,
String
TODO: ???
tokens
)

TODO: Javadoc.

private static BigDecimal

Returns:

TODO: ???.
parseBigDecimal
(String
TODO: ???.
whole
,
String
TODO: ???.
part
,
int
TODO: ???.
index
)

TODO: Javadoc.

private static BigInteger

Returns:

TODO: ???.
parseBigInteger
(String
TODO: ???.
whole
,
String
TODO: ???.
part
,
int
TODO: ???.
index
)

TODO: Javadoc

private static String

Returns:

Result of parsing.
parsePiece
(String
String to parse.
whole
,
int[]
TODO: ???
idx
)

TODO: Javadoc

private static BigDecimal

Returns:

non-null BigDecimal.
sanitize
(BigInteger
Value to sanitize.
value
,
int
0 to sanitize to 0, > 0 to sanitize to value, < 0 to sanitize to negative value.
signum
)

Compute value*signum where value==null is treated as value==0.

pack-priv static BigDecimal

Returns:

non-null BigDecimal.
sanitize
(BigDecimal
Value to sanitize.
value
,
int
0 to sanitize to 0, > 0 to sanitize to value, < 0 to sanitize to negative value.
signum
)

Compute value*signum where value==null is treated as value==0.

public int

Returns:

-1 if this duration is negative, 0 if the duration is zero, and 1 if the duration is postive.
signum
()

Returns the sign of this duration in -1,0, or 1.

public Duration

Returns:

New Duration created from subtracting rhs from this Duration.
subtract
(final Duration
Duration to substract from this Duration.
rhs
)

Overrides javax.xml.datatype.Duration.subtract.

Computes a new duration whose value is this-rhs.

private static void
testNonNegative(BigInteger
Number to test.
n
,
DatatypeConstants.Field
Field to test.
f
)

Makes sure that the given number is non-negative.

private static void
testNonNegative(BigDecimal
Number to test.
n
,
DatatypeConstants.Field
Field to test.
f
)

Makes sure that the given number is non-negative.

private static BigInteger

Returns:

BigInteger value of BigDecimal, possibly null.
toBigInteger
(BigDecimal
Value to convert.
value
,
boolean
Can returned value be null?
canBeNull
)

BigInteger value of BigDecimal value.

public String

Returns:

Always return a non-null valid String object.
toString
()

Overrides javax.xml.datatype.Duration.toString.

Returns a string representation of this duration object.
private String

Returns:

String representation of BigDecimal
toString
(BigDecimal
BigDecimal to format as a String
bd
)

Hides javax.xml.datatype.Duration.toString.

Turns BigDecimal to a string representation.

private static BigInteger

Returns:

BigInteger representation of int.
wrap
(final int
int to convert to BigInteger.
i
)

TODO: Javadoc

private Object

Returns:

An object that encapsulates the string returned by this.toString().
writeReplace
()

Writes Duration as a lexical representation for maximum future compatibility.

Inherited from javax.xml.datatype.Duration:
equalsgetXMLSchemaTypeisLongerThanisShorterThan