01: /*
02: * Copyright 2001-2005 Stephen Colebourne
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.PeriodType;
20: import org.joda.time.ReadWritablePeriod;
21: import org.joda.time.ReadablePeriod;
22:
23: /**
24: * ReadablePeriodConverter extracts milliseconds and chronology from a ReadablePeriod.
25: *
26: * @author Stephen Colebourne
27: * @author Brian S O'Neill
28: * @since 1.0
29: */
30: class ReadablePeriodConverter extends AbstractConverter implements
31: PeriodConverter {
32:
33: /**
34: * Singleton instance.
35: */
36: static final ReadablePeriodConverter INSTANCE = new ReadablePeriodConverter();
37:
38: /**
39: * Restricted constructor.
40: */
41: protected ReadablePeriodConverter() {
42: super ();
43: }
44:
45: //-----------------------------------------------------------------------
46: /**
47: * Extracts duration values from an object of this converter's type, and
48: * sets them into the given ReadWritablePeriod.
49: *
50: * @param duration duration to get modified
51: * @param object the object to convert, must not be null
52: * @param chrono the chronology to use
53: * @throws NullPointerException if the duration or object is null
54: * @throws ClassCastException if the object is an invalid type
55: * @throws IllegalArgumentException if the object is invalid
56: */
57: public void setInto(ReadWritablePeriod duration, Object object,
58: Chronology chrono) {
59: duration.setPeriod((ReadablePeriod) object);
60: }
61:
62: /**
63: * Selects a suitable period type for the given object.
64: *
65: * @param object the object to examine, must not be null
66: * @return the period type from the readable duration
67: * @throws NullPointerException if the object is null
68: * @throws ClassCastException if the object is an invalid type
69: */
70: public PeriodType getPeriodType(Object object) {
71: ReadablePeriod period = (ReadablePeriod) object;
72: return period.getPeriodType();
73: }
74:
75: //-----------------------------------------------------------------------
76: /**
77: * Returns ReadablePeriod class.
78: *
79: * @return ReadablePeriod.class
80: */
81: public Class getSupportedType() {
82: return ReadablePeriod.class;
83: }
84:
85: }
|