1 3 9 10 package javax.xml.datatype; 11 12 import java.math.BigDecimal ; 13 import java.math.BigInteger ; 14 import java.util.Calendar ; 15 import java.util.Date ; 16 import java.util.GregorianCalendar ; 17 18 import javax.xml.namespace.QName ; 19 20 84 public abstract class Duration { 85 86 89 private static final boolean DEBUG = true; 90 91 152 public QName getXMLSchemaType() { 153 154 boolean yearSet = isSet(DatatypeConstants.YEARS); 155 boolean monthSet = isSet(DatatypeConstants.MONTHS); 156 boolean daySet = isSet(DatatypeConstants.DAYS); 157 boolean hourSet = isSet(DatatypeConstants.HOURS); 158 boolean minuteSet = isSet(DatatypeConstants.MINUTES); 159 boolean secondSet = isSet(DatatypeConstants.SECONDS); 160 161 if (yearSet 163 && monthSet 164 && daySet 165 && hourSet 166 && minuteSet 167 && secondSet) { 168 return DatatypeConstants.DURATION; 169 } 170 171 if (!yearSet 173 && !monthSet 174 && daySet 175 && hourSet 176 && minuteSet 177 && secondSet) { 178 return DatatypeConstants.DURATION_DAYTIME; 179 } 180 181 if (yearSet 183 && monthSet 184 && !daySet 185 && !hourSet 186 && !minuteSet 187 && !secondSet) { 188 return DatatypeConstants.DURATION_YEARMONTH; 189 } 190 191 throw new IllegalStateException ( 193 "javax.xml.datatype.Duration#getXMLSchemaType():" 194 + " this Duration does not match one of the XML Schema date/time datatypes:" 195 + " year set = " + yearSet 196 + " month set = " + monthSet 197 + " day set = " + daySet 198 + " hour set = " + hourSet 199 + " minute set = " + minuteSet 200 + " second set = " + secondSet 201 ); 202 } 203 204 211 public abstract int getSign(); 212 213 225 public int getYears() { 226 return getField(DatatypeConstants.YEARS).intValue(); 227 } 228 229 238 public int getMonths() { 239 return getField(DatatypeConstants.MONTHS).intValue(); 240 } 241 242 251 public int getDays() { 252 return getField(DatatypeConstants.DAYS).intValue(); 253 } 254 255 265 public int getHours() { 266 return getField(DatatypeConstants.HOURS).intValue(); 267 } 268 269 279 public int getMinutes() { 280 return getField(DatatypeConstants.MINUTES).intValue(); 281 } 282 283 294 public int getSeconds() { 295 return getField(DatatypeConstants.SECONDS).intValue(); 296 } 297 298 328 public long getTimeInMillis(final Calendar startInstant) { 329 Calendar cal = (Calendar ) startInstant.clone(); 330 addTo(cal); 331 return getCalendarTimeInMillis(cal) 332 - getCalendarTimeInMillis(startInstant); 333 } 334 335 366 public long getTimeInMillis(final Date startInstant) { 367 Calendar cal = new GregorianCalendar (); 368 cal.setTime(startInstant); 369 this.addTo(cal); 370 return getCalendarTimeInMillis(cal) - startInstant.getTime(); 371 } 372 373 396 public abstract Number getField(final DatatypeConstants.Field field); 397 398 413 public abstract boolean isSet(final DatatypeConstants.Field field); 414 415 465 public abstract Duration add(final Duration rhs); 466 467 506 public abstract void addTo(Calendar calendar); 507 508 529 public void addTo(Date date) { 530 531 if (date == null) { 533 throw new NullPointerException ( 534 "Cannot call " 535 + this.getClass().getName() 536 + "#addTo(Date date) with date == null." 537 ); 538 } 539 540 Calendar cal = new GregorianCalendar (); 541 cal.setTime(date); 542 this.addTo(cal); 543 date.setTime(getCalendarTimeInMillis(cal)); 544 } 545 546 595 public Duration subtract(final Duration rhs) { 596 return add(rhs.negate()); 597 } 598 599 615 public Duration multiply(int factor) { 616 return multiply(new BigDecimal (String.valueOf(factor))); 617 } 618 619 666 public abstract Duration multiply(final BigDecimal factor); 667 668 680 public abstract Duration negate(); 681 682 712 public abstract Duration normalizeWith(final Calendar startTimeInstant); 713 714 744 public abstract int compare(final Duration duration); 745 746 772 public boolean isLongerThan(final Duration duration) { 773 return compare(duration) == DatatypeConstants.GREATER; 774 } 775 776 794 public boolean isShorterThan(final Duration duration) { 795 return compare(duration) == DatatypeConstants.LESSER; 796 } 797 798 836 public boolean equals(final Object duration) { 837 if (duration == null) { 838 throw new NullPointerException (); 839 } 840 841 if (!(duration instanceof Duration )) { 842 return false; 843 } 844 845 return compare((Duration ) duration) == DatatypeConstants.EQUAL; 846 } 847 848 853 public abstract int hashCode(); 854 855 869 public String toString() { 870 871 StringBuffer buf = new StringBuffer (); 872 873 if (getSign() < 0) { 874 buf.append('-'); 875 } 876 buf.append('P'); 877 878 BigInteger years = (BigInteger ) getField(DatatypeConstants.YEARS); 879 if (years != null) { 880 buf.append(years + "Y"); 881 } 882 883 BigInteger months = (BigInteger ) getField(DatatypeConstants.MONTHS); 884 if (months != null) { 885 buf.append(months + "M"); 886 } 887 888 BigInteger days = (BigInteger ) getField(DatatypeConstants.DAYS); 889 if (days != null) { 890 buf.append(days + "D"); 891 } 892 893 BigInteger hours = (BigInteger ) getField(DatatypeConstants.HOURS); 894 BigInteger minutes = (BigInteger ) getField(DatatypeConstants.MINUTES); 895 BigDecimal seconds = (BigDecimal ) getField(DatatypeConstants.SECONDS); 896 if (hours != null || minutes != null || seconds != null) { 897 buf.append('T'); 898 if (hours != null) { 899 buf.append(hours + "H"); 900 } 901 if (minutes != null) { 902 buf.append(minutes + "M"); 903 } 904 if (seconds != null) { 905 buf.append(toString(seconds) + "S"); 906 } 907 } 908 909 return buf.toString(); 910 } 911 912 922 private String toString(BigDecimal bd) { 923 String intString = bd.unscaledValue().toString(); 924 int scale = bd.scale(); 925 926 if (scale == 0) { 927 return intString; 928 } 929 930 931 StringBuffer buf; 932 int insertionPoint = intString.length() - scale; 933 if (insertionPoint == 0) { 934 return "0." + intString; 935 } else if (insertionPoint > 0) { 936 buf = new StringBuffer (intString); 937 buf.insert(insertionPoint, '.'); 938 } else { 939 buf = new StringBuffer (3 - insertionPoint + intString.length()); 940 buf.append("0."); 941 for (int i = 0; i < -insertionPoint; i++) { 942 buf.append('0'); 943 } 944 buf.append(intString); 945 } 946 return buf.toString(); 947 } 948 949 950 961 private static long getCalendarTimeInMillis(final Calendar cal) { 962 return cal.getTime().getTime(); 963 } 964 } 965 966 | Popular Tags |