jaxp/src/com/sun/org/apache/xerces/internal/jaxp/datatype/DurationImpl.java

Print this page

        

*** 1,16 **** /* * reserved comment block * DO NOT REMOVE OR ALTER! */ /* ! * Copyright 2005 The Apache Software Foundation. * - * 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. --- 1,17 ---- /* * reserved comment block * DO NOT REMOVE OR ALTER! */ /* ! * 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 * * 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,117 **** */ 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[]{ --- 105,114 ----
*** 141,151 **** private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); /** * <p>BigDecimal value of 0.</p> */ ! private static final BigDecimal ZERO = BigDecimal.valueOf((long) 0); /** * <p>Indicates the sign. -1, 0 or 1 if the duration is negative, * zero, or positive.</p> */ --- 138,148 ---- private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); /** * <p>BigDecimal value of 0.</p> */ ! 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,227 **** return 0; } if (isPositive) { return 1; ! } else { return -1; } - } /** * <p>Constructs a new Duration object by specifying each field individually.</p> * --- 211,224 ---- return 0; } if (isPositive) { return 1; ! } ! else { return -1; } } /** * <p>Constructs a new Duration object by specifying each field individually.</p> *
*** 355,365 **** if (i == DatatypeConstants.FIELD_UNDEFINED) { return null; } // int -> BigInteger ! return new BigInteger(String.valueOf(i)); } /** * <p>Constructs a new Duration object by specifying the duration * in milliseconds.</p> --- 352,362 ---- if (i == DatatypeConstants.FIELD_UNDEFINED) { return null; } // int -> BigInteger ! return BigInteger.valueOf(i); } /** * <p>Constructs a new Duration object by specifying the duration * in milliseconds.</p>
*** 371,388 **** long l = durationInMilliSeconds; if (l > 0) { signum = 1; ! } else if (l < 0) { signum = -1; if (l == 0x8000000000000000L) { // negating 0x8000000000000000L causes an overflow l++; } l *= -1; ! } else { signum = 0; } // let GregorianCalendar do the heavy lifting GregorianCalendar gregorianCalendar = new GregorianCalendar(GMT); --- 368,387 ---- long l = durationInMilliSeconds; if (l > 0) { signum = 1; ! } ! else if (l < 0) { signum = -1; if (l == 0x8000000000000000L) { // negating 0x8000000000000000L causes an overflow l++; } l *= -1; ! } ! else { signum = 0; } // let GregorianCalendar do the heavy lifting GregorianCalendar gregorianCalendar = new GregorianCalendar(GMT);
*** 452,476 **** */ protected DurationImpl(String lexicalRepresentation) throws IllegalArgumentException { // 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); --- 451,476 ---- */ 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; 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);
*** 491,501 **** } if (length != idx[0]) { if (s.charAt(idx[0]++) == 'T') { timeRequired = true; ! } else { throw new IllegalArgumentException(s); // ,idx[0]-1); } } int timeLen = 0; --- 491,502 ---- } if (length != idx[0]) { if (s.charAt(idx[0]++) == 'T') { timeRequired = true; ! } ! else { throw new IllegalArgumentException(s); // ,idx[0]-1); } } int timeLen = 0;
*** 602,611 **** --- 603,615 ---- 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,731 **** * @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); // check for fields that are too large in this Duration if (years != null && years.compareTo(maxintAsBigInteger) == 1) { throw new UnsupportedOperationException( DatatypeMessageFormatter.formatMessage(null, "TooLarge", --- 724,734 ---- * @see #isShorterThan(Duration) * @see #isLongerThan(Duration) */ public int compare(Duration rhs) { ! 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,786 **** ); } 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() ); --- 779,789 ---- ); } 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(), toString(seconds)}) //this.getClass().getName() + "#compare(Duration duration)" //+ " seconds too large to be supported by this implementation " //+ seconds.toString() );
*** 955,965 **** resultA = compareResults(resultA, resultB); return resultA; } ! private int compareResults(int resultA, int resultB){ if ( resultB == DatatypeConstants.INDETERMINATE ) { return DatatypeConstants.INDETERMINATE; } else if ( resultA!=resultB) { --- 958,968 ---- resultA = compareResults(resultA, resultB); return resultA; } ! private int compareResults(int resultA, int resultB) { if ( resultB == DatatypeConstants.INDETERMINATE ) { return DatatypeConstants.INDETERMINATE; } else if ( resultA!=resultB) {
*** 1005,1033 **** 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(); } --- 1008,1036 ---- buf.append('-'); } buf.append('P'); if (years != null) { ! buf.append(years).append('Y'); } if (months != null) { ! buf.append(months).append('M'); } if (days != null) { ! buf.append(days).append('D'); } if (hours != null || minutes != null || seconds != null) { buf.append('T'); if (hours != null) { ! buf.append(hours).append('H'); } if (minutes != null) { ! buf.append(minutes).append('M'); } if (seconds != null) { ! buf.append(toString(seconds)).append('S'); } } return buf.toString(); }
*** 1053,1066 **** /* 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'); } --- 1056,1071 ---- /* 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'); }
*** 1300,1310 **** */ private int getInt(DatatypeConstants.Field field) { Number n = getField(field); if (n == null) { return 0; ! } else { return n.intValue(); } } /** --- 1305,1316 ---- */ private int getInt(DatatypeConstants.Field field) { Number n = getField(field); if (n == null) { return 0; ! } ! else { return n.intValue(); } } /**
*** 1338,1349 **** * */ public long getTimeInMillis(final Calendar startInstant) { Calendar cal = (Calendar) startInstant.clone(); addTo(cal); ! return getCalendarTimeInMillis(cal) ! - getCalendarTimeInMillis(startInstant); } /** * <p>Returns the length of the duration in milli-seconds.</p> * --- 1344,1354 ---- * */ public long getTimeInMillis(final Calendar startInstant) { Calendar cal = (Calendar) startInstant.clone(); addTo(cal); ! return getCalendarTimeInMillis(cal) - getCalendarTimeInMillis(startInstant); } /** * <p>Returns the length of the duration in milli-seconds.</p> *
*** 1545,1562 **** 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, --- 1550,1569 ---- 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,
*** 1579,1596 **** */ private BigDecimal getFieldAsBigDecimal(DatatypeConstants.Field f) { 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); } } } --- 1586,1606 ---- */ private BigDecimal getFieldAsBigDecimal(DatatypeConstants.Field f) { 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); } } }
*** 1605,1624 **** private static BigInteger toBigInteger( BigDecimal value, boolean canBeNull) { if (canBeNull && value.signum() == 0) { return null; ! } 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[]{ BigDecimal.valueOf(12), null/*undefined*/, BigDecimal.valueOf(24), BigDecimal.valueOf(60), BigDecimal.valueOf(60) --- 1615,1635 ---- private static BigInteger toBigInteger( BigDecimal value, boolean canBeNull) { if (canBeNull && value.signum() == 0) { return null; ! } ! 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[] { BigDecimal.valueOf(12), null/*undefined*/, BigDecimal.valueOf(24), BigDecimal.valueOf(60), BigDecimal.valueOf(60)
*** 1962,1975 **** this.addTo(cal); date.setTime(getCalendarTimeInMillis(cal)); } /** * <p>Stream Unique Identifier.</p> * ! * <p>TODO: Serialization should use the XML string representation as ! * the serialization format to ensure future compatibility.</p> */ private static final long serialVersionUID = 1L; /** * Writes {@link Duration} as a lexical representation --- 1973,1996 ---- 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>Serialization uses the lexical form returned by toString().</p> */ private static final long serialVersionUID = 1L; /** * Writes {@link Duration} as a lexical representation
*** 1994,2020 **** 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(); - } } --- 2015,2026 ----