KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > convert > ReadablePartialConverter


1 /*
2  * Copyright 2001-2006 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.convert;
17
18 import org.joda.time.Chronology;
19 import org.joda.time.DateTimeUtils;
20 import org.joda.time.DateTimeZone;
21 import org.joda.time.ReadablePartial;
22
23 /**
24  * ReadablePartialConverter extracts partial fields and chronology from a ReadablePartial.
25  *
26  * @author Stephen Colebourne
27  * @since 1.0
28  */

29 class ReadablePartialConverter extends AbstractConverter
30         implements PartialConverter {
31
32     /**
33      * Singleton instance.
34      */

35     static final ReadablePartialConverter INSTANCE = new ReadablePartialConverter();
36
37     /**
38      * Restricted constructor.
39      */

40     protected ReadablePartialConverter() {
41         super();
42     }
43
44     //-----------------------------------------------------------------------
45
/**
46      * Gets the chronology, which is taken from the ReadablePartial.
47      *
48      * @param object the ReadablePartial to convert, must not be null
49      * @param zone the specified zone to use, null means default zone
50      * @return the chronology, never null
51      */

52     public Chronology getChronology(Object JavaDoc object, DateTimeZone zone) {
53         return getChronology(object, (Chronology) null).withZone(zone);
54     }
55
56     /**
57      * Gets the chronology, which is taken from the ReadableInstant.
58      * <p>
59      * If the passed in chronology is non-null, it is used.
60      * Otherwise the chronology from the instant is used.
61      *
62      * @param object the ReadablePartial to convert, must not be null
63      * @param chrono the chronology to use, null means use that from object
64      * @return the chronology, never null
65      */

66     public Chronology getChronology(Object JavaDoc object, Chronology chrono) {
67         if (chrono == null) {
68             chrono = ((ReadablePartial) object).getChronology();
69             chrono = DateTimeUtils.getChronology(chrono);
70         }
71         return chrono;
72     }
73
74     /**
75      * Extracts the values of the partial from an object of this converter's type.
76      * The chrono parameter is a hint to the converter, should it require a
77      * chronology to aid in conversion.
78      *
79      * @param fieldSource a partial that provides access to the fields.
80      * This partial may be incomplete and only getFieldType(int) should be used
81      * @param object the object to convert
82      * @param chrono the chronology to use, which is the non-null result of getChronology()
83      * @return the array of field values that match the fieldSource, must be non-null valid
84      * @throws ClassCastException if the object is invalid
85      */

86     public int[] getPartialValues(ReadablePartial fieldSource, Object JavaDoc object, Chronology chrono) {
87         ReadablePartial input = (ReadablePartial) object;
88         int size = fieldSource.size();
89         int[] values = new int[size];
90         for (int i = 0; i < size; i++) {
91             values[i] = input.get(fieldSource.getFieldType(i));
92         }
93         chrono.validate(fieldSource, values);
94         return values;
95     }
96
97     //-----------------------------------------------------------------------
98
/**
99      * Returns ReadableInstant.class.
100      *
101      * @return ReadableInstant.class
102      */

103     public Class JavaDoc getSupportedType() {
104         return ReadablePartial.class;
105     }
106
107 }
108
Popular Tags