| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import java.text.DecimalFormat; |
| 4 |
import java.text.DecimalFormatSymbols; |
| 5 |
import java.text.NumberFormat; |
| 6 |
import java.time.DateTimeException; |
| 7 |
import java.time.ZoneOffset; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Locale; |
| 10 |
import java.util.regex.Matcher; |
| 11 |
import java.util.regex.Pattern; |
| 12 |
|
| 13 |
import ezvcard.Messages; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public final class PartialDate { |
| 70 |
private static final int YEAR = 0; |
| 71 |
private static final int MONTH = 1; |
| 72 |
private static final int DATE = 2; |
| 73 |
private static final int HOUR = 3; |
| 74 |
private static final int MINUTE = 4; |
| 75 |
private static final int SECOND = 5; |
| 76 |
private static final int OFFSET_HOUR = 6; |
| 77 |
private static final int OFFSET_MINUTE = 7; |
| 78 |
|
| 79 |
|
| 80 |
private static final Format dateFormats[] = new Format[] { |
| 81 |
new Format("(\\d{4})", YEAR), |
| 82 |
new Format("(\\d{4})-(\\d{2})", YEAR, MONTH), |
| 83 |
new Format("(\\d{4})-?(\\d{2})-?(\\d{2})", YEAR, MONTH, DATE), |
| 84 |
new Format("--(\\d{2})-?(\\d{2})", MONTH, DATE), |
| 85 |
new Format("--(\\d{2})", MONTH), |
| 86 |
new Format("---(\\d{2})", DATE) |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
private static final String offsetRegex = "(([-+]\\d{1,2}):?(\\d{2})?)?"; |
| 91 |
|
| 92 |
|
| 93 |
private static final Format timeFormats[] = new Format[] { |
| 94 |
new Format("(\\d{2})" + offsetRegex, HOUR, null, OFFSET_HOUR, OFFSET_MINUTE), |
| 95 |
new Format("(\\d{2}):?(\\d{2})" + offsetRegex, HOUR, MINUTE, null, OFFSET_HOUR, OFFSET_MINUTE), |
| 96 |
new Format("(\\d{2}):?(\\d{2}):?(\\d{2})" + offsetRegex, HOUR, MINUTE, SECOND, null, OFFSET_HOUR, OFFSET_MINUTE), |
| 97 |
new Format("-(\\d{2}):?(\\d{2})" + offsetRegex, MINUTE, SECOND, null, OFFSET_HOUR, OFFSET_MINUTE), |
| 98 |
new Format("-(\\d{2})" + offsetRegex, MINUTE, null, OFFSET_HOUR, OFFSET_MINUTE), |
| 99 |
new Format("--(\\d{2})" + offsetRegex, SECOND, null, OFFSET_HOUR, OFFSET_MINUTE) |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
private final Integer[] components; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private PartialDate(Integer[] components) { |
| 109 |
this.components = components; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static Builder builder() { |
| 117 |
return new Builder(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static Builder builder(PartialDate orig) { |
| 126 |
return new Builder(orig); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public static PartialDate parse(String string) { |
| 137 |
int t = string.indexOf('T'); |
| 138 |
String beforeT, afterT; |
| 139 |
if (t < 0) { |
| 140 |
beforeT = string; |
| 141 |
afterT = null; |
| 142 |
} else { |
| 143 |
beforeT = string.substring(0, t); |
| 144 |
afterT = (t < string.length() - 1) ? string.substring(t + 1) : null; |
| 145 |
} |
| 146 |
|
| 147 |
Builder builder = new Builder(); |
| 148 |
boolean success; |
| 149 |
if (afterT == null) { |
| 150 |
|
| 151 |
success = parseDate(beforeT, builder) || parseTime(beforeT, builder); |
| 152 |
} else if (beforeT.isEmpty()) { |
| 153 |
|
| 154 |
success = parseTime(afterT, builder); |
| 155 |
} else { |
| 156 |
|
| 157 |
success = parseDate(beforeT, builder) && parseTime(afterT, builder); |
| 158 |
} |
| 159 |
|
| 160 |
if (!success) { |
| 161 |
throw Messages.INSTANCE.getIllegalArgumentException(36, string); |
| 162 |
} |
| 163 |
return builder.build(); |
| 164 |
} |
| 165 |
|
| 166 |
private static boolean parseDate(String value, Builder builder) { |
| 167 |
return parseFormats(value, builder, dateFormats); |
| 168 |
} |
| 169 |
|
| 170 |
private static boolean parseTime(String value, Builder builder) { |
| 171 |
return parseFormats(value, builder, timeFormats); |
| 172 |
} |
| 173 |
|
| 174 |
private static boolean parseFormats(String value, Builder builder, Format formats[]) { |
| 175 |
for (Format regex : formats) { |
| 176 |
if (regex.parse(builder, value)) { |
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public Integer getYear() { |
| 188 |
return components[YEAR]; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private boolean hasYear() { |
| 196 |
return getYear() != null; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public Integer getMonth() { |
| 204 |
return components[MONTH]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private boolean hasMonth() { |
| 212 |
return getMonth() != null; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public Integer getDate() { |
| 220 |
return components[DATE]; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private boolean hasDate() { |
| 228 |
return getDate() != null; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public Integer getHour() { |
| 236 |
return components[HOUR]; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private boolean hasHour() { |
| 244 |
return getHour() != null; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public Integer getMinute() { |
| 252 |
return components[MINUTE]; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
private boolean hasMinute() { |
| 260 |
return getMinute() != null; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public Integer getSecond() { |
| 268 |
return components[SECOND]; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
private boolean hasSecond() { |
| 276 |
return getSecond() != null; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public ZoneOffset getUtcOffset() { |
| 284 |
Integer hour = components[OFFSET_HOUR]; |
| 285 |
if (hour == null) { |
| 286 |
return null; |
| 287 |
} |
| 288 |
|
| 289 |
Integer minute = components[OFFSET_MINUTE]; |
| 290 |
if (minute == null) { |
| 291 |
return ZoneOffset.ofHours(hour); |
| 292 |
} |
| 293 |
|
| 294 |
return ZoneOffset.ofHoursMinutes(hour, minute); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public boolean hasDateComponent() { |
| 302 |
return hasYear() || hasMonth() || hasDate(); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public boolean hasTimeComponent() { |
| 310 |
return hasHour() || hasMinute() || hasSecond(); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public String toISO8601(boolean extended) { |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
final int maxPossibleLength = 25; |
| 328 |
|
| 329 |
StringBuilder sb = new StringBuilder(maxPossibleLength); |
| 330 |
NumberFormat nf = new DecimalFormat("00", DecimalFormatSymbols.getInstance(Locale.ROOT)); |
| 331 |
|
| 332 |
String yearStr = hasYear() ? getYear().toString() : null; |
| 333 |
String monthStr = hasMonth() ? nf.format(getMonth()) : null; |
| 334 |
String dateStr = hasDate() ? nf.format(getDate()) : null; |
| 335 |
|
| 336 |
String dash = extended ? "-" : ""; |
| 337 |
if (hasYear() && !hasMonth() && !hasDate()) { |
| 338 |
sb.append(yearStr); |
| 339 |
} else if (!hasYear() && hasMonth() && !hasDate()) { |
| 340 |
sb.append("--").append(monthStr); |
| 341 |
} else if (!hasYear() && !hasMonth() && hasDate()) { |
| 342 |
sb.append("---").append(dateStr); |
| 343 |
} else if (hasYear() && hasMonth() && !hasDate()) { |
| 344 |
sb.append(yearStr).append("-").append(monthStr); |
| 345 |
} else if (!hasYear() && hasMonth() && hasDate()) { |
| 346 |
sb.append("--").append(monthStr).append(dash).append(dateStr); |
| 347 |
} else if (hasYear() && !hasMonth() && hasDate()) { |
| 348 |
|
| 349 |
throw new IllegalStateException(Messages.INSTANCE.getExceptionMessage(38)); |
| 350 |
} else if (hasYear() && hasMonth() && hasDate()) { |
| 351 |
sb.append(yearStr).append(dash).append(monthStr).append(dash).append(dateStr); |
| 352 |
} |
| 353 |
|
| 354 |
if (hasTimeComponent()) { |
| 355 |
sb.append('T'); |
| 356 |
|
| 357 |
String hourStr = hasHour() ? nf.format(getHour()) : null; |
| 358 |
String minuteStr = hasMinute() ? nf.format(getMinute()) : null; |
| 359 |
String secondStr = hasSecond() ? nf.format(getSecond()) : null; |
| 360 |
|
| 361 |
dash = extended ? ":" : ""; |
| 362 |
if (hasHour() && !hasMinute() && !hasSecond()) { |
| 363 |
sb.append(hourStr); |
| 364 |
} else if (!hasHour() && hasMinute() && !hasSecond()) { |
| 365 |
sb.append("-").append(minuteStr); |
| 366 |
} else if (!hasHour() && !hasMinute() && hasSecond()) { |
| 367 |
sb.append("--").append(secondStr); |
| 368 |
} else if (hasHour() && hasMinute() && !hasSecond()) { |
| 369 |
sb.append(hourStr).append(dash).append(minuteStr); |
| 370 |
} else if (!hasHour() && hasMinute() && hasSecond()) { |
| 371 |
sb.append("-").append(minuteStr).append(dash).append(secondStr); |
| 372 |
} else if (hasHour() && !hasMinute() && hasSecond()) { |
| 373 |
|
| 374 |
throw new IllegalStateException(Messages.INSTANCE.getExceptionMessage(39)); |
| 375 |
} else if (hasHour() && hasMinute() && hasSecond()) { |
| 376 |
sb.append(hourStr).append(dash).append(minuteStr).append(dash).append(secondStr); |
| 377 |
} |
| 378 |
|
| 379 |
Integer offsetHour = components[OFFSET_HOUR]; |
| 380 |
if (offsetHour != null) { |
| 381 |
Integer offsetMinute = components[OFFSET_MINUTE]; |
| 382 |
String offsetHourStr = nf.format(Math.abs(offsetHour)); |
| 383 |
String offsetMinuteStr = nf.format(Math.abs(offsetMinute)); |
| 384 |
|
| 385 |
sb.append((offsetHour < 0 || offsetMinute < 0) ? '-' : '+'); |
| 386 |
sb.append(offsetHourStr).append(dash).append(offsetMinuteStr); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
return sb.toString(); |
| 391 |
} |
| 392 |
|
| 393 |
@Override |
| 394 |
public int hashCode() { |
| 395 |
final int prime = 31; |
| 396 |
int result = 1; |
| 397 |
result = prime * result + Arrays.hashCode(components); |
| 398 |
return result; |
| 399 |
} |
| 400 |
|
| 401 |
@Override |
| 402 |
public boolean equals(Object obj) { |
| 403 |
if (this == obj) return true; |
| 404 |
if (obj == null) return false; |
| 405 |
if (getClass() != obj.getClass()) return false; |
| 406 |
PartialDate other = (PartialDate) obj; |
| 407 |
if (!Arrays.equals(components, other.components)) return false; |
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public String toString() { |
| 413 |
return toISO8601(true); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
private static class Format { |
| 420 |
private Pattern regex; |
| 421 |
private Integer[] componentIndexes; |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public Format(String regex, Integer... componentIndexes) { |
| 430 |
this.regex = Pattern.compile('^' + regex + '$'); |
| 431 |
this.componentIndexes = componentIndexes; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public boolean parse(Builder builder, String value) { |
| 443 |
Matcher m = regex.matcher(value); |
| 444 |
if (!m.find()) { |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
boolean offsetPositive = false; |
| 458 |
Integer offsetHour = null, offsetMinute = null; |
| 459 |
|
| 460 |
for (int i = 0; i < componentIndexes.length; i++) { |
| 461 |
Integer index = componentIndexes[i]; |
| 462 |
if (index == null) { |
| 463 |
continue; |
| 464 |
} |
| 465 |
|
| 466 |
int group = i + 1; |
| 467 |
String groupStr = m.group(group); |
| 468 |
if (groupStr != null) { |
| 469 |
boolean startsWithPlus = groupStr.startsWith("+"); |
| 470 |
if (startsWithPlus) { |
| 471 |
groupStr = groupStr.substring(1); |
| 472 |
} |
| 473 |
|
| 474 |
int component = Integer.parseInt(groupStr); |
| 475 |
if (index == YEAR) { |
| 476 |
builder.year(component); |
| 477 |
} else if (index == MONTH) { |
| 478 |
builder.month(component); |
| 479 |
} else if (index == DATE) { |
| 480 |
builder.date(component); |
| 481 |
} else if (index == HOUR) { |
| 482 |
builder.hour(component); |
| 483 |
} else if (index == MINUTE) { |
| 484 |
builder.minute(component); |
| 485 |
} else if (index == SECOND) { |
| 486 |
builder.second(component); |
| 487 |
} else if (index == OFFSET_HOUR) { |
| 488 |
offsetHour = component; |
| 489 |
offsetPositive = startsWithPlus; |
| 490 |
} else if (index == OFFSET_MINUTE) { |
| 491 |
offsetMinute = component; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
if (offsetHour != null) { |
| 497 |
if (offsetMinute == null) { |
| 498 |
offsetMinute = 0; |
| 499 |
} |
| 500 |
if (!offsetPositive) { |
| 501 |
offsetMinute *= -1; |
| 502 |
} |
| 503 |
|
| 504 |
ZoneOffset offset; |
| 505 |
try { |
| 506 |
offset = ZoneOffset.ofHoursMinutes(offsetHour, offsetMinute); |
| 507 |
} catch (DateTimeException e) { |
| 508 |
throw new IllegalArgumentException(e); |
| 509 |
} |
| 510 |
builder.offset(offset); |
| 511 |
} |
| 512 |
|
| 513 |
return true; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public static class Builder { |
| 522 |
private final Integer[] components; |
| 523 |
|
| 524 |
public Builder() { |
| 525 |
components = new Integer[8]; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public Builder(PartialDate original) { |
| 532 |
components = original.components.clone(); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public Builder year(Integer year) { |
| 541 |
components[YEAR] = year; |
| 542 |
return this; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public Builder month(Integer month) { |
| 553 |
if (month != null && (month < 1 || month > 12)) { |
| 554 |
throw Messages.INSTANCE.getIllegalArgumentException(37, "Month", 1, 12); |
| 555 |
} |
| 556 |
|
| 557 |
components[MONTH] = month; |
| 558 |
return this; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public Builder date(Integer date) { |
| 569 |
if (date != null && (date < 1 || date > 31)) { |
| 570 |
throw Messages.INSTANCE.getIllegalArgumentException(37, "Date", 1, 31); |
| 571 |
} |
| 572 |
|
| 573 |
components[DATE] = date; |
| 574 |
return this; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public Builder hour(Integer hour) { |
| 585 |
if (hour != null && (hour < 0 || hour > 23)) { |
| 586 |
throw Messages.INSTANCE.getIllegalArgumentException(37, "Hour", 0, 23); |
| 587 |
} |
| 588 |
|
| 589 |
components[HOUR] = hour; |
| 590 |
return this; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
public Builder minute(Integer minute) { |
| 601 |
if (minute != null && (minute < 0 || minute > 59)) { |
| 602 |
throw Messages.INSTANCE.getIllegalArgumentException(37, "Minute", 0, 59); |
| 603 |
} |
| 604 |
|
| 605 |
components[MINUTE] = minute; |
| 606 |
return this; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
public Builder second(Integer second) { |
| 617 |
if (second != null && (second < 0 || second > 59)) { |
| 618 |
throw Messages.INSTANCE.getIllegalArgumentException(37, "Second", 0, 59); |
| 619 |
} |
| 620 |
|
| 621 |
components[SECOND] = second; |
| 622 |
return this; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
public Builder offset(ZoneOffset offset) { |
| 631 |
if (offset == null) { |
| 632 |
components[OFFSET_HOUR] = null; |
| 633 |
components[OFFSET_MINUTE] = null; |
| 634 |
} else { |
| 635 |
components[OFFSET_HOUR] = offset.getTotalSeconds() / 3600; |
| 636 |
components[OFFSET_MINUTE] = (offset.getTotalSeconds() % 3600) / 60; |
| 637 |
} |
| 638 |
return this; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
public PartialDate build() { |
| 649 |
if (components[YEAR] != null && components[MONTH] == null && components[DATE] != null) { |
| 650 |
throw Messages.INSTANCE.getIllegalArgumentException(38); |
| 651 |
} |
| 652 |
if (components[HOUR] != null && components[MINUTE] == null && components[SECOND] != null) { |
| 653 |
throw Messages.INSTANCE.getIllegalArgumentException(39); |
| 654 |
} |
| 655 |
|
| 656 |
return new PartialDate(components); |
| 657 |
} |
| 658 |
} |
| 659 |
} |