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.ReadWritableInterval; |
21 | import org.joda.time.ReadWritablePeriod; |
22 | import org.joda.time.ReadableInterval; |
23 | |
24 | /** |
25 | * Converts intervals into durations of any requested period type. |
26 | * |
27 | * @author Brian S O'Neill |
28 | * @since 1.0 |
29 | */ |
30 | class ReadableIntervalConverter extends AbstractConverter |
31 | implements IntervalConverter, DurationConverter, PeriodConverter { |
32 | |
33 | /** |
34 | * Singleton instance. |
35 | */ |
36 | static final ReadableIntervalConverter INSTANCE = new ReadableIntervalConverter(); |
37 | |
38 | /** |
39 | * Restricted constructor. |
40 | */ |
41 | protected ReadableIntervalConverter() { |
42 | super(); |
43 | } |
44 | |
45 | //----------------------------------------------------------------------- |
46 | /** |
47 | * Gets the millisecond length of the interval. |
48 | * |
49 | * @param object the interval |
50 | */ |
51 | public long getDurationMillis(Object object) { |
52 | return (((ReadableInterval) object)).toDurationMillis(); |
53 | } |
54 | |
55 | //----------------------------------------------------------------------- |
56 | /** |
57 | * Sets the values of the mutable duration from the specified interval. |
58 | * |
59 | * @param writablePeriod the period to modify |
60 | * @param object the interval to set from |
61 | * @param chrono the chronology to use |
62 | */ |
63 | public void setInto(ReadWritablePeriod writablePeriod, Object object, Chronology chrono) { |
64 | ReadableInterval interval = (ReadableInterval) object; |
65 | chrono = (chrono != null ? chrono : DateTimeUtils.getIntervalChronology(interval)); |
66 | long start = interval.getStartMillis(); |
67 | long end = interval.getEndMillis(); |
68 | int[] values = chrono.get(writablePeriod, start, end); |
69 | for (int i = 0; i < values.length; i++) { |
70 | writablePeriod.setValue(i, values[i]); |
71 | } |
72 | } |
73 | |
74 | //----------------------------------------------------------------------- |
75 | /** |
76 | * Checks if the input is a ReadableInterval. |
77 | * <p> |
78 | * If it is, then the calling code should cast and copy the fields directly. |
79 | * |
80 | * @param object the object to convert, must not be null |
81 | * @param chrono the chronology to use, may be null |
82 | * @return true if the input is a ReadableInterval |
83 | * @throws ClassCastException if the object is invalid |
84 | */ |
85 | public boolean isReadableInterval(Object object, Chronology chrono) { |
86 | return true; |
87 | } |
88 | |
89 | /** |
90 | * Extracts interval endpoint values from an object of this converter's |
91 | * type, and sets them into the given ReadWritableInterval. |
92 | * |
93 | * @param writableInterval interval to get modified, not null |
94 | * @param object the object to convert, must not be null |
95 | * @param chrono the chronology to use, may be null |
96 | * @throws ClassCastException if the object is invalid |
97 | */ |
98 | public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) { |
99 | ReadableInterval input = (ReadableInterval) object; |
100 | writableInterval.setInterval(input); |
101 | if (chrono != null) { |
102 | writableInterval.setChronology(chrono); |
103 | } else { |
104 | writableInterval.setChronology(input.getChronology()); |
105 | } |
106 | } |
107 | |
108 | //----------------------------------------------------------------------- |
109 | /** |
110 | * Returns ReadableInterval.class. |
111 | */ |
112 | public Class getSupportedType() { |
113 | return ReadableInterval.class; |
114 | } |
115 | |
116 | } |