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 java.util.HashMap;
019: import java.util.Map;
020:
021: import org.joda.time.Chronology;
022: import org.joda.time.DateTime;
023: import org.joda.time.DateTimeConstants;
024: import org.joda.time.DateTimeField;
025: import org.joda.time.DateTimeFieldType;
026: import org.joda.time.DateTimeZone;
027: import org.joda.time.field.DelegatedDateTimeField;
028: import org.joda.time.field.DividedDateTimeField;
029: import org.joda.time.field.OffsetDateTimeField;
030: import org.joda.time.field.RemainderDateTimeField;
031: import org.joda.time.field.SkipUndoDateTimeField;
032:
033: /**
034: * A chronology that matches the BuddhistCalendar class supplied by Sun.
035: * <p>
036: * The chronology is identical to the Gregorian/Julian, except that the
037: * year is offset by +543 and the era is named 'BE' for Buddhist Era.
038: * <p>
039: * This class was intended by Sun to model the calendar used in Thailand.
040: * However, the actual rules for Thailand are much more involved than
041: * this class covers. (This class is accurate after 1941-01-01 ISO).
042: * <p>
043: * This chronlogy is being retained for those who want a same effect
044: * replacement for the Sun class. It is hoped that community support will
045: * enable a more accurate chronology for Thailand, to be developed.
046: * <p>
047: * BuddhistChronology is thread-safe and immutable.
048: *
049: * @author Stephen Colebourne
050: * @author Brian S O'Neill
051: * @since 1.0
052: */
053: public final class BuddhistChronology extends AssembledChronology {
054:
055: /** Serialization lock */
056: private static final long serialVersionUID = -3474595157769370126L;
057:
058: /**
059: * Constant value for 'Buddhist Era', equivalent to the value returned
060: * for AD/CE. Note that this differs from the constant in BuddhistCalendar.
061: */
062: public static final int BE = DateTimeConstants.CE;
063:
064: /** A singleton era field. */
065: private static final DateTimeField ERA_FIELD = new BasicSingleEraDateTimeField(
066: "BE");
067:
068: /** Number of years difference in calendars. */
069: private static final int BUDDHIST_OFFSET = 543;
070:
071: /** Cache of zone to chronology */
072: private static final Map cCache = new HashMap();
073:
074: /** UTC instance of the chronology */
075: private static final BuddhistChronology INSTANCE_UTC = getInstance(DateTimeZone.UTC);
076:
077: /**
078: * Standard instance of a Buddhist Chronology, that matches
079: * Sun's BuddhistCalendar class. This means that it follows the
080: * GregorianJulian calendar rules with a cutover date.
081: * <p>
082: * The time zone of the returned instance is UTC.
083: */
084: public static BuddhistChronology getInstanceUTC() {
085: return INSTANCE_UTC;
086: }
087:
088: /**
089: * Standard instance of a Buddhist Chronology, that matches
090: * Sun's BuddhistCalendar class. This means that it follows the
091: * GregorianJulian calendar rules with a cutover date.
092: */
093: public static BuddhistChronology getInstance() {
094: return getInstance(DateTimeZone.getDefault());
095: }
096:
097: /**
098: * Standard instance of a Buddhist Chronology, that matches
099: * Sun's BuddhistCalendar class. This means that it follows the
100: * GregorianJulian calendar rules with a cutover date.
101: *
102: * @param zone the time zone to use, null is default
103: */
104: public static synchronized BuddhistChronology getInstance(
105: DateTimeZone zone) {
106: if (zone == null) {
107: zone = DateTimeZone.getDefault();
108: }
109: BuddhistChronology chrono = (BuddhistChronology) cCache
110: .get(zone);
111: if (chrono == null) {
112: // First create without a lower limit.
113: chrono = new BuddhistChronology(GJChronology.getInstance(
114: zone, null), null);
115: // Impose lower limit and make another BuddhistChronology.
116: DateTime lowerLimit = new DateTime(1, 1, 1, 0, 0, 0, 0,
117: chrono);
118: chrono = new BuddhistChronology(LimitChronology
119: .getInstance(chrono, lowerLimit, null), "");
120: cCache.put(zone, chrono);
121: }
122: return chrono;
123: }
124:
125: // Constructors and instance variables
126: //-----------------------------------------------------------------------
127:
128: /**
129: * Restricted constructor.
130: *
131: * @param param if non-null, then don't change the field set
132: */
133: private BuddhistChronology(Chronology base, Object param) {
134: super (base, param);
135: }
136:
137: /**
138: * Serialization singleton
139: */
140: private Object readResolve() {
141: Chronology base = getBase();
142: return base == null ? getInstanceUTC() : getInstance(base
143: .getZone());
144: }
145:
146: // Conversion
147: //-----------------------------------------------------------------------
148: /**
149: * Gets the Chronology in the UTC time zone.
150: *
151: * @return the chronology in UTC
152: */
153: public Chronology withUTC() {
154: return INSTANCE_UTC;
155: }
156:
157: /**
158: * Gets the Chronology in a specific time zone.
159: *
160: * @param zone the zone to get the chronology in, null is default
161: * @return the chronology
162: */
163: public Chronology withZone(DateTimeZone zone) {
164: if (zone == null) {
165: zone = DateTimeZone.getDefault();
166: }
167: if (zone == getZone()) {
168: return this ;
169: }
170: return getInstance(zone);
171: }
172:
173: // Output
174: //-----------------------------------------------------------------------
175: /**
176: * Gets a debugging toString.
177: *
178: * @return a debugging string
179: */
180: public String toString() {
181: String str = "BuddhistChronology";
182: DateTimeZone zone = getZone();
183: if (zone != null) {
184: str = str + '[' + zone.getID() + ']';
185: }
186: return str;
187: }
188:
189: protected void assemble(Fields fields) {
190: if (getParam() == null) {
191: // julian chrono removed zero, but we need to put it back
192: DateTimeField field = fields.year;
193: fields.year = new OffsetDateTimeField(
194: new SkipUndoDateTimeField(this , field),
195: BUDDHIST_OFFSET);
196:
197: // one era, so yearOfEra is the same
198: field = fields.yearOfEra;
199: fields.yearOfEra = new DelegatedDateTimeField(fields.year,
200: DateTimeFieldType.yearOfEra());
201:
202: // julian chrono removed zero, but we need to put it back
203: field = fields.weekyear;
204: fields.weekyear = new OffsetDateTimeField(
205: new SkipUndoDateTimeField(this , field),
206: BUDDHIST_OFFSET);
207:
208: field = new OffsetDateTimeField(fields.yearOfEra, 99);
209: fields.centuryOfEra = new DividedDateTimeField(field,
210: DateTimeFieldType.centuryOfEra(), 100);
211:
212: field = new RemainderDateTimeField(
213: (DividedDateTimeField) fields.centuryOfEra);
214: fields.yearOfCentury = new OffsetDateTimeField(field,
215: DateTimeFieldType.yearOfCentury(), 1);
216:
217: field = new RemainderDateTimeField(fields.weekyear,
218: DateTimeFieldType.weekyearOfCentury(), 100);
219: fields.weekyearOfCentury = new OffsetDateTimeField(field,
220: DateTimeFieldType.weekyearOfCentury(), 1);
221:
222: fields.era = ERA_FIELD;
223: }
224: }
225:
226: }
|