1 package org.jbpm.bpel.xml.util; 2 3 import java.util.Calendar ; 4 import java.util.Date ; 5 import java.util.GregorianCalendar ; 6 import java.util.regex.Matcher ; 7 import java.util.regex.Pattern ; 8 9 15 public class Duration { 16 17 private short year; 18 private short month; 19 private short day; 20 private short hour; 21 private short minute; 22 private short second; 23 private short millis; 24 25 private boolean negative; 26 27 private static final Pattern durationPattern = Pattern.compile( 28 "(-)?" + "P" + "(?:(\\p{Digit}+)Y)?" + "(?:(\\p{Digit}+)M)?" + "(?:(\\p{Digit}+)D)?" + "(?:T" + "(?:(\\p{Digit}+)H)?" + "(?:(\\p{Digit}+)M)?" + "(?:" + "(\\p{Digit}+)" + "(?:\\." + "(\\p{Digit}{1,3})" + "\\p{Digit}*" + ")?" + "S)?" + ")?"); 45 public Duration() { 46 } 47 48 public Duration(int year, int month, int day, 49 int hour, int minute, int second, int millis) { 50 setYear((short) year); 51 setMonth((short) month); 52 setDay((short) day); 53 setHour((short) hour); 54 setMinute((short) minute); 55 setSecond((short) second); 56 setMillis((short) millis); 57 } 58 59 public boolean isNegative() { 60 return negative; 61 } 62 63 public void setNegative(boolean negative) { 64 this.negative = negative; 65 } 66 67 public short getYear() { 68 return year; 69 } 70 71 public void setYear(short year) { 72 this.year = year; 73 } 74 75 public short getMonth() { 76 return month; 77 } 78 79 public void setMonth(short month) { 80 this.month = month; 81 } 82 83 public short getDay() { 84 return day; 85 } 86 87 public void setDay(short day) { 88 this.day = day; 89 } 90 91 public short getHour() { 92 return hour; 93 } 94 95 public void setHour(short hour) { 96 this.hour = hour; 97 } 98 99 public short getMinute() { 100 return minute; 101 } 102 103 public void setMinute(short minute) { 104 this.minute = minute; 105 } 106 107 public short getSecond() { 108 return second; 109 } 110 111 public void setSecond(short second) { 112 this.second = second; 113 } 114 115 public short getMillis() { 116 return millis; 117 } 118 119 public void setMillis(short millis) { 120 this.millis = millis; 121 } 122 123 public Date add(Date dateTime) { 124 Calendar calendar = Calendar.getInstance(); 125 calendar.setTime(dateTime); 126 127 calendar.add(Calendar.YEAR, year); 128 calendar.add(Calendar.MONTH, month); 129 calendar.add(Calendar.DAY_OF_MONTH, day); 130 calendar.add(Calendar.HOUR_OF_DAY, hour); 131 calendar.add(Calendar.MINUTE, minute); 132 calendar.add(Calendar.SECOND, second); 133 calendar.add(Calendar.MILLISECOND, millis); 134 135 return calendar.getTime(); 136 } 137 138 public boolean equals(Object obj) { 139 boolean equals = false; 140 141 if (obj instanceof Duration) { 142 Duration d = (Duration) obj; 143 equals = 144 year == d.year && month == d.month && day == d.day && 145 hour == d.hour && minute == d.minute && second == d.second && 146 millis == d.millis && negative == d.negative; 147 } 148 return equals; 149 } 150 151 public int hashCode() { 152 return year ^ month ^ day ^ hour ^ minute ^ second ^ millis ^ (negative ? 1231 : 1237); 153 } 154 155 public String toString() { 156 StringBuffer literal = new StringBuffer (); 157 158 if (negative) 159 literal.append('-'); 160 literal.append('P'); 161 162 if (year != 0) 163 literal.append(year).append('Y'); 164 if (month != 0) 165 literal.append(month).append('M'); 166 if (day != 0) 167 literal.append(day).append('D'); 168 169 if (hour != 0 || minute != 0 || second != 0 || millis != 0) { 170 literal.append('T'); 171 172 if (hour != 0) 173 literal.append(hour).append('H'); 174 if (minute != 0) 175 literal.append(minute).append('M'); 176 177 if (second != 0) { 178 literal.append(second); 179 180 if (millis != 0) 181 literal.append('.').append(formatDecimal(millis, 3)); 182 183 literal.append('S'); 184 } 185 else if (millis != 0) 186 literal.append('0').append(formatDecimal(millis, 3)).append('S'); 187 } 188 return literal.toString(); 189 } 190 191 public static Duration parseDuration(String literal) { 192 Matcher matcher = durationPattern.matcher(literal); 193 Duration duration = null; 194 if (matcher.matches()) { 195 duration = new Duration(); 196 if (matcher.group(1) != null) 198 duration.setNegative(true); 199 String group = matcher.group(2); 201 if (group != null) 202 duration.setYear(Short.parseShort(group)); 203 group = matcher.group(3); 205 if (group != null) 206 duration.setMonth(Short.parseShort(group)); 207 group = matcher.group(4); 209 if (group != null) 210 duration.setDay(Short.parseShort(group)); 211 group = matcher.group(5); 213 if (group != null) 214 duration.setHour(Short.parseShort(group)); 215 group = matcher.group(6); 217 if (group != null) 218 duration.setMinute(Short.parseShort(group)); 219 group = matcher.group(7); 221 if (group != null) 222 duration.setSecond(Short.parseShort(group)); 223 group = matcher.group(8); 225 if (group != null) 226 duration.setMillis(parseDecimal(group, 3)); 227 } 228 return duration; 229 } 230 231 private static short parseDecimal(String literal, int digitCount) { 232 short number = Short.parseShort(literal); 233 234 for (int i = 0, n = digitCount - literal.length(); i < n; i++) 235 number *= 10; 236 237 return number; 238 } 239 240 private static String formatDecimal(short number, int digitCount) { 241 String literal = Short.toString(number); 242 243 if (literal.length() < digitCount) { 244 StringBuffer buffer = new StringBuffer (); 245 246 for (int i = 0, n = digitCount - literal.length(); i < n; i++) { 247 buffer.append('0'); 248 } 249 literal = buffer.append(literal).toString(); 250 } 251 252 int i = digitCount; 253 while (--i > 0 && literal.charAt(i) == '0') 254 ; 256 return literal.substring(0, i + 1); 257 } 258 259 public Date getTime() { 260 Calendar calendar = new GregorianCalendar (year, month, day, hour, minute, second); 261 calendar.add(Calendar.MILLISECOND, millis); 262 return calendar.getTime(); 263 } 264 } 265 | Popular Tags |