durationimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,826 行 · 第 1/5 页
JAVA
1,826 行
//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.equals(rhsCalendar)) { return DatatypeConstants.EQUAL; } return compareDates(this, rhs); } /** * Compares 2 given durations. (refer to W3C Schema Datatypes "3.2.6 duration") * * @param duration1 Unnormalized duration * @param duration2 Unnormalized duration * @return 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 */ private int compareDates(Duration duration1, Duration duration2) { int resultA = DatatypeConstants.INDETERMINATE; int resultB = DatatypeConstants.INDETERMINATE; XMLGregorianCalendar tempA = (XMLGregorianCalendar)TEST_POINTS[0].clone(); XMLGregorianCalendar tempB = (XMLGregorianCalendar)TEST_POINTS[0].clone(); //long comparison algorithm is required tempA.add(duration1); tempB.add(duration2); resultA = tempA.compare(tempB); if ( resultA == DatatypeConstants.INDETERMINATE ) { return DatatypeConstants.INDETERMINATE; } tempA = (XMLGregorianCalendar)TEST_POINTS[1].clone(); tempB = (XMLGregorianCalendar)TEST_POINTS[1].clone(); tempA.add(duration1); tempB.add(duration2); resultB = tempA.compare(tempB); resultA = compareResults(resultA, resultB); if (resultA == DatatypeConstants.INDETERMINATE) { return DatatypeConstants.INDETERMINATE; } tempA = (XMLGregorianCalendar)TEST_POINTS[2].clone(); tempB = (XMLGregorianCalendar)TEST_POINTS[2].clone(); tempA.add(duration1); tempB.add(duration2); resultB = tempA.compare(tempB); resultA = compareResults(resultA, resultB); if (resultA == DatatypeConstants.INDETERMINATE) { return DatatypeConstants.INDETERMINATE; } tempA = (XMLGregorianCalendar)TEST_POINTS[3].clone(); tempB = (XMLGregorianCalendar)TEST_POINTS[3].clone(); tempA.add(duration1); tempB.add(duration2); resultB = tempA.compare(tempB); resultA = compareResults(resultA, resultB); return resultA; } private int compareResults(int resultA, int resultB){ if ( resultB == DatatypeConstants.INDETERMINATE ) { return DatatypeConstants.INDETERMINATE; } else if ( resultA!=resultB) { return DatatypeConstants.INDETERMINATE; } return resultA; } /** * Returns a hash code consistent with the definition of the equals method. * * @see Object#hashCode() */ public int hashCode() { // component wise hash is not correct because 1day = 24hours Calendar cal = TEST_POINTS[0].toGregorianCalendar(); this.addTo(cal); return (int) getCalendarTimeInMillis(cal); } /** * Returns a string representation of this duration object. * * <p> * 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. * * <p> * Formally, the following holds for any {@link Duration} * object x. * <pre> * new Duration(x.toString()).equals(x) * </pre> * * @return * Always return a non-null valid String object. */ public String toString() { 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(); } /** * <p>Turns {@link BigDecimal} to a string representation.</p> * * <p>Due to a behavior change in the {@link BigDecimal#toString()} * method in JDK1.5, this had to be implemented here.</p> * * @param bd <code>BigDecimal</code> to format as a <code>String</code> * * @return <code>String</code> representation of <code>BigDecimal</code> */ private String toString(BigDecimal bd) { 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(); } /** * 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. * * @param field * one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, * MINUTES, or SECONDS.) * @return * true if the field is present. false if not. * * @throws NullPointerException * If the field parameter is null. */ public boolean isSet(DatatypeConstants.Field field) { 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; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?