1 16 package org.joda.time.chrono; 17 18 import org.joda.time.Chronology; 19 import org.joda.time.DateTimeField; 20 import org.joda.time.DateTimeZone; 21 import org.joda.time.field.StrictDateTimeField; 22 23 33 public final class StrictChronology extends AssembledChronology { 34 35 36 private static final long serialVersionUID = 6633006628097111960L; 37 38 44 public static StrictChronology getInstance(Chronology base) { 45 if (base == null) { 46 throw new IllegalArgumentException ("Must supply a chronology"); 47 } 48 return new StrictChronology(base); 49 } 50 51 private transient Chronology iWithUTC; 52 53 58 private StrictChronology(Chronology base) { 59 super(base, null); 60 } 61 62 public Chronology withUTC() { 63 if (iWithUTC == null) { 64 if (getZone() == DateTimeZone.UTC) { 65 iWithUTC = this; 66 } else { 67 iWithUTC = StrictChronology.getInstance(getBase().withUTC()); 68 } 69 } 70 return iWithUTC; 71 } 72 73 public Chronology withZone(DateTimeZone zone) { 74 if (zone == null) { 75 zone = DateTimeZone.getDefault(); 76 } 77 if (zone == DateTimeZone.UTC) { 78 return withUTC(); 79 } 80 if (zone == getZone()) { 81 return this; 82 } 83 return StrictChronology.getInstance(getBase().withZone(zone)); 84 } 85 86 protected void assemble(Fields fields) { 87 fields.year = convertField(fields.year); 88 fields.yearOfEra = convertField(fields.yearOfEra); 89 fields.yearOfCentury = convertField(fields.yearOfCentury); 90 fields.centuryOfEra = convertField(fields.centuryOfEra); 91 fields.era = convertField(fields.era); 92 fields.dayOfWeek = convertField(fields.dayOfWeek); 93 fields.dayOfMonth = convertField(fields.dayOfMonth); 94 fields.dayOfYear = convertField(fields.dayOfYear); 95 fields.monthOfYear = convertField(fields.monthOfYear); 96 fields.weekOfWeekyear = convertField(fields.weekOfWeekyear); 97 fields.weekyear = convertField(fields.weekyear); 98 fields.weekyearOfCentury = convertField(fields.weekyearOfCentury); 99 100 fields.millisOfSecond = convertField(fields.millisOfSecond); 101 fields.millisOfDay = convertField(fields.millisOfDay); 102 fields.secondOfMinute = convertField(fields.secondOfMinute); 103 fields.secondOfDay = convertField(fields.secondOfDay); 104 fields.minuteOfHour = convertField(fields.minuteOfHour); 105 fields.minuteOfDay = convertField(fields.minuteOfDay); 106 fields.hourOfDay = convertField(fields.hourOfDay); 107 fields.hourOfHalfday = convertField(fields.hourOfHalfday); 108 fields.clockhourOfDay = convertField(fields.clockhourOfDay); 109 fields.clockhourOfHalfday = convertField(fields.clockhourOfHalfday); 110 fields.halfdayOfDay = convertField(fields.halfdayOfDay); 111 } 112 113 private static final DateTimeField convertField(DateTimeField field) { 114 return StrictDateTimeField.getInstance(field); 115 } 116 117 126 public boolean equals(Object obj) { 127 if (this == obj) { 128 return true; 129 } 130 if (obj instanceof StrictChronology == false) { 131 return false; 132 } 133 StrictChronology chrono = (StrictChronology) obj; 134 return getBase().equals(chrono.getBase()); 135 } 136 137 143 public int hashCode() { 144 return 352831696 + getBase().hashCode() * 7; 145 } 146 147 152 public String toString() { 153 return "StrictChronology[" + getBase().toString() + ']'; 154 } 155 156 } 157 | Popular Tags |