DurationImplpublic class DurationImpl extends Duration implements SerializableImmutable 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:
- A<B (A is shorter than B)
- A>B (A is longer than B)
- A==B (A and B are of the same duration)
- A<>B (Comparison between A and B is indeterminate)
For example, 30 days cannot be meaningfully compared to one month.
The {@link #compare(Duration)} method implements this
relationship.
See the {@link #isLongerThan(Duration)} method for details about
the order relationship among {@link 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 {@link 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 {@link Duration} rely on {@link Calendar}
even though {@link Duration} can hold very large or very small values,
some of the methods may not work correctly on such {@link Duration}s.
The impacted methods document their dependency on {@link Calendar}. |
Fields Summary |
---|
private static final int | FIELD_NUM Number of Fields. | private static final DatatypeConstants$Field[] | FIELDS Internal array of value Fields. | private static final int[] | FIELD_IDS Internal array of value Field ids. | private static final BigDecimal | ZERO BigDecimal value of 0. | private final int | signum Indicates the sign. -1, 0 or 1 if the duration is negative,
zero, or positive. | private final BigInteger | years Years of this Duration . | private final BigInteger | months Months of this Duration . | private final BigInteger | days Days of this Duration . | private final BigInteger | hours Hours of this Duration . | private final BigInteger | minutes Minutes of this Duration . | private final BigDecimal | seconds Seconds of this Duration . | private static final XMLGregorianCalendar[] | TEST_POINTS Four constants defined for the comparison of durations. | private static final BigDecimal[] | FACTORS1 unit of FIELDS[i] is equivalent to FACTORS[i] unit of
FIELDS[i+1]. | private static final long | serialVersionUID Stream Unique Identifier.
TODO: Serialization should use the XML string representation as
the serialization format to ensure future compatibility. |
Constructors Summary |
---|
protected DurationImpl(boolean isPositive, BigInteger years, BigInteger months, BigInteger days, BigInteger hours, BigInteger minutes, BigDecimal seconds)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.
this.years = years;
this.months = months;
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.signum = calcSignum(isPositive);
// sanity check
if (years == null
&& months == null
&& days == null
&& hours == null
&& minutes == null
&& seconds == null) {
throw new IllegalArgumentException(
//"all the fields are null"
DatatypeMessageFormatter.formatMessage(null, "AllFieldsNull", null)
);
}
testNonNegative(years, DatatypeConstants.YEARS);
testNonNegative(months, DatatypeConstants.MONTHS);
testNonNegative(days, DatatypeConstants.DAYS);
testNonNegative(hours, DatatypeConstants.HOURS);
testNonNegative(minutes, DatatypeConstants.MINUTES);
testNonNegative(seconds, DatatypeConstants.SECONDS);
| protected DurationImpl(boolean isPositive, int years, int months, int days, int hours, int minutes, int seconds)Constructs a new Duration object by specifying each field
individually.
This method is functionally equivalent to
invoking another constructor by wrapping
all non-zero parameters into {@link BigInteger} and {@link BigDecimal}.
Zero value of int parameter is equivalent of null value of
the corresponding field.
this(
isPositive,
wrap(years),
wrap(months),
wrap(days),
wrap(hours),
wrap(minutes),
seconds != 0 ? new BigDecimal(String.valueOf(seconds)) : null);
| protected DurationImpl(long durationInMilliSeconds)Constructs a new Duration object by specifying the duration
in milliseconds.
The DAYS, HOURS, MINUTES and SECONDS fields are used to
represent the specifed duration in a reasonable way.
That is, the constructed object x satisfies
the following conditions:
- x.getHours()<24
- x.getMinutes()<60
- x.getSeconds()<60
boolean is0x8000000000000000L = false;
long l = durationInMilliSeconds;
if (l > 0) {
signum = 1;
} else if (l < 0) {
signum = -1;
if (l == 0x8000000000000000L) {
// negating 0x8000000000000000L causes an overflow
l++;
is0x8000000000000000L = true;
}
l *= -1;
} else {
signum = 0;
}
this.years = null;
this.months = null;
this.seconds =
BigDecimal.valueOf((l % 60000L) + (is0x8000000000000000L ? 1 : 0), 3);
l /= 60000L;
this.minutes = (l == 0) ? null : BigInteger.valueOf(l % 60L);
l /= 60L;
this.hours = (l == 0) ? null : BigInteger.valueOf(l % 24L);
l /= 24L;
this.days = (l == 0) ? null : BigInteger.valueOf(l);
| protected DurationImpl(String lexicalRepresentation)Constructs a new Duration object by
parsing its string representation
"PnYnMnDTnHnMnS" as defined in XML Schema 1.0 section 3.2.6.1.
The string representation may not have any leading
and trailing whitespaces.
For example, this method parses strings like
"P1D" (1 day), "-PT100S" (-100 sec.), "P1DT12H" (1 days and 12 hours).
The parsing is done field by field so that
the following holds for any lexically correct string x:
new Duration(x).toString().equals(x)
Returns a non-null valid duration object that holds the value
indicated by the lexicalRepresentation parameter.
// only if I could use the JDK1.4 regular expression ....
final String s = lexicalRepresentation;
boolean positive;
int[] idx = new int[1];
int length = s.length();
boolean timeRequired = false;
if (lexicalRepresentation == null) {
throw new NullPointerException();
}
idx[0] = 0;
if (length != idx[0] && s.charAt(idx[0]) == '-") {
idx[0]++;
positive = false;
} else {
positive = true;
}
if (length != idx[0] && s.charAt(idx[0]++) != 'P") {
throw new IllegalArgumentException(s); //,idx[0]-1);
}
// phase 1: chop the string into chunks
// (where a chunk is '<number><a symbol>'
//--------------------------------------
int dateLen = 0;
String[] dateParts = new String[3];
int[] datePartsIndex = new int[3];
while (length != idx[0]
&& isDigit(s.charAt(idx[0]))
&& dateLen < 3) {
datePartsIndex[dateLen] = idx[0];
dateParts[dateLen++] = parsePiece(s, idx);
}
if (length != idx[0]) {
if (s.charAt(idx[0]++) == 'T") {
timeRequired = true;
} else {
throw new IllegalArgumentException(s); // ,idx[0]-1);
}
}
int timeLen = 0;
String[] timeParts = new String[3];
int[] timePartsIndex = new int[3];
while (length != idx[0]
&& isDigitOrPeriod(s.charAt(idx[0]))
&& timeLen < 3) {
timePartsIndex[timeLen] = idx[0];
timeParts[timeLen++] = parsePiece(s, idx);
}
if (timeRequired && timeLen == 0) {
throw new IllegalArgumentException(s); // ,idx[0]);
}
if (length != idx[0]) {
throw new IllegalArgumentException(s); // ,idx[0]);
}
if (dateLen == 0 && timeLen == 0) {
throw new IllegalArgumentException(s); // ,idx[0]);
}
// phase 2: check the ordering of chunks
//--------------------------------------
organizeParts(s, dateParts, datePartsIndex, dateLen, "YMD");
organizeParts(s, timeParts, timePartsIndex, timeLen, "HMS");
// parse into numbers
years = parseBigInteger(s, dateParts[0], datePartsIndex[0]);
months = parseBigInteger(s, dateParts[1], datePartsIndex[1]);
days = parseBigInteger(s, dateParts[2], datePartsIndex[2]);
hours = parseBigInteger(s, timeParts[0], timePartsIndex[0]);
minutes = parseBigInteger(s, timeParts[1], timePartsIndex[1]);
seconds = parseBigDecimal(s, timeParts[2], timePartsIndex[2]);
signum = calcSignum(positive);
|
Methods Summary |
---|
public javax.xml.datatype.Duration | add(javax.xml.datatype.Duration rhs)Computes a new duration whose value is this+rhs .
For example,
"1 day" + "-3 days" = "-2 days"
"1 year" + "1 day" = "1 year and 1 day"
"-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
"15 hours" + "-3 days" = "-(2 days,9 hours)"
"1 year" + "-1 day" = IllegalStateException
Since there's no way to meaningfully subtract 1 day from 1 month,
there are cases where the operation fails in
{@link IllegalStateException}.
Formally, the computation is defined as follows.
Firstly, we can assume that two {@link Duration}s to be added
are both positive without losing generality (i.e.,
(-X)+Y=Y-X , X+(-Y)=X-Y ,
(-X)+(-Y)=-(X+Y) )
Addition of two positive {@link Duration}s are simply defined as
field by field addition where missing fields are treated as 0.
A field of the resulting {@link Duration} will be unset if and
only if respective fields of two input {@link Duration}s are unset.
Note that lhs.add(rhs) will be always successful if
lhs.signum()*rhs.signum()!=-1 or both of them are
normalized.
Duration lhs = this;
BigDecimal[] buf = new BigDecimal[6];
buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS), rhs.getSign()));
buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS), rhs.getSign()));
buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS), rhs.getSign()));
buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));
// align sign
alignSigns(buf, 0, 2); // Y,M
alignSigns(buf, 2, 6); // D,h,m,s
// make sure that the sign bit is consistent across all 6 fields.
int s = 0;
for (int i = 0; i < 6; i++) {
if (s * buf[i].signum() < 0) {
throw new IllegalStateException();
}
if (s == 0) {
s = buf[i].signum();
}
}
return new DurationImpl(
s >= 0,
toBigInteger(sanitize(buf[0], s),
lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
toBigInteger(sanitize(buf[1], s),
lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
toBigInteger(sanitize(buf[2], s),
lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
toBigInteger(sanitize(buf[3], s),
lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
toBigInteger(sanitize(buf[4], s),
lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
(buf[5].signum() == 0
&& lhs.getField(DatatypeConstants.SECONDS) == null
&& rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
| public void | addTo(java.util.Calendar calendar)Adds this duration to a {@link Calendar} object.
Calls {@link java.util.Calendar#add(int,int)} in the
order of YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, and MILLISECONDS
if those fields are present. Because the {@link Calendar} class
uses int to hold values, there are cases where this method
won't work correctly (for example if values of fields
exceed the range of int.)
Also, since this duration class is a Gregorian duration, this
method will not work correctly if the given {@link Calendar}
object is based on some other calendar systems.
Any fractional parts of this {@link Duration} object
beyond milliseconds will be simply ignored. For example, if
this duration is "P1.23456S", then 1 is added to SECONDS,
234 is added to MILLISECONDS, and the rest will be unused.
Note that because {@link Calendar#add(int, int)} is using
int, {@link Duration} with values beyond the
range of int in its fields
will cause overflow/underflow to the given {@link Calendar}.
{@link XMLGregorianCalendar#add(Duration)} provides the same
basic operation as this method while avoiding
the overflow/underflow issues.
calendar.add(Calendar.YEAR, getYears() * signum);
calendar.add(Calendar.MONTH, getMonths() * signum);
calendar.add(Calendar.DAY_OF_MONTH, getDays() * signum);
calendar.add(Calendar.HOUR, getHours() * signum);
calendar.add(Calendar.MINUTE, getMinutes() * signum);
calendar.add(Calendar.SECOND, getSeconds() * signum);
if (seconds != null) {
BigDecimal fraction =
seconds.subtract(seconds.setScale(0, BigDecimal.ROUND_DOWN));
int millisec = fraction.movePointRight(3).intValue();
calendar.add(Calendar.MILLISECOND, millisec * signum);
}
| public void | addTo(java.util.Date date)Adds this duration to a {@link Date} object.
The given date is first converted into
a {@link java.util.GregorianCalendar}, then the duration
is added exactly like the {@link #addTo(Calendar)} method.
The updated time instant is then converted back into a
{@link Date} object and used to update the given {@link Date} object.
This somewhat redundant computation is necessary to unambiguously
determine the duration of months and years.
Calendar cal = new GregorianCalendar();
cal.setTime(date); // this will throw NPE if date==null
this.addTo(cal);
date.setTime(getCalendarTimeInMillis(cal));
| private static void | alignSigns(java.math.BigDecimal[] buf, int start, int end)
// align sign
boolean touched;
do { // repeat until all the sign bits become consistent
touched = false;
int s = 0; // sign of the left fields
for (int i = start; i < end; i++) {
if (s * buf[i].signum() < 0) {
// this field has different sign than its left field.
touched = true;
// compute the number of unit that needs to be borrowed.
BigDecimal borrow =
buf[i].abs().divide(
FACTORS[i - 1],
BigDecimal.ROUND_UP);
if (buf[i].signum() > 0) {
borrow = borrow.negate();
}
// update values
buf[i - 1] = buf[i - 1].subtract(borrow);
buf[i] = buf[i].add(borrow.multiply(FACTORS[i - 1]));
}
if (buf[i].signum() != 0) {
s = buf[i].signum();
}
}
} while (touched);
| private int | calcSignum(boolean isPositive)TODO: Javadoc
if ((years == null || years.signum() == 0)
&& (months == null || months.signum() == 0)
&& (days == null || days.signum() == 0)
&& (hours == null || hours.signum() == 0)
&& (minutes == null || minutes.signum() == 0)
&& (seconds == null || seconds.signum() == 0)) {
return 0;
}
if (isPositive) {
return 1;
} else {
return -1;
}
| public int | compare(javax.xml.datatype.Duration rhs)Partial order relation comparison with this Duration instance.
Comparison result must be in accordance with
W3C XML Schema 1.0 Part 2, Section 3.2.7.6.2,
Order relation on duration.
Return:
- {@link DatatypeConstants#LESSER} if this
Duration is shorter than duration parameter
- {@link DatatypeConstants#EQUAL} if this
Duration is equal to duration parameter
- {@link DatatypeConstants#GREATER} if this
Duration is longer than duration parameter
- {@link DatatypeConstants#INDETERMINATE} if a conclusive partial order relation cannot be determined
BigInteger maxintAsBigInteger = BigInteger.valueOf((long) Integer.MAX_VALUE);
BigInteger minintAsBigInteger = BigInteger.valueOf((long) Integer.MIN_VALUE);
// check for fields that are too large in this Duration
if (years != null && years.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.YEARS.toString(), years.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " years too large to be supported by this implementation "
//+ years.toString()
);
}
if (months != null && months.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.MONTHS.toString(), months.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " months too large to be supported by this implementation "
//+ months.toString()
);
}
if (days != null && days.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.DAYS.toString(), days.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " days too large to be supported by this implementation "
//+ days.toString()
);
}
if (hours != null && hours.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.HOURS.toString(), hours.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " hours too large to be supported by this implementation "
//+ hours.toString()
);
}
if (minutes != null && minutes.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.MINUTES.toString(), minutes.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " minutes too large to be supported by this implementation "
//+ minutes.toString()
);
}
if (seconds != null && seconds.toBigInteger().compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.SECONDS.toString(), seconds.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " seconds too large to be supported by this implementation "
//+ seconds.toString()
);
}
// check for fields that are too large in rhs Duration
BigInteger rhsYears = (BigInteger) rhs.getField(DatatypeConstants.YEARS);
if (rhsYears != null && rhsYears.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.YEARS.toString(), rhsYears.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " years too large to be supported by this implementation "
//+ rhsYears.toString()
);
}
BigInteger rhsMonths = (BigInteger) rhs.getField(DatatypeConstants.MONTHS);
if (rhsMonths != null && rhsMonths.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.MONTHS.toString(), rhsMonths.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " months too large to be supported by this implementation "
//+ rhsMonths.toString()
);
}
BigInteger rhsDays = (BigInteger) rhs.getField(DatatypeConstants.DAYS);
if (rhsDays != null && rhsDays.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.DAYS.toString(), rhsDays.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " days too large to be supported by this implementation "
//+ rhsDays.toString()
);
}
BigInteger rhsHours = (BigInteger) rhs.getField(DatatypeConstants.HOURS);
if (rhsHours != null && rhsHours.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.HOURS.toString(), rhsHours.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " hours too large to be supported by this implementation "
//+ rhsHours.toString()
);
}
BigInteger rhsMinutes = (BigInteger) rhs.getField(DatatypeConstants.MINUTES);
if (rhsMinutes != null && rhsMinutes.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.MINUTES.toString(), rhsMinutes.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " minutes too large to be supported by this implementation "
//+ rhsMinutes.toString()
);
}
BigDecimal rhsSecondsAsBigDecimal = (BigDecimal) rhs.getField(DatatypeConstants.SECONDS);
BigInteger rhsSeconds = null;
if ( rhsSecondsAsBigDecimal != null ) {
rhsSeconds = rhsSecondsAsBigDecimal.toBigInteger();
}
if (rhsSeconds != null && rhsSeconds.compareTo(maxintAsBigInteger) == 1) {
throw new UnsupportedOperationException(
DatatypeMessageFormatter.formatMessage(null, "TooLarge",
new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.SECONDS.toString(), rhsSeconds.toString()})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " seconds too large to be supported by this implementation "
//+ rhsSeconds.toString()
);
}
// turn this Duration into a GregorianCalendar
GregorianCalendar lhsCalendar = new GregorianCalendar(
1970,
1,
1,
0,
0,
0);
lhsCalendar.add(GregorianCalendar.YEAR, getYears() * getSign());
lhsCalendar.add(GregorianCalendar.MONTH, getMonths() * getSign());
lhsCalendar.add(GregorianCalendar.DAY_OF_YEAR, getDays() * getSign());
lhsCalendar.add(GregorianCalendar.HOUR_OF_DAY, getHours() * getSign());
lhsCalendar.add(GregorianCalendar.MINUTE, getMinutes() * getSign());
lhsCalendar.add(GregorianCalendar.SECOND, getSeconds() * getSign());
// turn compare Duration into a GregorianCalendar
GregorianCalendar rhsCalendar = new GregorianCalendar(
1970,
1,
1,
0,
0,
0);
rhsCalendar.add(GregorianCalendar.YEAR, rhs.getYears() * rhs.getSign());
rhsCalendar.add(GregorianCalendar.MONTH, rhs.getMonths() * rhs.getSign());
rhsCalendar.add(GregorianCalendar.DAY_OF_YEAR, rhs.getDays() * rhs.getSign());
rhsCalendar.add(GregorianCalendar.HOUR_OF_DAY, rhs.getHours() * rhs.getSign());
rhsCalendar.add(GregorianCalendar.MINUTE, rhs.getMinutes() * rhs.getSign());
rhsCalendar.add(GregorianCalendar.SECOND, rhs.getSeconds() * rhs.getSign());
if (lhsCalendar.before(rhsCalendar)) {
return DatatypeConstants.LESSER;
}
if (lhsCalendar.after(rhsCalendar)) {
return DatatypeConstants.GREATER;
}
if (lhsCalendar.equals(rhsCalendar)) {
return DatatypeConstants.EQUAL;
}
return DatatypeConstants.INDETERMINATE;
| private static long | getCalendarTimeInMillis(java.util.Calendar cal)Calls the {@link Calendar#getTimeInMillis} method.
Prior to JDK1.4, this method was protected and therefore
cannot be invoked directly.
In future, this should be replaced by
cal.getTimeInMillis()
return cal.getTime().getTime();
| public int | getDays()Obtains the value of the DAYS field as an integer value,
or 0 if not present.
This method works just like {@link #getYears()} except
that this method works on the DAYS field.
return getInt(DatatypeConstants.DAYS);
| public java.lang.Number | getField(javax.xml.datatype.DatatypeConstants$Field field)Gets the value of a field.
Fields of a duration object may contain arbitrary large value.
Therefore this method is designed to return a {@link Number} object.
In case of YEARS, MONTHS, DAYS, HOURS, and MINUTES, the returned
number will be a non-negative integer. In case of seconds,
the returned number may be a non-negative decimal value.
if (field == null) {
String methodName = "javax.xml.datatype.Duration" + "#isSet(DatatypeConstants.Field field) " ;
throw new NullPointerException(
DatatypeMessageFormatter.formatMessage(null,"FieldCannotBeNull", new Object[]{methodName})
);
}
if (field == DatatypeConstants.YEARS) {
return years;
}
if (field == DatatypeConstants.MONTHS) {
return months;
}
if (field == DatatypeConstants.DAYS) {
return days;
}
if (field == DatatypeConstants.HOURS) {
return hours;
}
if (field == DatatypeConstants.MINUTES) {
return minutes;
}
if (field == DatatypeConstants.SECONDS) {
return seconds;
}
/**
throw new IllegalArgumentException(
"javax.xml.datatype.Duration"
+ "#(getSet(DatatypeConstants.Field field) called with an unknown field: "
+ field.toString()
);
*/
String methodName = "javax.xml.datatype.Duration" + "#(getSet(DatatypeConstants.Field field)";
throw new IllegalArgumentException(
DatatypeMessageFormatter.formatMessage(null,"UnknownField", new Object[]{methodName, field.toString()})
);
| private java.math.BigDecimal | getFieldAsBigDecimal(javax.xml.datatype.DatatypeConstants$Field f)Gets the value of the field as a {@link BigDecimal}.
If the field is unset, return 0.
if (f == DatatypeConstants.SECONDS) {
if (seconds != null) {
return seconds;
} else {
return ZERO;
}
} else {
BigInteger bi = (BigInteger) getField(f);
if (bi == null) {
return ZERO;
} else {
return new BigDecimal(bi);
}
}
| public int | getHours()Obtains the value of the HOURS field as an integer value,
or 0 if not present.
This method works just like {@link #getYears()} except
that this method works on the HOURS field.
return getInt(DatatypeConstants.HOURS);
| private int | getInt(javax.xml.datatype.DatatypeConstants$Field field)Return the requested field value as an int.
If field is not set, i.e. == null, 0 is returned.
Number n = getField(field);
if (n == null) {
return 0;
} else {
return n.intValue();
}
| public int | getMinutes()Obtains the value of the MINUTES field as an integer value,
or 0 if not present.
This method works just like {@link #getYears()} except
that this method works on the MINUTES field.
return getInt(DatatypeConstants.MINUTES);
| public int | getMonths()Obtains the value of the MONTHS field as an integer value,
or 0 if not present.
This method works just like {@link #getYears()} except
that this method works on the MONTHS field.
return getInt(DatatypeConstants.MONTHS);
| public int | getSeconds()Obtains the value of the SECONDS field as an integer value,
or 0 if not present.
This method works just like {@link #getYears()} except
that this method works on the SECONDS field.
return getInt(DatatypeConstants.SECONDS);
| public int | getSign()Returns the sign of this duration in -1,0, or 1.
return signum;
| public long | getTimeInMillis(java.util.Calendar startInstant)Returns the length of the duration in milli-seconds.
If the seconds field carries more digits than milli-second order,
those will be simply discarded (or in other words, rounded to zero.)
For example, for any Calendar value x,
new Duration("PT10.00099S").getTimeInMills(x) == 10000 .
new Duration("-PT10.00099S").getTimeInMills(x) == -10000 .
Note that this method uses the {@link #addTo(Calendar)} method,
which may work incorectly with {@link Duration} objects with
very large values in its fields. See the {@link #addTo(Calendar)}
method for details.
Calendar cal = (Calendar) startInstant.clone();
addTo(cal);
return getCalendarTimeInMillis(cal)
- getCalendarTimeInMillis(startInstant);
| public long | getTimeInMillis(java.util.Date startInstant)Returns the length of the duration in milli-seconds.
If the seconds field carries more digits than milli-second order,
those will be simply discarded (or in other words, rounded to zero.)
For example, for any Date value x,
new Duration("PT10.00099S").getTimeInMills(x) == 10000 .
new Duration("-PT10.00099S").getTimeInMills(x) == -10000 .
Note that this method uses the {@link #addTo(Date)} method,
which may work incorectly with {@link Duration} objects with
very large values in its fields. See the {@link #addTo(Date)}
method for details.
Calendar cal = new GregorianCalendar();
cal.setTime(startInstant);
this.addTo(cal);
return getCalendarTimeInMillis(cal) - startInstant.getTime();
| public int | getYears()Obtains the value of the YEARS field as an integer value,
or 0 if not present.
This method is a convenience method around the
{@link #getField(DatatypeConstants.Field)} method.
Note that since this method returns int, this
method will return an incorrect value for {@link Duration}s
with the year field that goes beyond the range of int.
Use getField(YEARS) to avoid possible loss of precision.
return getInt(DatatypeConstants.YEARS);
| public int | hashCode()Returns a hash code consistent with the definition of the equals method.
// component wise hash is not correct because 1day = 24hours
Calendar cal = TEST_POINTS[0].toGregorianCalendar();
this.addTo(cal);
return (int) getCalendarTimeInMillis(cal);
| private static boolean | isDigit(char ch)TODO: Javadoc
return '0" <= ch && ch <= '9";
| private static boolean | isDigitOrPeriod(char ch)TODO: Javadoc
return isDigit(ch) || ch == '.";
| public boolean | isSet(javax.xml.datatype.DatatypeConstants$Field field)Checks if a field is set.
A field of a duration object may or may not be present.
This method can be used to test if a field is present.
if (field == null) {
String methodName = "javax.xml.datatype.Duration" + "#isSet(DatatypeConstants.Field field)" ;
throw new NullPointerException(
//"cannot be called with field == null"
DatatypeMessageFormatter.formatMessage(null, "FieldCannotBeNull", new Object[]{methodName})
);
}
if (field == DatatypeConstants.YEARS) {
return years != null;
}
if (field == DatatypeConstants.MONTHS) {
return months != null;
}
if (field == DatatypeConstants.DAYS) {
return days != null;
}
if (field == DatatypeConstants.HOURS) {
return hours != null;
}
if (field == DatatypeConstants.MINUTES) {
return minutes != null;
}
if (field == DatatypeConstants.SECONDS) {
return seconds != null;
}
String methodName = "javax.xml.datatype.Duration" + "#isSet(DatatypeConstants.Field field)";
throw new IllegalArgumentException(
DatatypeMessageFormatter.formatMessage(null,"UnknownField", new Object[]{methodName, field.toString()})
);
| public javax.xml.datatype.Duration | multiply(int factor)Computes a new duration whose value is factor times
longer than the value of this duration.
This method is provided for the convenience.
It is functionally equivalent to the following code:
multiply(new BigDecimal(String.valueOf(factor)))
return multiply(BigDecimal.valueOf(factor));
| public javax.xml.datatype.Duration | multiply(java.math.BigDecimal factor)Computes a new duration whose value is factor times
longer than the value of this duration.
For example,
"P1M" (1 month) * "12" = "P12M" (12 months)
"PT1M" (1 min) * "0.3" = "PT18S" (18 seconds)
"P1M" (1 month) * "1.5" = IllegalStateException
Since the {@link Duration} class is immutable, this method
doesn't change the value of this object. It simply computes
a new Duration object and returns it.
The operation will be performed field by field with the precision
of {@link BigDecimal}. Since all the fields except seconds are
restricted to hold integers,
any fraction produced by the computation will be
carried down toward the next lower unit. For example,
if you multiply "P1D" (1 day) with "0.5", then it will be 0.5 day,
which will be carried down to "PT12H" (12 hours).
When fractions of month cannot be meaningfully carried down
to days, or year to months, this will cause an
{@link IllegalStateException} to be thrown.
For example if you multiple one month by 0.5.
To avoid {@link IllegalStateException}, use
the {@link #normalizeWith(Calendar)} method to remove the years
and months fields.
BigDecimal carry = ZERO;
int factorSign = factor.signum();
factor = factor.abs();
BigDecimal[] buf = new BigDecimal[6];
for (int i = 0; i < 5; i++) {
BigDecimal bd = getFieldAsBigDecimal(FIELDS[i]);
bd = bd.multiply(factor).add(carry);
buf[i] = bd.setScale(0, BigDecimal.ROUND_DOWN);
bd = bd.subtract(buf[i]);
if (i == 1) {
if (bd.signum() != 0) {
throw new IllegalStateException(); // illegal carry-down
} else {
carry = ZERO;
}
} else {
carry = bd.multiply(FACTORS[i]);
}
}
if (seconds != null) {
buf[5] = seconds.multiply(factor).add(carry);
} else {
buf[5] = carry;
}
return new DurationImpl(
this.signum * factorSign >= 0,
toBigInteger(buf[0], null == years),
toBigInteger(buf[1], null == months),
toBigInteger(buf[2], null == days),
toBigInteger(buf[3], null == hours),
toBigInteger(buf[4], null == minutes),
(buf[5].signum() == 0 && seconds == null) ? null : buf[5]);
| public javax.xml.datatype.Duration | negate()Returns a new {@link Duration} object whose
value is -this .
Since the {@link Duration} class is immutable, this method
doesn't change the value of this object. It simply computes
a new Duration object and returns it.
return new DurationImpl(
signum <= 0,
years,
months,
days,
hours,
minutes,
seconds);
| public javax.xml.datatype.Duration | normalizeWith(java.util.Calendar startTimeInstant)Converts the years and months fields into the days field
by using a specific time instant as the reference point.
For example, duration of one month normalizes to 31 days
given the start time instance "July 8th 2003, 17:40:32".
Formally, the computation is done as follows:
- The given Calendar object is cloned.
- The years, months and days fields will be added to
the {@link Calendar} object
by using the {@link Calendar#add(int,int)} method.
- The difference between two Calendars are computed in terms of days.
- The computed days, along with the hours, minutes and seconds
fields of this duration object is used to construct a new
Duration object.
Note that since the Calendar class uses int to
hold the value of year and month, this method may produce
an unexpected result if this duration object holds
a very large value in the years or months fields.
Calendar c = (Calendar) startTimeInstant.clone();
// using int may cause overflow, but
// Calendar internally treats value as int anyways.
c.add(Calendar.YEAR, getYears() * signum);
c.add(Calendar.MONTH, getMonths() * signum);
c.add(Calendar.DAY_OF_MONTH, getDays() * signum);
// obtain the difference in terms of days
long diff = getCalendarTimeInMillis(c) - getCalendarTimeInMillis(startTimeInstant);
int days = (int) (diff / (1000L * 60L * 60L * 24L));
return new DurationImpl(
days >= 0,
null,
null,
wrap(Math.abs(days)),
(BigInteger) getField(DatatypeConstants.HOURS),
(BigInteger) getField(DatatypeConstants.MINUTES),
(BigDecimal) getField(DatatypeConstants.SECONDS));
| private static void | organizeParts(java.lang.String whole, java.lang.String[] parts, int[] partsIndex, int len, java.lang.String tokens)TODO: Javadoc.
int idx = tokens.length();
for (int i = len - 1; i >= 0; i--) {
int nidx =
tokens.lastIndexOf(
parts[i].charAt(parts[i].length() - 1),
idx - 1);
if (nidx == -1) {
throw new IllegalArgumentException(whole);
// ,partsIndex[i]+parts[i].length()-1);
}
for (int j = nidx + 1; j < idx; j++) {
parts[j] = null;
}
idx = nidx;
parts[idx] = parts[i];
partsIndex[idx] = partsIndex[i];
}
for (idx--; idx >= 0; idx--) {
parts[idx] = null;
}
| private static java.math.BigDecimal | parseBigDecimal(java.lang.String whole, java.lang.String part, int index)TODO: Javadoc.
if (part == null) {
return null;
}
part = part.substring(0, part.length() - 1);
// NumberFormatException is IllegalArgumentException
// try {
return new BigDecimal(part);
// } catch( NumberFormatException e ) {
// throw new ParseException( whole, index );
// }
| private static java.math.BigInteger | parseBigInteger(java.lang.String whole, java.lang.String part, int index)TODO: Javadoc
if (part == null) {
return null;
}
part = part.substring(0, part.length() - 1);
// try {
return new BigInteger(part);
// } catch( NumberFormatException e ) {
// throw new ParseException( whole, index );
// }
| private static java.lang.String | parsePiece(java.lang.String whole, int[] idx)TODO: Javadoc
int start = idx[0];
while (idx[0] < whole.length()
&& isDigitOrPeriod(whole.charAt(idx[0]))) {
idx[0]++;
}
if (idx[0] == whole.length()) {
throw new IllegalArgumentException(whole); // ,idx[0]);
}
idx[0]++;
return whole.substring(start, idx[0]);
| private static java.math.BigDecimal | sanitize(java.math.BigInteger value, int signum)Compute value*signum where value==null is treated as
value==0.
if (signum == 0 || value == null) {
return ZERO;
}
if (signum > 0) {
return new BigDecimal(value);
}
return new BigDecimal(value.negate());
| static java.math.BigDecimal | sanitize(java.math.BigDecimal value, int signum)Compute value*signum where value==null is treated as value==0 .
if (signum == 0 || value == null) {
return ZERO;
}
if (signum > 0) {
return value;
}
return value.negate();
| public int | signum()Returns the sign of this duration in -1,0, or 1.
return signum;
| public javax.xml.datatype.Duration | subtract(javax.xml.datatype.Duration rhs)Computes a new duration whose value is this-rhs .
For example:
"1 day" - "-3 days" = "4 days"
"1 year" - "1 day" = IllegalStateException
"-(1 hour,50 minutes)" - "-20 minutes" = "-(1hours,30 minutes)"
"15 hours" - "-3 days" = "3 days and 15 hours"
"1 year" - "-1 day" = "1 year and 1 day"
Since there's no way to meaningfully subtract 1 day from 1 month,
there are cases where the operation fails in {@link IllegalStateException}.
Formally the computation is defined as follows.
First, we can assume that two {@link Duration}s are both positive
without losing generality. (i.e.,
(-X)-Y=-(X+Y) , X-(-Y)=X+Y ,
(-X)-(-Y)=-(X-Y) )
Then two durations are subtracted field by field.
If the sign of any non-zero field F is different from
the sign of the most significant field,
1 (if F is negative) or -1 (otherwise)
will be borrowed from the next bigger unit of F.
This process is repeated until all the non-zero fields have
the same sign.
If a borrow occurs in the days field (in other words, if
the computation needs to borrow 1 or -1 month to compensate
days), then the computation fails by throwing an
{@link IllegalStateException}.
return add(rhs.negate());
| private static void | testNonNegative(java.math.BigInteger n, javax.xml.datatype.DatatypeConstants$Field f)Makes sure that the given number is non-negative. If it is not,
throw {@link IllegalArgumentException}.
if (n != null && n.signum() < 0) {
throw new IllegalArgumentException(
DatatypeMessageFormatter.formatMessage(null, "NegativeField", new Object[]{f.toString()})
);
}
| private static void | testNonNegative(java.math.BigDecimal n, javax.xml.datatype.DatatypeConstants$Field f)Makes sure that the given number is non-negative. If it is not,
throw {@link IllegalArgumentException}.
if (n != null && n.signum() < 0) {
throw new IllegalArgumentException(
DatatypeMessageFormatter.formatMessage(null, "NegativeField", new Object[]{f.toString()})
);
}
| private static java.math.BigInteger | toBigInteger(java.math.BigDecimal value, boolean canBeNull)BigInteger value of BigDecimal value.
if (canBeNull && value.signum() == 0) {
return null;
} else {
return value.unscaledValue();
}
| public java.lang.String | toString()Returns a string representation of this duration object.
The result is formatter according to the XML Schema 1.0
spec and can be always parsed back later into the
equivalent duration object by
the {@link #DurationImpl(String)} constructor.
Formally, the following holds for any {@link Duration}
object x.
new Duration(x.toString()).equals(x)
StringBuffer buf = new StringBuffer();
if (signum < 0) {
buf.append('-");
}
buf.append('P");
if (years != null) {
buf.append(years + "Y");
}
if (months != null) {
buf.append(months + "M");
}
if (days != null) {
buf.append(days + "D");
}
if (hours != null || minutes != null || seconds != null) {
buf.append('T");
if (hours != null) {
buf.append(hours + "H");
}
if (minutes != null) {
buf.append(minutes + "M");
}
if (seconds != null) {
buf.append(toString(seconds) + "S");
}
}
return buf.toString();
| private java.lang.String | toString(java.math.BigDecimal bd)Turns {@link BigDecimal} to a string representation.
Due to a behavior change in the {@link BigDecimal#toString()}
method in JDK1.5, this had to be implemented here.
String intString = bd.unscaledValue().toString();
int scale = bd.scale();
if (scale == 0) {
return intString;
}
/* Insert decimal point */
StringBuffer buf;
int insertionPoint = intString.length() - scale;
if (insertionPoint == 0) { /* Point goes right before intVal */
return "0." + intString;
} else if (insertionPoint > 0) { /* Point goes inside intVal */
buf = new StringBuffer(intString);
buf.insert(insertionPoint, '.");
} else { /* We must insert zeros between point and intVal */
buf = new StringBuffer(3 - insertionPoint + intString.length());
buf.append("0.");
for (int i = 0; i < -insertionPoint; i++) {
buf.append('0");
}
buf.append(intString);
}
return buf.toString();
| private static java.math.BigInteger | wrap(int i)TODO: Javadoc
// field may not be set
if (i == DatatypeConstants.FIELD_UNDEFINED) {
return null;
}
// int -> BigInteger
return new BigInteger(String.valueOf(i));
| private java.lang.Object | writeReplace()Writes {@link Duration} as a lexical representation
for maximum future compatibility.
return new DurationStream(this.toString());
|
|