001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.chrono;
017:
018: import org.joda.time.DateTimeField;
019: import org.joda.time.DateTimeFieldType;
020: import org.joda.time.ReadablePartial;
021: import org.joda.time.field.DecoratedDateTimeField;
022: import org.joda.time.field.FieldUtils;
023:
024: /**
025: * Provides time calculations for the year of era component of time.
026: *
027: * @author Brian S O'Neill
028: * @since 1.0
029: */
030: final class GJYearOfEraDateTimeField extends DecoratedDateTimeField {
031:
032: private static final long serialVersionUID = -5961050944769862059L;
033:
034: private final BasicChronology iChronology;
035:
036: /**
037: * Restricted constructor.
038: */
039: GJYearOfEraDateTimeField(DateTimeField yearField,
040: BasicChronology chronology) {
041: super (yearField, DateTimeFieldType.yearOfEra());
042: iChronology = chronology;
043: }
044:
045: public int get(long instant) {
046: int year = getWrappedField().get(instant);
047: if (year <= 0) {
048: year = 1 - year;
049: }
050: return year;
051: }
052:
053: public long add(long instant, int years) {
054: return getWrappedField().add(instant, years);
055: }
056:
057: public long add(long instant, long years) {
058: return getWrappedField().add(instant, years);
059: }
060:
061: public long addWrapField(long instant, int years) {
062: return getWrappedField().addWrapField(instant, years);
063: }
064:
065: public int[] addWrapField(ReadablePartial instant, int fieldIndex,
066: int[] values, int years) {
067: return getWrappedField().addWrapField(instant, fieldIndex,
068: values, years);
069: }
070:
071: public int getDifference(long minuendInstant, long subtrahendInstant) {
072: return getWrappedField().getDifference(minuendInstant,
073: subtrahendInstant);
074: }
075:
076: public long getDifferenceAsLong(long minuendInstant,
077: long subtrahendInstant) {
078: return getWrappedField().getDifferenceAsLong(minuendInstant,
079: subtrahendInstant);
080: }
081:
082: /**
083: * Set the year component of the specified time instant.
084: *
085: * @param instant the time instant in millis to update.
086: * @param year the year (0,292278994) to update the time to.
087: * @return the updated time instant.
088: * @throws IllegalArgumentException if year is invalid.
089: */
090: public long set(long instant, int year) {
091: FieldUtils.verifyValueBounds(this , year, 1, getMaximumValue());
092: if (iChronology.getYear(instant) <= 0) {
093: year = 1 - year;
094: }
095: return super .set(instant, year);
096: }
097:
098: public int getMinimumValue() {
099: return 1;
100: }
101:
102: public int getMaximumValue() {
103: return getWrappedField().getMaximumValue();
104: }
105:
106: public long roundFloor(long instant) {
107: return getWrappedField().roundFloor(instant);
108: }
109:
110: public long roundCeiling(long instant) {
111: return getWrappedField().roundCeiling(instant);
112: }
113:
114: public long remainder(long instant) {
115: return getWrappedField().remainder(instant);
116: }
117:
118: /**
119: * Serialization singleton
120: */
121: private Object readResolve() {
122: return iChronology.yearOfEra();
123: }
124: }
|