1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.base.BasePeriod; 21 import org.joda.time.field.FieldUtils; 22 23 53 public class MutablePeriod 54 extends BasePeriod 55 implements ReadWritablePeriod, Cloneable , Serializable { 56 57 58 private static final long serialVersionUID = 3436451121567212165L; 59 60 63 public MutablePeriod() { 64 super(0L, null, null); 65 } 66 67 72 public MutablePeriod(PeriodType type) { 73 super(0L, type, null); 74 } 75 76 84 public MutablePeriod(int hours, int minutes, int seconds, int millis) { 85 super(0, 0, 0, 0, hours, minutes, seconds, millis, PeriodType.standard()); 86 } 87 88 100 public MutablePeriod(int years, int months, int weeks, int days, 101 int hours, int minutes, int seconds, int millis) { 102 super(years, months, weeks, days, hours, minutes, seconds, millis, PeriodType.standard()); 103 } 104 105 119 public MutablePeriod(int years, int months, int weeks, int days, 120 int hours, int minutes, int seconds, int millis, PeriodType type) { 121 super(years, months, weeks, days, hours, minutes, seconds, millis, type); 122 } 123 124 150 public MutablePeriod(long duration) { 151 super(duration, null, null); 152 } 153 154 170 public MutablePeriod(long duration, PeriodType type) { 171 super(duration, type, null); 172 } 173 174 191 public MutablePeriod(long duration, Chronology chronology) { 192 super(duration, null, chronology); 193 } 194 195 212 public MutablePeriod(long duration, PeriodType type, Chronology chronology) { 213 super(duration, type, chronology); 214 } 215 216 223 public MutablePeriod(long startInstant, long endInstant) { 224 super(startInstant, endInstant, null, null); 225 } 226 227 234 public MutablePeriod(long startInstant, long endInstant, PeriodType type) { 235 super(startInstant, endInstant, type, null); 236 } 237 238 246 public MutablePeriod(long startInstant, long endInstant, Chronology chrono) { 247 super(startInstant, endInstant, null, chrono); 248 } 249 250 258 public MutablePeriod(long startInstant, long endInstant, PeriodType type, Chronology chrono) { 259 super(startInstant, endInstant, type, chrono); 260 } 261 262 272 public MutablePeriod(ReadableInstant startInstant, ReadableInstant endInstant) { 273 super(startInstant, endInstant, null); 274 } 275 276 286 public MutablePeriod(ReadableInstant startInstant, ReadableInstant endInstant, PeriodType type) { 287 super(startInstant, endInstant, type); 288 } 289 290 296 public MutablePeriod(ReadableInstant startInstant, ReadableDuration duration) { 297 super(startInstant, duration, null); 298 } 299 300 307 public MutablePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) { 308 super(startInstant, duration, type); 309 } 310 311 317 public MutablePeriod(ReadableDuration duration, ReadableInstant endInstant) { 318 super(duration, endInstant, null); 319 } 320 321 328 public MutablePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) { 329 super(duration, endInstant, type); 330 } 331 332 340 public MutablePeriod(Object period) { 341 super(period, null, null); 342 } 343 344 353 public MutablePeriod(Object period, PeriodType type) { 354 super(period, type, null); 355 } 356 357 366 public MutablePeriod(Object period, Chronology chrono) { 367 super(period, null, chrono); 368 } 369 370 380 public MutablePeriod(Object period, PeriodType type, Chronology chrono) { 381 super(period, type, chrono); 382 } 383 384 388 public void clear() { 389 super.setValues(new int[size()]); 390 } 391 392 399 public void setValue(int index, int value) { 400 super.setValue(index, value); 401 } 402 403 412 public void set(DurationFieldType field, int value) { 413 super.setField(field, value); 414 } 415 416 422 public void setPeriod(ReadablePeriod period) { 423 super.setPeriod(period); 424 } 425 426 439 public void setPeriod(int years, int months, int weeks, int days, 440 int hours, int minutes, int seconds, int millis) { 441 super.setPeriod(years, months, weeks, days, hours, minutes, seconds, millis); 442 } 443 444 451 public void setPeriod(ReadableInterval interval) { 452 if (interval == null) { 453 setPeriod(0L); 454 } else { 455 Chronology chrono = DateTimeUtils.getChronology(interval.getChronology()); 456 setPeriod(interval.getStartMillis(), interval.getEndMillis(), chrono); 457 } 458 } 459 460 470 public void setPeriod(ReadableInstant start, ReadableInstant end) { 471 if (start == end) { 472 setPeriod(0L); 473 } else { 474 long startMillis = DateTimeUtils.getInstantMillis(start); 475 long endMillis = DateTimeUtils.getInstantMillis(end); 476 Chronology chrono = DateTimeUtils.getIntervalChronology(start, end); 477 setPeriod(startMillis, endMillis, chrono); 478 } 479 } 480 481 489 public void setPeriod(long startInstant, long endInstant) { 490 setPeriod(startInstant, endInstant, null); 491 } 492 493 501 public void setPeriod(long startInstant, long endInstant, Chronology chrono) { 502 chrono = DateTimeUtils.getChronology(chrono); 503 setValues(chrono.get(this, startInstant, endInstant)); 504 } 505 506 517 public void setPeriod(ReadableDuration duration) { 518 setPeriod(duration, null); 519 } 520 521 533 public void setPeriod(ReadableDuration duration, Chronology chrono) { 534 long durationMillis = DateTimeUtils.getDurationMillis(duration); 535 setPeriod(durationMillis, chrono); 536 } 537 538 549 public void setPeriod(long duration) { 550 setPeriod(duration, null); 551 } 552 553 564 public void setPeriod(long duration, Chronology chrono) { 565 chrono = DateTimeUtils.getChronology(chrono); 566 setValues(chrono.get(this, duration)); 567 } 568 569 579 public void add(DurationFieldType field, int value) { 580 super.addField(field, value); 581 } 582 583 591 public void add(ReadablePeriod period) { 592 super.addPeriod(period); 593 } 594 595 610 public void add(int years, int months, int weeks, int days, 611 int hours, int minutes, int seconds, int millis) { 612 setPeriod( 613 FieldUtils.safeAdd(getYears(), years), 614 FieldUtils.safeAdd(getMonths(), months), 615 FieldUtils.safeAdd(getWeeks(), weeks), 616 FieldUtils.safeAdd(getDays(), days), 617 FieldUtils.safeAdd(getHours(), hours), 618 FieldUtils.safeAdd(getMinutes(), minutes), 619 FieldUtils.safeAdd(getSeconds(), seconds), 620 FieldUtils.safeAdd(getMillis(), millis) 621 ); 622 } 623 624 631 public void add(ReadableInterval interval) { 632 if (interval != null) { 633 add(interval.toPeriod(getPeriodType())); 634 } 635 } 636 637 644 public void add(ReadableDuration duration) { 645 if (duration != null) { 646 add(new Period(duration.getMillis(), getPeriodType())); 647 } 648 } 649 650 661 public void add(long duration) { 662 add(new Period(duration, getPeriodType())); 663 } 664 665 677 public void add(long duration, Chronology chrono) { 678 add(new Period(duration, getPeriodType(), chrono)); 679 } 680 681 690 public void mergePeriod(ReadablePeriod period) { 691 super.mergePeriod(period); 692 } 693 694 700 public int getYears() { 701 return getPeriodType().getIndexedField(this, PeriodType.YEAR_INDEX); 702 } 703 704 709 public int getMonths() { 710 return getPeriodType().getIndexedField(this, PeriodType.MONTH_INDEX); 711 } 712 713 718 public int getWeeks() { 719 return getPeriodType().getIndexedField(this, PeriodType.WEEK_INDEX); 720 } 721 722 727 public int getDays() { 728 return getPeriodType().getIndexedField(this, PeriodType.DAY_INDEX); 729 } 730 731 737 public int getHours() { 738 return getPeriodType().getIndexedField(this, PeriodType.HOUR_INDEX); 739 } 740 741 746 public int getMinutes() { 747 return getPeriodType().getIndexedField(this, PeriodType.MINUTE_INDEX); 748 } 749 750 755 public int getSeconds() { 756 return getPeriodType().getIndexedField(this, PeriodType.SECOND_INDEX); 757 } 758 759 764 public int getMillis() { 765 return getPeriodType().getIndexedField(this, PeriodType.MILLI_INDEX); 766 } 767 768 775 public void setYears(int years) { 776 super.setField(DurationFieldType.years(), years); 777 } 778 779 786 public void addYears(int years) { 787 super.addField(DurationFieldType.years(), years); 788 } 789 790 797 public void setMonths(int months) { 798 super.setField(DurationFieldType.months(), months); 799 } 800 801 808 public void addMonths(int months) { 809 super.addField(DurationFieldType.months(), months); 810 } 811 812 819 public void setWeeks(int weeks) { 820 super.setField(DurationFieldType.weeks(), weeks); 821 } 822 823 830 public void addWeeks(int weeks) { 831 super.addField(DurationFieldType.weeks(), weeks); 832 } 833 834 841 public void setDays(int days) { 842 super.setField(DurationFieldType.days(), days); 843 } 844 845 852 public void addDays(int days) { 853 super.addField(DurationFieldType.days(), days); 854 } 855 856 863 public void setHours(int hours) { 864 super.setField(DurationFieldType.hours(), hours); 865 } 866 867 874 public void addHours(int hours) { 875 super.addField(DurationFieldType.hours(), hours); 876 } 877 878 885 public void setMinutes(int minutes) { 886 super.setField(DurationFieldType.minutes(), minutes); 887 } 888 889 896 public void addMinutes(int minutes) { 897 super.addField(DurationFieldType.minutes(), minutes); 898 } 899 900 907 public void setSeconds(int seconds) { 908 super.setField(DurationFieldType.seconds(), seconds); 909 } 910 911 918 public void addSeconds(int seconds) { 919 super.addField(DurationFieldType.seconds(), seconds); 920 } 921 922 929 public void setMillis(int millis) { 930 super.setField(DurationFieldType.millis(), millis); 931 } 932 933 940 public void addMillis(int millis) { 941 super.addField(DurationFieldType.millis(), millis); 942 } 943 944 951 public MutablePeriod copy() { 952 return (MutablePeriod) clone(); 953 } 954 955 960 public Object clone() { 961 try { 962 return super.clone(); 963 } catch (CloneNotSupportedException ex) { 964 throw new InternalError ("Clone error"); 965 } 966 } 967 968 } 969 | Popular Tags |