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.convert;
017:
018: import java.util.Calendar;
019: import java.util.GregorianCalendar;
020:
021: import org.joda.time.Chronology;
022: import org.joda.time.DateTimeZone;
023: import org.joda.time.chrono.BuddhistChronology;
024: import org.joda.time.chrono.GJChronology;
025: import org.joda.time.chrono.GregorianChronology;
026: import org.joda.time.chrono.ISOChronology;
027: import org.joda.time.chrono.JulianChronology;
028:
029: /**
030: * CalendarConverter converts a java util Calendar to an instant or partial.
031: * The Calendar is converted to milliseconds and the chronology that best
032: * matches the calendar.
033: *
034: * @author Stephen Colebourne
035: * @since 1.0
036: */
037: final class CalendarConverter extends AbstractConverter implements
038: InstantConverter, PartialConverter {
039:
040: /**
041: * Singleton instance.
042: */
043: static final CalendarConverter INSTANCE = new CalendarConverter();
044:
045: /**
046: * Restricted constructor.
047: */
048: protected CalendarConverter() {
049: super ();
050: }
051:
052: //-----------------------------------------------------------------------
053: /**
054: * Gets the chronology.
055: * <p>
056: * If a chronology is specified then it is used.
057: * Otherwise, it is the GJChronology if a GregorianCalendar is used,
058: * BuddhistChronology if a BuddhistCalendar is used or ISOChronology otherwise.
059: * The time zone is extracted from the calendar if possible, default used if not.
060: *
061: * @param object the Calendar to convert, must not be null
062: * @param chrono the chronology to use, null means use Calendar
063: * @return the chronology, never null
064: * @throws NullPointerException if the object is null
065: * @throws ClassCastException if the object is an invalid type
066: */
067: public Chronology getChronology(Object object, Chronology chrono) {
068: if (chrono != null) {
069: return chrono;
070: }
071: Calendar cal = (Calendar) object;
072: DateTimeZone zone = null;
073: try {
074: zone = DateTimeZone.forTimeZone(cal.getTimeZone());
075:
076: } catch (IllegalArgumentException ex) {
077: zone = DateTimeZone.getDefault();
078: }
079: return getChronology(cal, zone);
080: }
081:
082: /**
083: * Gets the chronology, which is the GJChronology if a GregorianCalendar is used,
084: * BuddhistChronology if a BuddhistCalendar is used or ISOChronology otherwise.
085: * The time zone specified is used in preference to that on the calendar.
086: *
087: * @param object the Calendar to convert, must not be null
088: * @param zone the specified zone to use, null means default zone
089: * @return the chronology, never null
090: * @throws NullPointerException if the object is null
091: * @throws ClassCastException if the object is an invalid type
092: */
093: public Chronology getChronology(Object object, DateTimeZone zone) {
094: if (object.getClass().getName().endsWith(".BuddhistCalendar")) {
095: return BuddhistChronology.getInstance(zone);
096: } else if (object instanceof GregorianCalendar) {
097: GregorianCalendar gc = (GregorianCalendar) object;
098: long cutover = gc.getGregorianChange().getTime();
099: if (cutover == Long.MIN_VALUE) {
100: return GregorianChronology.getInstance(zone);
101: } else if (cutover == Long.MAX_VALUE) {
102: return JulianChronology.getInstance(zone);
103: } else {
104: return GJChronology.getInstance(zone, cutover, 4);
105: }
106: } else {
107: return ISOChronology.getInstance(zone);
108: }
109: }
110:
111: /**
112: * Gets the millis, which is the Calendar millis value.
113: *
114: * @param object the Calendar to convert, must not be null
115: * @param chrono the chronology result from getChronology, non-null
116: * @return the millisecond value
117: * @throws NullPointerException if the object is null
118: * @throws ClassCastException if the object is an invalid type
119: */
120: public long getInstantMillis(Object object, Chronology chrono) {
121: Calendar calendar = (Calendar) object;
122: return calendar.getTime().getTime();
123: }
124:
125: //-----------------------------------------------------------------------
126: /**
127: * Returns Calendar.class.
128: *
129: * @return Calendar.class
130: */
131: public Class getSupportedType() {
132: return Calendar.class;
133: }
134:
135: }
|