📄 durationimpl.java
字号:
//+ " 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; } /** * 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; } 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()}) ); } /** * 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. * * @param field * one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, * MINUTES, or SECONDS.) * @return * If the specified field is present, this method returns * a non-null non-negative {@link Number} object that * represents its value. If it is not present, return null. * For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method * returns a {@link BigInteger} object. For SECONDS, this * method returns a {@link BigDecimal}. * * @throws NullPointerException * If the field parameter is null. */ public Number getField(DatatypeConstants.Field field) { 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()}) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -