| 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.DateTimeZone; |
| 21 | import org.joda.time.ReadableInstant; |
| 22 | import org.joda.time.chrono.ISOChronology; |
| 23 | |
| 24 | /** |
| 25 | * ReadableInstantConverter extracts milliseconds and chronology from a ReadableInstant. |
| 26 | * |
| 27 | * @author Stephen Colebourne |
| 28 | * @since 1.0 |
| 29 | */ |
| 30 | class ReadableInstantConverter extends AbstractConverter |
| 31 | implements InstantConverter, PartialConverter { |
| 32 | |
| 33 | /** |
| 34 | * Singleton instance. |
| 35 | */ |
| 36 | static final ReadableInstantConverter INSTANCE = new ReadableInstantConverter(); |
| 37 | |
| 38 | /** |
| 39 | * Restricted constructor. |
| 40 | */ |
| 41 | protected ReadableInstantConverter() { |
| 42 | super(); |
| 43 | } |
| 44 | |
| 45 | //----------------------------------------------------------------------- |
| 46 | /** |
| 47 | * Gets the chronology, which is taken from the ReadableInstant. |
| 48 | * If the chronology on the instant is null, the ISOChronology in the |
| 49 | * specified time zone is used. |
| 50 | * If the chronology on the instant is not in the specified zone, it is |
| 51 | * adapted. |
| 52 | * |
| 53 | * @param object the ReadableInstant to convert, must not be null |
| 54 | * @param zone the specified zone to use, null means default zone |
| 55 | * @return the chronology, never null |
| 56 | */ |
| 57 | public Chronology getChronology(Object object, DateTimeZone zone) { |
| 58 | Chronology chrono = ((ReadableInstant) object).getChronology(); |
| 59 | if (chrono == null) { |
| 60 | return ISOChronology.getInstance(zone); |
| 61 | } |
| 62 | DateTimeZone chronoZone = chrono.getZone(); |
| 63 | if (chronoZone != zone) { |
| 64 | chrono = chrono.withZone(zone); |
| 65 | if (chrono == null) { |
| 66 | return ISOChronology.getInstance(zone); |
| 67 | } |
| 68 | } |
| 69 | return chrono; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Gets the chronology, which is taken from the ReadableInstant. |
| 74 | * <p> |
| 75 | * If the passed in chronology is non-null, it is used. |
| 76 | * Otherwise the chronology from the instant is used. |
| 77 | * |
| 78 | * @param object the ReadableInstant to convert, must not be null |
| 79 | * @param chrono the chronology to use, null means use that from object |
| 80 | * @return the chronology, never null |
| 81 | */ |
| 82 | public Chronology getChronology(Object object, Chronology chrono) { |
| 83 | if (chrono == null) { |
| 84 | chrono = ((ReadableInstant) object).getChronology(); |
| 85 | chrono = DateTimeUtils.getChronology(chrono); |
| 86 | } |
| 87 | return chrono; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Extracts the millis from an object of this convertor's type. |
| 92 | * |
| 93 | * @param object the ReadableInstant to convert, must not be null |
| 94 | * @param chrono the non-null result of getChronology |
| 95 | * @return the millisecond value |
| 96 | * @throws NullPointerException if the object is null |
| 97 | * @throws ClassCastException if the object is an invalid type |
| 98 | */ |
| 99 | public long getInstantMillis(Object object, Chronology chrono) { |
| 100 | return ((ReadableInstant) object).getMillis(); |
| 101 | } |
| 102 | |
| 103 | //----------------------------------------------------------------------- |
| 104 | /** |
| 105 | * Returns ReadableInstant.class. |
| 106 | * |
| 107 | * @return ReadableInstant.class |
| 108 | */ |
| 109 | public Class getSupportedType() { |
| 110 | return ReadableInstant.class; |
| 111 | } |
| 112 | |
| 113 | } |