EMMA Coverage Report (generated Tue Oct 28 00:01:11 GMT 2008)
[all classes][org.joda.time.convert]

COVERAGE SUMMARY FOR SOURCE FILE [CalendarConverter.java]

nameclass, %method, %block, %line, %
CalendarConverter.java100% (1/1)100% (6/6)100% (87/87)100% (26/26)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CalendarConverter100% (1/1)100% (6/6)100% (87/87)100% (26/26)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
CalendarConverter (): void 100% (1/1)100% (3/3)100% (2/2)
getChronology (Object, Chronology): Chronology 100% (1/1)100% (22/22)100% (9/9)
getChronology (Object, DateTimeZone): Chronology 100% (1/1)100% (41/41)100% (11/11)
getInstantMillis (Object, Chronology): long 100% (1/1)100% (7/7)100% (2/2)
getSupportedType (): Class 100% (1/1)100% (9/9)100% (1/1)

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 */
16package org.joda.time.convert;
17 
18import java.util.Calendar;
19import java.util.GregorianCalendar;
20 
21import org.joda.time.Chronology;
22import org.joda.time.DateTimeZone;
23import org.joda.time.chrono.BuddhistChronology;
24import org.joda.time.chrono.GJChronology;
25import org.joda.time.chrono.GregorianChronology;
26import org.joda.time.chrono.ISOChronology;
27import org.joda.time.chrono.JulianChronology;
28 
29/**
30 * CalendarConverter converts a java util Calendar to an instant or partial.
31 * The Calendar is converted to milliseconds and the chronology that best
32 * matches the calendar.
33 *
34 * @author Stephen Colebourne
35 * @since 1.0
36 */
37final class CalendarConverter extends AbstractConverter
38        implements InstantConverter, PartialConverter {
39 
40    /**
41     * Singleton instance.
42     */
43    static final CalendarConverter INSTANCE = new CalendarConverter();
44 
45    /**
46     * Restricted constructor.
47     */
48    protected CalendarConverter() {
49        super();
50    }
51 
52    //-----------------------------------------------------------------------
53    /**
54     * Gets the chronology.
55     * <p>
56     * If a chronology is specified then it is used.
57     * Otherwise, it is the GJChronology if a GregorianCalendar is used,
58     * BuddhistChronology if a BuddhistCalendar is used or ISOChronology otherwise.
59     * The time zone is extracted from the calendar if possible, default used if not.
60     * 
61     * @param object  the Calendar to convert, must not be null
62     * @param chrono  the chronology to use, null means use Calendar
63     * @return the chronology, never null
64     * @throws NullPointerException if the object is null
65     * @throws ClassCastException if the object is an invalid type
66     */
67    public Chronology getChronology(Object object, Chronology chrono) {
68        if (chrono != null) {
69            return chrono;
70        }
71        Calendar cal = (Calendar) object;
72        DateTimeZone zone = null;
73        try {
74            zone = DateTimeZone.forTimeZone(cal.getTimeZone());
75            
76        } catch (IllegalArgumentException ex) {
77            zone = DateTimeZone.getDefault();
78        }
79        return getChronology(cal, zone);
80    }
81 
82    /**
83     * Gets the chronology, which is the GJChronology if a GregorianCalendar is used,
84     * BuddhistChronology if a BuddhistCalendar is used or ISOChronology otherwise.
85     * The time zone specified is used in preference to that on the calendar.
86     * 
87     * @param object  the Calendar to convert, must not be null
88     * @param zone  the specified zone to use, null means default zone
89     * @return the chronology, never null
90     * @throws NullPointerException if the object is null
91     * @throws ClassCastException if the object is an invalid type
92     */
93    public Chronology getChronology(Object object, DateTimeZone zone) {
94        if (object.getClass().getName().endsWith(".BuddhistCalendar")) {
95            return BuddhistChronology.getInstance(zone);
96        } else if (object instanceof GregorianCalendar) {
97            GregorianCalendar gc = (GregorianCalendar) object;
98            long cutover = gc.getGregorianChange().getTime();
99            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}

[all classes][org.joda.time.convert]
EMMA 2.0.5312 (C) Vladimir Roubtsov