jaxp/src/com/sun/org/apache/xerces/internal/jaxp/datatype/DurationImpl.java
Print this page
@@ -1,16 +1,17 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
- * Copyright 2005 The Apache Software Foundation.
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -104,14 +105,10 @@
*/
class DurationImpl
extends Duration
implements Serializable {
- /**
- * <p>Number of Fields.</p>
- */
- private static final int FIELD_NUM = 6;
/**
* <p>Internal array of value Fields.</p>
*/
private static final DatatypeConstants.Field[] FIELDS = new DatatypeConstants.Field[]{
@@ -141,11 +138,11 @@
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
/**
* <p>BigDecimal value of 0.</p>
*/
- private static final BigDecimal ZERO = BigDecimal.valueOf((long) 0);
+ private static final BigDecimal ZERO = BigDecimal.valueOf(0);
/**
* <p>Indicates the sign. -1, 0 or 1 if the duration is negative,
* zero, or positive.</p>
*/
@@ -214,14 +211,14 @@
return 0;
}
if (isPositive) {
return 1;
- } else {
+ }
+ else {
return -1;
}
-
}
/**
* <p>Constructs a new Duration object by specifying each field individually.</p>
*
@@ -355,11 +352,11 @@
if (i == DatatypeConstants.FIELD_UNDEFINED) {
return null;
}
// int -> BigInteger
- return new BigInteger(String.valueOf(i));
+ return BigInteger.valueOf(i);
}
/**
* <p>Constructs a new Duration object by specifying the duration
* in milliseconds.</p>
@@ -371,18 +368,20 @@
long l = durationInMilliSeconds;
if (l > 0) {
signum = 1;
- } else if (l < 0) {
+ }
+ else if (l < 0) {
signum = -1;
if (l == 0x8000000000000000L) {
// negating 0x8000000000000000L causes an overflow
l++;
}
l *= -1;
- } else {
+ }
+ else {
signum = 0;
}
// let GregorianCalendar do the heavy lifting
GregorianCalendar gregorianCalendar = new GregorianCalendar(GMT);
@@ -452,25 +451,26 @@
*/
protected DurationImpl(String lexicalRepresentation)
throws IllegalArgumentException {
// only if I could use the JDK1.4 regular expression ....
+ if (lexicalRepresentation == null) {
+ throw new NullPointerException();
+ }
+
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 {
+ }
+ else {
positive = true;
}
if (length != idx[0] && s.charAt(idx[0]++) != 'P') {
throw new IllegalArgumentException(s); //,idx[0]-1);
@@ -491,11 +491,12 @@
}
if (length != idx[0]) {
if (s.charAt(idx[0]++) == 'T') {
timeRequired = true;
- } else {
+ }
+ else {
throw new IllegalArgumentException(s); // ,idx[0]-1);
}
}
int timeLen = 0;
@@ -602,10 +603,13 @@
String tokens)
throws IllegalArgumentException {
int idx = tokens.length();
for (int i = len - 1; i >= 0; i--) {
+ if (parts[i] == null) {
+ throw new IllegalArgumentException(whole);
+ }
int nidx =
tokens.lastIndexOf(
parts[i].charAt(parts[i].length() - 1),
idx - 1);
if (nidx == -1) {
@@ -720,12 +724,11 @@
* @see #isShorterThan(Duration)
* @see #isLongerThan(Duration)
*/
public int compare(Duration rhs) {
- BigInteger maxintAsBigInteger = BigInteger.valueOf((long) Integer.MAX_VALUE);
- BigInteger minintAsBigInteger = BigInteger.valueOf((long) Integer.MIN_VALUE);
+ BigInteger maxintAsBigInteger = BigInteger.valueOf(Integer.MAX_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",
@@ -776,11 +779,11 @@
);
}
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()})
+ new Object[]{this.getClass().getName() + "#compare(Duration duration)" + DatatypeConstants.SECONDS.toString(), toString(seconds)})
//this.getClass().getName() + "#compare(Duration duration)"
//+ " seconds too large to be supported by this implementation "
//+ seconds.toString()
);
@@ -955,11 +958,11 @@
resultA = compareResults(resultA, resultB);
return resultA;
}
- private int compareResults(int resultA, int resultB){
+ private int compareResults(int resultA, int resultB) {
if ( resultB == DatatypeConstants.INDETERMINATE ) {
return DatatypeConstants.INDETERMINATE;
}
else if ( resultA!=resultB) {
@@ -1005,29 +1008,29 @@
buf.append('-');
}
buf.append('P');
if (years != null) {
- buf.append(years + "Y");
+ buf.append(years).append('Y');
}
if (months != null) {
- buf.append(months + "M");
+ buf.append(months).append('M');
}
if (days != null) {
- buf.append(days + "D");
+ buf.append(days).append('D');
}
if (hours != null || minutes != null || seconds != null) {
buf.append('T');
if (hours != null) {
- buf.append(hours + "H");
+ buf.append(hours).append('H');
}
if (minutes != null) {
- buf.append(minutes + "M");
+ buf.append(minutes).append('M');
}
if (seconds != null) {
- buf.append(toString(seconds) + "S");
+ buf.append(toString(seconds)).append('S');
}
}
return buf.toString();
}
@@ -1053,14 +1056,16 @@
/* 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 */
+ }
+ else if (insertionPoint > 0) { /* Point goes inside intVal */
buf = new StringBuffer(intString);
buf.insert(insertionPoint, '.');
- } else { /* We must insert zeros between point and intVal */
+ }
+ 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');
}
@@ -1300,11 +1305,12 @@
*/
private int getInt(DatatypeConstants.Field field) {
Number n = getField(field);
if (n == null) {
return 0;
- } else {
+ }
+ else {
return n.intValue();
}
}
/**
@@ -1338,12 +1344,11 @@
*
*/
public long getTimeInMillis(final Calendar startInstant) {
Calendar cal = (Calendar) startInstant.clone();
addTo(cal);
- return getCalendarTimeInMillis(cal)
- - getCalendarTimeInMillis(startInstant);
+ return getCalendarTimeInMillis(cal) - getCalendarTimeInMillis(startInstant);
}
/**
* <p>Returns the length of the duration in milli-seconds.</p>
*
@@ -1545,18 +1550,20 @@
if (bd.signum() != 0) {
throw new IllegalStateException(); // illegal carry-down
} else {
carry = ZERO;
}
- } else {
+ }
+ else {
carry = bd.multiply(FACTORS[i]);
}
}
if (seconds != null) {
buf[5] = seconds.multiply(factor).add(carry);
- } else {
+ }
+ else {
buf[5] = carry;
}
return new DurationImpl(
this.signum * factorSign >= 0,
@@ -1579,18 +1586,21 @@
*/
private BigDecimal getFieldAsBigDecimal(DatatypeConstants.Field f) {
if (f == DatatypeConstants.SECONDS) {
if (seconds != null) {
return seconds;
- } else {
+ }
+ else {
return ZERO;
}
- } else {
+ }
+ else {
BigInteger bi = (BigInteger) getField(f);
if (bi == null) {
return ZERO;
- } else {
+ }
+ else {
return new BigDecimal(bi);
}
}
}
@@ -1605,20 +1615,21 @@
private static BigInteger toBigInteger(
BigDecimal value,
boolean canBeNull) {
if (canBeNull && value.signum() == 0) {
return null;
- } else {
+ }
+ else {
return value.unscaledValue();
}
}
/**
* 1 unit of FIELDS[i] is equivalent to <code>FACTORS[i]</code> unit of
* FIELDS[i+1].
*/
- private static final BigDecimal[] FACTORS = new BigDecimal[]{
+ private static final BigDecimal[] FACTORS = new BigDecimal[] {
BigDecimal.valueOf(12),
null/*undefined*/,
BigDecimal.valueOf(24),
BigDecimal.valueOf(60),
BigDecimal.valueOf(60)
@@ -1962,14 +1973,24 @@
this.addTo(cal);
date.setTime(getCalendarTimeInMillis(cal));
}
/**
+ * Returns time value in milliseconds
+ * @param cal A calendar object
+ * @return time value
+ *
+ * Diff from Xerces; Use JDK 1.5 feature.
+ */
+ private static long getCalendarTimeInMillis(Calendar cal) {
+ return cal.getTimeInMillis();
+ }
+
+ /**
* <p>Stream Unique Identifier.</p>
*
- * <p>TODO: Serialization should use the XML string representation as
- * the serialization format to ensure future compatibility.</p>
+ * <p>Serialization uses the lexical form returned by toString().</p>
*/
private static final long serialVersionUID = 1L;
/**
* Writes {@link Duration} as a lexical representation
@@ -1994,27 +2015,12 @@
private DurationStream(String _lexical) {
this.lexical = _lexical;
}
private Object readResolve() throws ObjectStreamException {
- // try {
return new DurationImpl(lexical);
- // } catch( ParseException e ) {
- // throw new StreamCorruptedException("unable to parse "+lexical+" as duration");
- // }
}
private static final long serialVersionUID = 1L;
}
- /**
- * 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
- * <code>cal.getTimeInMillis()</code>
- */
- private static long getCalendarTimeInMillis(Calendar cal) {
- return cal.getTime().getTime();
- }
}