001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.chrono;
017:
018: import org.joda.time.Chronology;
019: import org.joda.time.DateTimeField;
020: import org.joda.time.DateTimeZone;
021: import org.joda.time.field.LenientDateTimeField;
022:
023: /**
024: * Wraps another Chronology, ensuring all the fields are lenient.
025: * <p>
026: * LenientChronology is thread-safe and immutable.
027: *
028: * @author Brian S O'Neill
029: * @since 1.0
030: * @see LenientDateTimeField
031: * @see StrictChronology
032: */
033: public final class LenientChronology extends AssembledChronology {
034:
035: /** Serialization lock */
036: private static final long serialVersionUID = -3148237568046877177L;
037:
038: /**
039: * Create a LenientChronology for any chronology.
040: *
041: * @param base the chronology to wrap
042: * @throws IllegalArgumentException if chronology is null
043: */
044: public static LenientChronology getInstance(Chronology base) {
045: if (base == null) {
046: throw new IllegalArgumentException(
047: "Must supply a chronology");
048: }
049: return new LenientChronology(base);
050: }
051:
052: private transient Chronology iWithUTC;
053:
054: /**
055: * Create a LenientChronology for any chronology.
056: *
057: * @param base the chronology to wrap
058: */
059: private LenientChronology(Chronology base) {
060: super (base, null);
061: }
062:
063: public Chronology withUTC() {
064: if (iWithUTC == null) {
065: if (getZone() == DateTimeZone.UTC) {
066: iWithUTC = this ;
067: } else {
068: iWithUTC = LenientChronology.getInstance(getBase()
069: .withUTC());
070: }
071: }
072: return iWithUTC;
073: }
074:
075: public Chronology withZone(DateTimeZone zone) {
076: if (zone == null) {
077: zone = DateTimeZone.getDefault();
078: }
079: if (zone == DateTimeZone.UTC) {
080: return withUTC();
081: }
082: if (zone == getZone()) {
083: return this ;
084: }
085: return LenientChronology.getInstance(getBase().withZone(zone));
086: }
087:
088: protected void assemble(Fields fields) {
089: fields.year = convertField(fields.year);
090: fields.yearOfEra = convertField(fields.yearOfEra);
091: fields.yearOfCentury = convertField(fields.yearOfCentury);
092: fields.centuryOfEra = convertField(fields.centuryOfEra);
093: fields.era = convertField(fields.era);
094: fields.dayOfWeek = convertField(fields.dayOfWeek);
095: fields.dayOfMonth = convertField(fields.dayOfMonth);
096: fields.dayOfYear = convertField(fields.dayOfYear);
097: fields.monthOfYear = convertField(fields.monthOfYear);
098: fields.weekOfWeekyear = convertField(fields.weekOfWeekyear);
099: fields.weekyear = convertField(fields.weekyear);
100: fields.weekyearOfCentury = convertField(fields.weekyearOfCentury);
101:
102: fields.millisOfSecond = convertField(fields.millisOfSecond);
103: fields.millisOfDay = convertField(fields.millisOfDay);
104: fields.secondOfMinute = convertField(fields.secondOfMinute);
105: fields.secondOfDay = convertField(fields.secondOfDay);
106: fields.minuteOfHour = convertField(fields.minuteOfHour);
107: fields.minuteOfDay = convertField(fields.minuteOfDay);
108: fields.hourOfDay = convertField(fields.hourOfDay);
109: fields.hourOfHalfday = convertField(fields.hourOfHalfday);
110: fields.clockhourOfDay = convertField(fields.clockhourOfDay);
111: fields.clockhourOfHalfday = convertField(fields.clockhourOfHalfday);
112: fields.halfdayOfDay = convertField(fields.halfdayOfDay);
113: }
114:
115: private final DateTimeField convertField(DateTimeField field) {
116: return LenientDateTimeField.getInstance(field, getBase());
117: }
118:
119: //-----------------------------------------------------------------------
120: /**
121: * A lenient chronology is only equal to a lenient chronology with the
122: * same base chronology.
123: *
124: * @param obj the object to compare to
125: * @return true if equal
126: * @since 1.4
127: */
128: public boolean equals(Object obj) {
129: if (this == obj) {
130: return true;
131: }
132: if (obj instanceof LenientChronology == false) {
133: return false;
134: }
135: LenientChronology chrono = (LenientChronology) obj;
136: return getBase().equals(chrono.getBase());
137: }
138:
139: /**
140: * A suitable hashcode for the chronology.
141: *
142: * @return the hashcode
143: * @since 1.4
144: */
145: public int hashCode() {
146: return 236548278 + getBase().hashCode() * 7;
147: }
148:
149: /**
150: * A debugging string for the chronology.
151: *
152: * @return the debugging string
153: */
154: public String toString() {
155: return "LenientChronology[" + getBase().toString() + ']';
156: }
157:
158: }
|