1 /*
2  *  Copyright 2001-2013 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     @SuppressWarnings("unused")
39     private static final long serialVersionUID = 4240986525305515528L;
40
41     private final BasicChronology iChronology;
42
43     /**
44      * Restricted constructor
45      */

46     GJEraDateTimeField(BasicChronology chronology) {
47         super(DateTimeFieldType.era());
48         iChronology = chronology;
49     }
50
51     public boolean isLenient() {
52         return false;
53     }
54
55     /**
56      * Get the Era component of the specified time instant.
57      * 
58      * @param instant  the time instant in millis to query.
59      */

60     public int get(long instant) {
61         if (iChronology.getYear(instant) <= 0) {
62             return DateTimeConstants.BCE;
63         } else {
64             return DateTimeConstants.CE;
65         }
66     }
67
68     public String getAsText(int fieldValue, Locale locale) {
69         return GJLocaleSymbols.forLocale(locale).eraValueToText(fieldValue);
70     }
71
72     /**
73      * Set the Era component of the specified time instant.
74      * 
75      * @param instant  the time instant in millis to update.
76      * @param era  the era to update the time to.
77      * @return the updated time instant.
78      * @throws IllegalArgumentException  if era is invalid.
79      */

80     public long set(long instant, int era) {
81         FieldUtils.verifyValueBounds(this, era, DateTimeConstants.BCE, DateTimeConstants.CE);
82             
83         int oldEra = get(instant);
84         if (oldEra != era) {
85             int year = iChronology.getYear(instant);
86             return iChronology.setYear(instant, -year);
87         } else {
88             return instant;
89         }
90     }
91
92     public long set(long instant, String text, Locale locale) {
93         return set(instant, GJLocaleSymbols.forLocale(locale).eraTextToValue(text));
94     }
95
96     public long roundFloor(long instant) {
97         if (get(instant) == DateTimeConstants.CE) {
98             return iChronology.setYear(0, 1);
99         } else {
100             return Long.MIN_VALUE;
101         }
102     }
103
104     public long roundCeiling(long instant) {
105         if (get(instant) == DateTimeConstants.BCE) {
106             return iChronology.setYear(0, 1);
107         } else {
108             return Long.MAX_VALUE;
109         }
110     }
111
112     public long roundHalfFloor(long instant) {
113         // In reality, the era is infinite, so there is no halfway point.
114         return roundFloor(instant);
115     }
116
117     public long roundHalfCeiling(long instant) {
118         // In reality, the era is infinite, so there is no halfway point.
119         return roundFloor(instant);
120     }
121
122     public long roundHalfEven(long instant) {
123         // In reality, the era is infinite, so there is no halfway point.
124         return roundFloor(instant);
125     }
126
127     public DurationField getDurationField() {
128         return UnsupportedDurationField.getInstance(DurationFieldType.eras());
129     }
130
131     public DurationField getRangeDurationField() {
132         return null;
133     }
134
135     public int getMinimumValue() {
136         return DateTimeConstants.BCE;
137     }
138
139     public int getMaximumValue() {
140         return DateTimeConstants.CE;
141     }
142
143     public int getMaximumTextLength(Locale locale) {
144         return GJLocaleSymbols.forLocale(locale).getEraMaxTextLength();
145     }
146
147     /**
148      * Serialization singleton
149      */

150     private Object readResolve() {
151         return iChronology.era();
152     }
153 }
154