| 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.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 |
| 31 | implements 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, Chronology chrono) { |
| 58 | duration.setPeriod((ReadablePeriod) object); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Selects a suitable period type for the given object. |
| 63 | * |
| 64 | * @param object the object to examine, must not be null |
| 65 | * @return the period type from the readable duration |
| 66 | * @throws NullPointerException if the object is null |
| 67 | * @throws ClassCastException if the object is an invalid type |
| 68 | */ |
| 69 | public PeriodType getPeriodType(Object object) { |
| 70 | ReadablePeriod period = (ReadablePeriod) object; |
| 71 | return period.getPeriodType(); |
| 72 | } |
| 73 | |
| 74 | //----------------------------------------------------------------------- |
| 75 | /** |
| 76 | * Returns ReadablePeriod class. |
| 77 | * |
| 78 | * @return ReadablePeriod.class |
| 79 | */ |
| 80 | public Class getSupportedType() { |
| 81 | return ReadablePeriod.class; |
| 82 | } |
| 83 | |
| 84 | } |