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.DateTimeUtils;
20: import org.joda.time.ReadWritablePeriod;
21: import org.joda.time.ReadableDuration;
22:
23: /**
24: * ReadableDurationConverter extracts milliseconds and chronology from a ReadableDuration.
25: *
26: * @author Stephen Colebourne
27: * @author Brian S O'Neill
28: * @since 1.0
29: */
30: class ReadableDurationConverter extends AbstractConverter implements
31: DurationConverter, PeriodConverter {
32:
33: /**
34: * Singleton instance.
35: */
36: static final ReadableDurationConverter INSTANCE = new ReadableDurationConverter();
37:
38: /**
39: * Restricted constructor.
40: */
41: protected ReadableDurationConverter() {
42: super ();
43: }
44:
45: //-----------------------------------------------------------------------
46: /**
47: * Extracts the millis from an object of this convertor's type.
48: *
49: * @param object the object to convert, must not be null
50: * @return the millisecond value
51: * @throws NullPointerException if the object is null
52: * @throws ClassCastException if the object is an invalid type
53: * @throws IllegalArgumentException if the object is invalid
54: */
55: public long getDurationMillis(Object object) {
56: return ((ReadableDuration) object).getMillis();
57: }
58:
59: //-----------------------------------------------------------------------
60: /**
61: * Extracts duration values from an object of this converter's type, and
62: * sets them into the given ReadWritableDuration.
63: *
64: * @param writablePeriod period to get modified
65: * @param object the object to convert, must not be null
66: * @param chrono the chronology to use, must not be null
67: * @throws NullPointerException if the duration or object is null
68: * @throws ClassCastException if the object is an invalid type
69: * @throws IllegalArgumentException if the object is invalid
70: */
71: public void setInto(ReadWritablePeriod writablePeriod,
72: Object object, Chronology chrono) {
73: ReadableDuration dur = (ReadableDuration) object;
74: chrono = DateTimeUtils.getChronology(chrono);
75: long duration = dur.getMillis();
76: int[] values = chrono.get(writablePeriod, duration);
77: for (int i = 0; i < values.length; i++) {
78: writablePeriod.setValue(i, values[i]);
79: }
80: }
81:
82: //-----------------------------------------------------------------------
83: /**
84: * Returns ReadableDuration.class.
85: *
86: * @return ReadableDuration.class
87: */
88: public Class getSupportedType() {
89: return ReadableDuration.class;
90: }
91:
92: }
|