1 package org.quartz.impl.calendar; 2 3 import java.text.NumberFormat ; 4 import java.util.ArrayList ; 5 import java.util.Calendar ; 6 import java.util.StringTokenizer ; 7 import java.util.TimeZone ; 8 9 29 public class DailyCalendar extends BaseCalendar { 30 static final long serialVersionUID = -7561220099904944039L; 31 32 private static final String invalidHourOfDay = "Invalid hour of day: "; 33 private static final String invalidMinute = "Invalid minute: "; 34 private static final String invalidSecond = "Invalid second: "; 35 private static final String invalidMillis = "Invalid millis: "; 36 private static final String invalidTimeRange = "Invalid time range: "; 37 private static final String separator = " - "; 38 private static final long oneMillis = 1; 39 private static final String colon = ":"; 40 41 42 private String name; 43 44 private int rangeStartingHourOfDay; 45 private int rangeStartingMinute; 46 private int rangeStartingSecond; 47 private int rangeStartingMillis; 48 private int rangeEndingHourOfDay; 49 private int rangeEndingMinute; 50 private int rangeEndingSecond; 51 private int rangeEndingMillis; 52 53 private boolean invertTimeRange = false; 54 55 86 public DailyCalendar(String rangeStartingTime, 87 String rangeEndingTime) { 88 super(); 89 setTimeRange(rangeStartingTime, rangeEndingTime); 90 } 91 92 126 public DailyCalendar(org.quartz.Calendar baseCalendar, 127 String rangeStartingTime, 128 String rangeEndingTime) { 129 super(baseCalendar); 130 setTimeRange(rangeStartingTime, rangeEndingTime); 131 } 132 133 164 public DailyCalendar(int rangeStartingHourOfDay, 165 int rangeStartingMinute, 166 int rangeStartingSecond, 167 int rangeStartingMillis, 168 int rangeEndingHourOfDay, 169 int rangeEndingMinute, 170 int rangeEndingSecond, 171 int rangeEndingMillis) { 172 super(); 173 setTimeRange(rangeStartingHourOfDay, 174 rangeStartingMinute, 175 rangeStartingSecond, 176 rangeStartingMillis, 177 rangeEndingHourOfDay, 178 rangeEndingMinute, 179 rangeEndingSecond, 180 rangeEndingMillis); 181 } 182 183 219 public DailyCalendar(org.quartz.Calendar baseCalendar, 220 int rangeStartingHourOfDay, 221 int rangeStartingMinute, 222 int rangeStartingSecond, 223 int rangeStartingMillis, 224 int rangeEndingHourOfDay, 225 int rangeEndingMinute, 226 int rangeEndingSecond, 227 int rangeEndingMillis) { 228 super(baseCalendar); 229 setTimeRange(rangeStartingHourOfDay, 230 rangeStartingMinute, 231 rangeStartingSecond, 232 rangeStartingMillis, 233 rangeEndingHourOfDay, 234 rangeEndingMinute, 235 rangeEndingSecond, 236 rangeEndingMillis); 237 } 238 239 266 public DailyCalendar( 267 Calendar rangeStartingCalendar, 268 Calendar rangeEndingCalendar) { 269 super(); 270 setTimeRange(rangeStartingCalendar, rangeEndingCalendar); 271 } 272 273 303 public DailyCalendar(org.quartz.Calendar baseCalendar, 304 Calendar rangeStartingCalendar, 305 Calendar rangeEndingCalendar) { 306 super(baseCalendar); 307 setTimeRange(rangeStartingCalendar, rangeEndingCalendar); 308 } 309 310 339 public DailyCalendar(long rangeStartingTimeInMillis, 340 long rangeEndingTimeInMillis) { 341 super(); 342 setTimeRange(rangeStartingTimeInMillis, 343 rangeEndingTimeInMillis); 344 } 345 346 379 public DailyCalendar(org.quartz.Calendar baseCalendar, 380 long rangeStartingTimeInMillis, 381 long rangeEndingTimeInMillis) { 382 super(baseCalendar); 383 setTimeRange(rangeStartingTimeInMillis, 384 rangeEndingTimeInMillis); 385 } 386 387 410 public DailyCalendar(TimeZone timeZone, 411 long rangeStartingTimeInMillis, 412 long rangeEndingTimeInMillis) { 413 super(timeZone); 414 setTimeRange(rangeStartingTimeInMillis, 415 rangeEndingTimeInMillis); 416 } 417 418 445 public DailyCalendar(org.quartz.Calendar baseCalendar, 446 TimeZone timeZone, 447 long rangeStartingTimeInMillis, 448 long rangeEndingTimeInMillis) { 449 super(baseCalendar, timeZone); 450 setTimeRange(rangeStartingTimeInMillis, 451 rangeEndingTimeInMillis); 452 } 453 454 459 public DailyCalendar(String name, 460 String rangeStartingTime, 461 String rangeEndingTime) { 462 this(rangeStartingTime, rangeEndingTime); 463 this.name = name; 464 } 465 466 471 public DailyCalendar(String name, 472 org.quartz.Calendar baseCalendar, 473 String rangeStartingTime, 474 String rangeEndingTime) { 475 this(baseCalendar, rangeStartingTime, rangeEndingTime); 476 this.name = name; 477 } 478 479 484 public DailyCalendar(String name, 485 int rangeStartingHourOfDay, 486 int rangeStartingMinute, 487 int rangeStartingSecond, 488 int rangeStartingMillis, 489 int rangeEndingHourOfDay, 490 int rangeEndingMinute, 491 int rangeEndingSecond, 492 int rangeEndingMillis) { 493 this(rangeStartingHourOfDay, 494 rangeStartingMinute, 495 rangeStartingSecond, 496 rangeStartingMillis, 497 rangeEndingHourOfDay, 498 rangeEndingMinute, 499 rangeEndingSecond, 500 rangeEndingMillis); 501 this.name = name; 502 } 503 504 509 public DailyCalendar(String name, 510 org.quartz.Calendar baseCalendar, 511 int rangeStartingHourOfDay, 512 int rangeStartingMinute, 513 int rangeStartingSecond, 514 int rangeStartingMillis, 515 int rangeEndingHourOfDay, 516 int rangeEndingMinute, 517 int rangeEndingSecond, 518 int rangeEndingMillis) { 519 this(baseCalendar, 520 rangeStartingHourOfDay, 521 rangeStartingMinute, 522 rangeStartingSecond, 523 rangeStartingMillis, 524 rangeEndingHourOfDay, 525 rangeEndingMinute, 526 rangeEndingSecond, 527 rangeEndingMillis); 528 this.name = name; 529 } 530 531 536 public DailyCalendar(String name, 537 Calendar rangeStartingCalendar, 538 Calendar rangeEndingCalendar) { 539 this(rangeStartingCalendar, rangeEndingCalendar); 540 this.name = name; 541 } 542 543 548 public DailyCalendar(String name, 549 org.quartz.Calendar baseCalendar, 550 Calendar rangeStartingCalendar, 551 Calendar rangeEndingCalendar) { 552 this(baseCalendar, rangeStartingCalendar, rangeEndingCalendar); 553 this.name = name; 554 } 555 556 561 public DailyCalendar(String name, 562 long rangeStartingTimeInMillis, 563 long rangeEndingTimeInMillis) { 564 this(rangeStartingTimeInMillis, rangeEndingTimeInMillis); 565 this.name = name; 566 } 567 568 573 public DailyCalendar(String name, 574 org.quartz.Calendar baseCalendar, 575 long rangeStartingTimeInMillis, 576 long rangeEndingTimeInMillis) { 577 this(baseCalendar, rangeStartingTimeInMillis, rangeEndingTimeInMillis); 578 this.name = name; 579 } 580 581 586 public DailyCalendar(String name, 587 TimeZone timeZone, 588 long rangeStartingTimeInMillis, 589 long rangeEndingTimeInMillis) { 590 this(timeZone, 591 rangeStartingTimeInMillis, 592 rangeEndingTimeInMillis); 593 this.name = name; 594 } 595 596 601 public DailyCalendar(String name, 602 org.quartz.Calendar baseCalendar, 603 TimeZone timeZone, 604 long rangeStartingTimeInMillis, 605 long rangeEndingTimeInMillis) { 606 this(baseCalendar, 607 timeZone, 608 rangeStartingTimeInMillis, 609 rangeEndingTimeInMillis); 610 this.name = name; 611 } 612 613 620 public String getName() { 621 return name; 622 } 623 624 632 public boolean isTimeIncluded(long timeInMillis) { 633 if ((getBaseCalendar() != null) && 634 (getBaseCalendar().isTimeIncluded(timeInMillis) == false)) { 635 return false; 636 } 637 638 long startOfDayInMillis = getStartOfDayJavaCalendar(timeInMillis).getTime().getTime(); 639 long endOfDayInMillis = getEndOfDayJavaCalendar(timeInMillis).getTime().getTime(); 640 long timeRangeStartingTimeInMillis = 641 getTimeRangeStartingTimeInMillis(timeInMillis); 642 long timeRangeEndingTimeInMillis = 643 getTimeRangeEndingTimeInMillis(timeInMillis); 644 if (!invertTimeRange) { 645 return 646 ((timeInMillis > startOfDayInMillis && 647 timeInMillis < timeRangeStartingTimeInMillis) || 648 (timeInMillis > timeRangeEndingTimeInMillis && 649 timeInMillis < endOfDayInMillis)); 650 } else { 651 return ((timeInMillis >= timeRangeStartingTimeInMillis) && 652 (timeInMillis <= timeRangeEndingTimeInMillis)); 653 } 654 } 655 656 665 public long getNextIncludedTime(long timeInMillis) { 666 long nextIncludedTime = timeInMillis + oneMillis; 667 668 while (!isTimeIncluded(nextIncludedTime)) { 669 if (!invertTimeRange) { 670 if ((nextIncludedTime >= 677 getTimeRangeStartingTimeInMillis(nextIncludedTime)) && 678 (nextIncludedTime <= 679 getTimeRangeEndingTimeInMillis(nextIncludedTime))) { 680 681 nextIncludedTime = 682 getTimeRangeEndingTimeInMillis(nextIncludedTime) + 683 oneMillis; 684 } else if ((getBaseCalendar() != null) && 685 (!getBaseCalendar().isTimeIncluded(nextIncludedTime))){ 686 nextIncludedTime = 687 getBaseCalendar().getNextIncludedTime(nextIncludedTime); 688 } else { 689 nextIncludedTime++; 690 } 691 } else { 692 if (nextIncludedTime < 699 getTimeRangeStartingTimeInMillis(nextIncludedTime)) { 700 nextIncludedTime = 701 getTimeRangeStartingTimeInMillis(nextIncludedTime); 702 } else if (nextIncludedTime > 703 getTimeRangeEndingTimeInMillis(nextIncludedTime)) { 704 nextIncludedTime = getEndOfDayJavaCalendar(nextIncludedTime).getTime().getTime(); 706 nextIncludedTime += 1l; 707 } else if ((getBaseCalendar() != null) && 708 (!getBaseCalendar().isTimeIncluded(nextIncludedTime))){ 709 nextIncludedTime = 710 getBaseCalendar().getNextIncludedTime(nextIncludedTime); 711 } else { 712 nextIncludedTime++; 713 } 714 } 715 } 716 717 return nextIncludedTime; 718 } 719 720 729 public long getTimeRangeStartingTimeInMillis(long timeInMillis) { 730 Calendar rangeStartingTime = createJavaCalendar(timeInMillis); 731 rangeStartingTime.set(Calendar.HOUR_OF_DAY, rangeStartingHourOfDay); 732 rangeStartingTime.set(Calendar.MINUTE, rangeStartingMinute); 733 rangeStartingTime.set(Calendar.SECOND, rangeStartingSecond); 734 rangeStartingTime.set(Calendar.MILLISECOND, rangeStartingMillis); 735 return rangeStartingTime.getTime().getTime(); 736 } 737 738 747 public long getTimeRangeEndingTimeInMillis(long timeInMillis) { 748 Calendar rangeEndingTime = createJavaCalendar(timeInMillis); 749 rangeEndingTime.set(Calendar.HOUR_OF_DAY, rangeEndingHourOfDay); 750 rangeEndingTime.set(Calendar.MINUTE, rangeEndingMinute); 751 rangeEndingTime.set(Calendar.SECOND, rangeEndingSecond); 752 rangeEndingTime.set(Calendar.MILLISECOND, rangeEndingMillis); 753 return rangeEndingTime.getTime().getTime(); 754 } 755 756 762 public boolean getInvertTimeRange() { 763 return invertTimeRange; 764 } 765 766 772 public void setInvertTimeRange(boolean flag) { 773 this.invertTimeRange = flag; 774 } 775 776 782 public String toString() { 783 NumberFormat numberFormatter = NumberFormat.getNumberInstance(); 784 numberFormatter.setMaximumFractionDigits(0); 785 numberFormatter.setMinimumIntegerDigits(2); 786 StringBuffer buffer = new StringBuffer (); 787 if (name != null) { 788 buffer.append(name).append(": "); 789 } 790 buffer.append("base calendar: ["); 791 if (getBaseCalendar() != null) { 792 buffer.append(getBaseCalendar().toString()); 793 } else { 794 buffer.append("null"); 795 } 796 buffer.append("], time range: '"); 797 buffer.append(numberFormatter.format(rangeStartingHourOfDay)); 798 buffer.append(":"); 799 buffer.append(numberFormatter.format(rangeStartingMinute)); 800 buffer.append(":"); 801 buffer.append(numberFormatter.format(rangeStartingSecond)); 802 buffer.append(":"); 803 numberFormatter.setMinimumIntegerDigits(3); 804 buffer.append(numberFormatter.format(rangeStartingMillis)); 805 numberFormatter.setMinimumIntegerDigits(2); 806 buffer.append(" - "); 807 buffer.append(numberFormatter.format(rangeEndingHourOfDay)); 808 buffer.append(":"); 809 buffer.append(numberFormatter.format(rangeEndingMinute)); 810 buffer.append(":"); 811 buffer.append(numberFormatter.format(rangeEndingSecond)); 812 buffer.append(":"); 813 numberFormatter.setMinimumIntegerDigits(3); 814 buffer.append(numberFormatter.format(rangeEndingMillis)); 815 buffer.append("', inverted: " + invertTimeRange + "]"); 816 return buffer.toString(); 817 } 818 819 822 private String [] split(String string, String delim) { 823 ArrayList result = new ArrayList (); 824 825 StringTokenizer stringTokenizer = new StringTokenizer (string, delim); 826 while (stringTokenizer.hasMoreTokens()) { 827 result.add(stringTokenizer.nextToken()); 828 } 829 830 return (String [])result.toArray(new String [result.size()]); 831 } 832 833 842 private void setTimeRange(String rangeStartingTimeString, 843 String rangeEndingTimeString) { 844 String [] rangeStartingTime; 845 int rangeStartingHourOfDay; 846 int rangeStartingMinute; 847 int rangeStartingSecond; 848 int rangeStartingMillis; 849 850 String [] rangeEndingTime; 851 int rangeEndingHourOfDay; 852 int rangeEndingMinute; 853 int rangeEndingSecond; 854 int rangeEndingMillis; 855 856 rangeStartingTime = split(rangeStartingTimeString, colon); 857 858 if ((rangeStartingTime.length < 2) || (rangeStartingTime.length > 4)) { 859 throw new IllegalArgumentException ("Invalid time string '" + 860 rangeStartingTimeString + "'"); 861 } 862 863 rangeStartingHourOfDay = Integer.parseInt(rangeStartingTime[0]); 864 rangeStartingMinute = Integer.parseInt(rangeStartingTime[1]); 865 if (rangeStartingTime.length > 2) { 866 rangeStartingSecond = Integer.parseInt(rangeStartingTime[2]); 867 } else { 868 rangeStartingSecond = 0; 869 } 870 if (rangeStartingTime.length == 4) { 871 rangeStartingMillis = Integer.parseInt(rangeStartingTime[3]); 872 } else { 873 rangeStartingMillis = 0; 874 } 875 876 rangeEndingTime = split(rangeEndingTimeString, colon); 877 878 if ((rangeEndingTime.length < 2) || (rangeEndingTime.length > 4)) { 879 throw new IllegalArgumentException ("Invalid time string '" + 880 rangeEndingTimeString + "'"); 881 } 882 883 rangeEndingHourOfDay = Integer.parseInt(rangeEndingTime[0]); 884 rangeEndingMinute = Integer.parseInt(rangeEndingTime[1]); 885 if (rangeEndingTime.length > 2) { 886 rangeEndingSecond = Integer.parseInt(rangeEndingTime[2]); 887 } else { 888 rangeEndingSecond = 0; 889 } 890 if (rangeEndingTime.length == 4) { 891 rangeEndingMillis = Integer.parseInt(rangeEndingTime[3]); 892 } else { 893 rangeEndingMillis = 0; 894 } 895 896 setTimeRange(rangeStartingHourOfDay, 897 rangeStartingMinute, 898 rangeStartingSecond, 899 rangeStartingMillis, 900 rangeEndingHourOfDay, 901 rangeEndingMinute, 902 rangeEndingSecond, 903 rangeEndingMillis); 904 } 905 906 921 private void setTimeRange(int rangeStartingHourOfDay, 922 int rangeStartingMinute, 923 int rangeStartingSecond, 924 int rangeStartingMillis, 925 int rangeEndingHourOfDay, 926 int rangeEndingMinute, 927 int rangeEndingSecond, 928 int rangeEndingMillis) { 929 validate(rangeStartingHourOfDay, 930 rangeStartingMinute, 931 rangeStartingSecond, 932 rangeStartingMillis); 933 934 validate(rangeEndingHourOfDay, 935 rangeEndingMinute, 936 rangeEndingSecond, 937 rangeEndingMillis); 938 939 Calendar startCal = createJavaCalendar(); 940 startCal.set(Calendar.HOUR_OF_DAY, rangeStartingHourOfDay); 941 startCal.set(Calendar.MINUTE, rangeStartingMinute); 942 startCal.set(Calendar.SECOND, rangeStartingSecond); 943 startCal.set(Calendar.MILLISECOND, rangeStartingMillis); 944 945 Calendar endCal = createJavaCalendar(); 946 endCal.set(Calendar.HOUR_OF_DAY, rangeEndingHourOfDay); 947 endCal.set(Calendar.MINUTE, rangeEndingMinute); 948 endCal.set(Calendar.SECOND, rangeEndingSecond); 949 endCal.set(Calendar.MILLISECOND, rangeEndingMillis); 950 951 if (!startCal.before(endCal)) { 952 throw new IllegalArgumentException (invalidTimeRange + 953 rangeStartingHourOfDay + ":" + 954 rangeStartingMinute + ":" + 955 rangeStartingSecond + ":" + 956 rangeStartingMillis + separator + 957 rangeEndingHourOfDay + ":" + 958 rangeEndingMinute + ":" + 959 rangeEndingSecond + ":" + 960 rangeEndingMillis); 961 } 962 963 this.rangeStartingHourOfDay = rangeStartingHourOfDay; 964 this.rangeStartingMinute = rangeStartingMinute; 965 this.rangeStartingSecond = rangeStartingSecond; 966 this.rangeStartingMillis = rangeStartingMillis; 967 this.rangeEndingHourOfDay = rangeEndingHourOfDay; 968 this.rangeEndingMinute = rangeEndingMinute; 969 this.rangeEndingSecond = rangeEndingSecond; 970 this.rangeEndingMillis = rangeEndingMillis; 971 } 972 973 982 private void setTimeRange(Calendar rangeStartingCalendar, 983 Calendar rangeEndingCalendar) { 984 setTimeRange( 985 rangeStartingCalendar.get(Calendar.HOUR_OF_DAY), 986 rangeStartingCalendar.get(Calendar.MINUTE), 987 rangeStartingCalendar.get(Calendar.SECOND), 988 rangeStartingCalendar.get(Calendar.MILLISECOND), 989 rangeEndingCalendar.get(Calendar.HOUR_OF_DAY), 990 rangeEndingCalendar.get(Calendar.MINUTE), 991 rangeEndingCalendar.get(Calendar.SECOND), 992 rangeEndingCalendar.get(Calendar.MILLISECOND)); 993 } 994 995 1004 private void setTimeRange(long rangeStartingTime, 1005 long rangeEndingTime) { 1006 setTimeRange( 1007 createJavaCalendar(rangeStartingTime), 1008 createJavaCalendar(rangeEndingTime)); 1009 } 1010 1011 1020 private void validate(int hourOfDay, int minute, int second, int millis) { 1021 if (hourOfDay < 0 || hourOfDay > 23) { 1022 throw new IllegalArgumentException (invalidHourOfDay + hourOfDay); 1023 } 1024 if (minute < 0 || minute > 59) { 1025 throw new IllegalArgumentException (invalidMinute + minute); 1026 } 1027 if (second < 0 || second > 59) { 1028 throw new IllegalArgumentException (invalidSecond + second); 1029 } 1030 if (millis < 0 || millis > 999) { 1031 throw new IllegalArgumentException (invalidMillis + millis); 1032 } 1033 } 1034} | Popular Tags |