| 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.chrono; |
| 17 | |
| 18 | import java.util.Locale; |
| 19 | |
| 20 | import org.joda.time.DateTimeConstants; |
| 21 | import org.joda.time.DateTimeFieldType; |
| 22 | import org.joda.time.DurationField; |
| 23 | import org.joda.time.DurationFieldType; |
| 24 | import org.joda.time.field.BaseDateTimeField; |
| 25 | import org.joda.time.field.FieldUtils; |
| 26 | import org.joda.time.field.UnsupportedDurationField; |
| 27 | |
| 28 | /** |
| 29 | * Provides time calculations for the era component of time. |
| 30 | * |
| 31 | * @author Stephen Colebourne |
| 32 | * @author Brian S O'Neill |
| 33 | * @since 1.0 |
| 34 | */ |
| 35 | final class GJEraDateTimeField extends BaseDateTimeField { |
| 36 | |
| 37 | /** Serialization version */ |
| 38 | private static final long serialVersionUID = 4240986525305515528L; |
| 39 | |
| 40 | private final BasicChronology iChronology; |
| 41 | |
| 42 | /** |
| 43 | * Restricted constructor |
| 44 | */ |
| 45 | GJEraDateTimeField(BasicChronology chronology) { |
| 46 | super(DateTimeFieldType.era()); |
| 47 | iChronology = chronology; |
| 48 | } |
| 49 | |
| 50 | public boolean isLenient() { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the Era component of the specified time instant. |
| 56 | * |
| 57 | * @param instant the time instant in millis to query. |
| 58 | */ |
| 59 | public int get(long instant) { |
| 60 | if (iChronology.getYear(instant) <= 0) { |
| 61 | return DateTimeConstants.BCE; |
| 62 | } else { |
| 63 | return DateTimeConstants.CE; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public String getAsText(int fieldValue, Locale locale) { |
| 68 | return GJLocaleSymbols.forLocale(locale).eraValueToText(fieldValue); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set the Era component of the specified time instant. |
| 73 | * |
| 74 | * @param instant the time instant in millis to update. |
| 75 | * @param era the era to update the time to. |
| 76 | * @return the updated time instant. |
| 77 | * @throws IllegalArgumentException if era is invalid. |
| 78 | */ |
| 79 | public long set(long instant, int era) { |
| 80 | FieldUtils.verifyValueBounds(this, era, DateTimeConstants.BCE, DateTimeConstants.CE); |
| 81 | |
| 82 | int oldEra = get(instant); |
| 83 | if (oldEra != era) { |
| 84 | int year = iChronology.getYear(instant); |
| 85 | return iChronology.setYear(instant, -year); |
| 86 | } else { |
| 87 | return instant; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public long set(long instant, String text, Locale locale) { |
| 92 | return set(instant, GJLocaleSymbols.forLocale(locale).eraTextToValue(text)); |
| 93 | } |
| 94 | |
| 95 | public long roundFloor(long instant) { |
| 96 | if (get(instant) == DateTimeConstants.CE) { |
| 97 | return iChronology.setYear(0, 1); |
| 98 | } else { |
| 99 | return Long.MIN_VALUE; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public long roundCeiling(long instant) { |
| 104 | if (get(instant) == DateTimeConstants.BCE) { |
| 105 | return iChronology.setYear(0, 1); |
| 106 | } else { |
| 107 | return Long.MAX_VALUE; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public long roundHalfFloor(long instant) { |
| 112 | // In reality, the era is infinite, so there is no halfway point. |
| 113 | return roundFloor(instant); |
| 114 | } |
| 115 | |
| 116 | public long roundHalfCeiling(long instant) { |
| 117 | // In reality, the era is infinite, so there is no halfway point. |
| 118 | return roundFloor(instant); |
| 119 | } |
| 120 | |
| 121 | public long roundHalfEven(long instant) { |
| 122 | // In reality, the era is infinite, so there is no halfway point. |
| 123 | return roundFloor(instant); |
| 124 | } |
| 125 | |
| 126 | public DurationField getDurationField() { |
| 127 | return UnsupportedDurationField.getInstance(DurationFieldType.eras()); |
| 128 | } |
| 129 | |
| 130 | public DurationField getRangeDurationField() { |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | public int getMinimumValue() { |
| 135 | return DateTimeConstants.BCE; |
| 136 | } |
| 137 | |
| 138 | public int getMaximumValue() { |
| 139 | return DateTimeConstants.CE; |
| 140 | } |
| 141 | |
| 142 | public int getMaximumTextLength(Locale locale) { |
| 143 | return GJLocaleSymbols.forLocale(locale).getEraMaxTextLength(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Serialization singleton |
| 148 | */ |
| 149 | private Object readResolve() { |
| 150 | return iChronology.era(); |
| 151 | } |
| 152 | } |