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 org.joda.time.DateTimeField;
19 import org.joda.time.DateTimeFieldType;
20 import org.joda.time.DurationField;
21 import org.joda.time.ReadablePartial;
22 import org.joda.time.field.DecoratedDateTimeField;
23 import org.joda.time.field.FieldUtils;
24
25 /**
26  * This field is not publicy exposed by ISOChronology, but rather it is used to
27  * build the yearOfCentury and centuryOfEra fields. It merely drops the sign of
28  * the year.
29  *
30  * @author Brian S O'Neill
31  * @see GJYearOfEraDateTimeField
32  * @since 1.0
33  */

34 class ISOYearOfEraDateTimeField extends DecoratedDateTimeField {
35
36     @SuppressWarnings("unused")
37     private static final long serialVersionUID = 7037524068969447317L;
38
39     /**
40      * Singleton instance
41      */

42     static final DateTimeField INSTANCE = new ISOYearOfEraDateTimeField();
43
44     /**
45      * Restricted constructor.
46      */

47     private ISOYearOfEraDateTimeField() {
48         super(GregorianChronology.getInstanceUTC().year(), DateTimeFieldType.yearOfEra());
49     }
50
51     @Override
52     public DurationField getRangeDurationField() {
53         return GregorianChronology.getInstanceUTC().eras();
54     }
55
56     public int get(long instant) {
57         int year = getWrappedField().get(instant);
58         return year < 0 ? -year : year;
59     }
60
61     public long add(long instant, int years) {
62         return getWrappedField().add(instant, years);
63     }
64
65     public long add(long instant, long years) {
66         return getWrappedField().add(instant, years);
67     }
68
69     public long addWrapField(long instant, int years) {
70         return getWrappedField().addWrapField(instant, years);
71     }
72
73     public int[] addWrapField(ReadablePartial instant, int fieldIndex, int[] values, int years) {
74         return getWrappedField().addWrapField(instant, fieldIndex, values, years);
75     }
76
77     public int getDifference(long minuendInstant, long subtrahendInstant) {
78         return getWrappedField().getDifference(minuendInstant, subtrahendInstant);
79     }
80
81     public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
82         return getWrappedField().getDifferenceAsLong(minuendInstant, subtrahendInstant);
83     }
84
85     public long set(long instant, int year) {
86         FieldUtils.verifyValueBounds(this, year, 0, getMaximumValue());
87         if (getWrappedField().get(instant) < 0) {
88             year = -year;
89         }
90         return super.set(instant, year);
91     }
92
93     public int getMinimumValue() {
94         return 0;
95     }
96
97     public int getMaximumValue() {
98         return getWrappedField().getMaximumValue();
99     }
100
101     public long roundFloor(long instant) {
102         return getWrappedField().roundFloor(instant);
103     }
104
105     public long roundCeiling(long instant) {
106         return getWrappedField().roundCeiling(instant);
107     }
108
109     public long remainder(long instant) {
110         return getWrappedField().remainder(instant);
111     }
112
113     /**
114      * Serialization singleton
115      */

116     private Object readResolve() {
117         return INSTANCE;
118     }
119 }
120