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  * Provides time calculations for the year of era component of time.
27  * 
28  * @author Brian S O'Neill
29  * @since 1.0
30  */

31 final class GJYearOfEraDateTimeField extends DecoratedDateTimeField {
32
33     @SuppressWarnings("unused")
34     private static final long serialVersionUID = -5961050944769862059L;
35
36     private final BasicChronology iChronology;
37
38     /**
39      * Restricted constructor.
40      */

41     GJYearOfEraDateTimeField(DateTimeField yearField, BasicChronology chronology) {
42         super(yearField, DateTimeFieldType.yearOfEra());
43         iChronology = chronology;
44     }
45
46     @Override
47     public DurationField getRangeDurationField() {
48         return iChronology.eras();
49     }
50
51     public int get(long instant) {
52         int year = getWrappedField().get(instant);
53         if (year <= 0) {
54             year = 1 - year;
55         }
56         return year;
57     }
58
59     public long add(long instant, int years) {
60         return getWrappedField().add(instant, years);
61     }
62
63     public long add(long instant, long years) {
64         return getWrappedField().add(instant, years);
65     }
66
67     public long addWrapField(long instant, int years) {
68         return getWrappedField().addWrapField(instant, years);
69     }
70
71     public int[] addWrapField(ReadablePartial instant, int fieldIndex, int[] values, int years) {
72         return getWrappedField().addWrapField(instant, fieldIndex, values, years);
73     }
74
75     public int getDifference(long minuendInstant, long subtrahendInstant) {
76         return getWrappedField().getDifference(minuendInstant, subtrahendInstant);
77     }
78
79     public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
80         return getWrappedField().getDifferenceAsLong(minuendInstant, subtrahendInstant);
81     }
82
83     /**
84      * Set the year component of the specified time instant.
85      * 
86      * @param instant  the time instant in millis to update.
87      * @param year  the year (0,292278994) to update the time to.
88      * @return the updated time instant.
89      * @throws IllegalArgumentException  if year is invalid.
90      */

91     public long set(long instant, int year) {
92         FieldUtils.verifyValueBounds(this, year, 1, getMaximumValue());
93         if (iChronology.getYear(instant) <= 0) {
94             year = 1 - year;
95         }
96         return super.set(instant, year);
97     }
98
99     public int getMinimumValue() {
100         return 1;
101     }
102
103     public int getMaximumValue() {
104         return getWrappedField().getMaximumValue();
105     }
106
107     public long roundFloor(long instant) {
108         return getWrappedField().roundFloor(instant);
109     }
110
111     public long roundCeiling(long instant) {
112         return getWrappedField().roundCeiling(instant);
113     }
114
115     public long remainder(long instant) {
116         return getWrappedField().remainder(instant);
117     }
118
119     /**
120      * Serialization singleton
121      */

122     private Object readResolve() {
123         return iChronology.yearOfEra();
124     }
125 }
126