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 | */ |
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 |
31 | implements 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, Object object, Chronology chrono) { |
72 | ReadableDuration dur = (ReadableDuration) object; |
73 | chrono = DateTimeUtils.getChronology(chrono); |
74 | long duration = dur.getMillis(); |
75 | int[] values = chrono.get(writablePeriod, duration); |
76 | for (int i = 0; i < values.length; i++) { |
77 | writablePeriod.setValue(i, values[i]); |
78 | } |
79 | } |
80 | |
81 | //----------------------------------------------------------------------- |
82 | /** |
83 | * Returns ReadableDuration.class. |
84 | * |
85 | * @return ReadableDuration.class |
86 | */ |
87 | public Class getSupportedType() { |
88 | return ReadableDuration.class; |
89 | } |
90 | |
91 | } |