| 1 | /* |
| 2 | * Copyright 2001-2005 Stephen Colebourne |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 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.LenientDateTimeField; |
| 22 | |
| 23 | /** |
| 24 | * Wraps another Chronology, ensuring all the fields are lenient. |
| 25 | * <p> |
| 26 | * LenientChronology is thread-safe and immutable. |
| 27 | * |
| 28 | * @author Brian S O'Neill |
| 29 | * @since 1.0 |
| 30 | * @see LenientDateTimeField |
| 31 | * @see StrictChronology |
| 32 | */ |
| 33 | public final class LenientChronology extends AssembledChronology { |
| 34 | |
| 35 | /** Serialization lock */ |
| 36 | private static final long serialVersionUID = -3148237568046877177L; |
| 37 | |
| 38 | /** |
| 39 | * Create a LenientChronology for any chronology. |
| 40 | * |
| 41 | * @param base the chronology to wrap |
| 42 | * @throws IllegalArgumentException if chronology is null |
| 43 | */ |
| 44 | public static LenientChronology getInstance(Chronology base) { |
| 45 | if (base == null) { |
| 46 | throw new IllegalArgumentException("Must supply a chronology"); |
| 47 | } |
| 48 | return new LenientChronology(base); |
| 49 | } |
| 50 | |
| 51 | private transient Chronology iWithUTC; |
| 52 | |
| 53 | /** |
| 54 | * Create a LenientChronology for any chronology. |
| 55 | * |
| 56 | * @param base the chronology to wrap |
| 57 | */ |
| 58 | private LenientChronology(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 = LenientChronology.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 LenientChronology.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 final DateTimeField convertField(DateTimeField field) { |
| 114 | return LenientDateTimeField.getInstance(field, getBase()); |
| 115 | } |
| 116 | |
| 117 | //----------------------------------------------------------------------- |
| 118 | /** |
| 119 | * A lenient chronology is only equal to a lenient chronology with the |
| 120 | * same base chronology. |
| 121 | * |
| 122 | * @param obj the object to compare to |
| 123 | * @return true if equal |
| 124 | * @since 1.4 |
| 125 | */ |
| 126 | public boolean equals(Object obj) { |
| 127 | if (this == obj) { |
| 128 | return true; |
| 129 | } |
| 130 | if (obj instanceof LenientChronology == false) { |
| 131 | return false; |
| 132 | } |
| 133 | LenientChronology chrono = (LenientChronology) obj; |
| 134 | return getBase().equals(chrono.getBase()); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * A suitable hashcode for the chronology. |
| 139 | * |
| 140 | * @return the hashcode |
| 141 | * @since 1.4 |
| 142 | */ |
| 143 | public int hashCode() { |
| 144 | return 236548278 + getBase().hashCode() * 7; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * A debugging string for the chronology. |
| 149 | * |
| 150 | * @return the debugging string |
| 151 | */ |
| 152 | public String toString() { |
| 153 | return "LenientChronology[" + getBase().toString() + ']'; |
| 154 | } |
| 155 | |
| 156 | } |