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 java.util.Locale;
019:
020: import org.joda.time.DateTimeConstants;
021: import org.joda.time.DateTimeFieldType;
022: import org.joda.time.DurationField;
023: import org.joda.time.DurationFieldType;
024: import org.joda.time.field.BaseDateTimeField;
025: import org.joda.time.field.FieldUtils;
026: import org.joda.time.field.UnsupportedDurationField;
027:
028: /**
029: * Provides time calculations for the era component of time.
030: *
031: * @author Stephen Colebourne
032: * @author Brian S O'Neill
033: * @since 1.0
034: */
035: final class GJEraDateTimeField extends BaseDateTimeField {
036:
037: /** Serialization version */
038: private static final long serialVersionUID = 4240986525305515528L;
039:
040: private final BasicChronology iChronology;
041:
042: /**
043: * Restricted constructor
044: */
045: GJEraDateTimeField(BasicChronology chronology) {
046: super (DateTimeFieldType.era());
047: iChronology = chronology;
048: }
049:
050: public boolean isLenient() {
051: return false;
052: }
053:
054: /**
055: * Get the Era component of the specified time instant.
056: *
057: * @param instant the time instant in millis to query.
058: */
059: public int get(long instant) {
060: if (iChronology.getYear(instant) <= 0) {
061: return DateTimeConstants.BCE;
062: } else {
063: return DateTimeConstants.CE;
064: }
065: }
066:
067: public String getAsText(int fieldValue, Locale locale) {
068: return GJLocaleSymbols.forLocale(locale).eraValueToText(
069: fieldValue);
070: }
071:
072: /**
073: * Set the Era component of the specified time instant.
074: *
075: * @param instant the time instant in millis to update.
076: * @param era the era to update the time to.
077: * @return the updated time instant.
078: * @throws IllegalArgumentException if era is invalid.
079: */
080: public long set(long instant, int era) {
081: FieldUtils.verifyValueBounds(this , era, DateTimeConstants.BCE,
082: DateTimeConstants.CE);
083:
084: int oldEra = get(instant);
085: if (oldEra != era) {
086: int year = iChronology.getYear(instant);
087: return iChronology.setYear(instant, -year);
088: } else {
089: return instant;
090: }
091: }
092:
093: public long set(long instant, String text, Locale locale) {
094: return set(instant, GJLocaleSymbols.forLocale(locale)
095: .eraTextToValue(text));
096: }
097:
098: public long roundFloor(long instant) {
099: if (get(instant) == DateTimeConstants.CE) {
100: return iChronology.setYear(0, 1);
101: } else {
102: return Long.MIN_VALUE;
103: }
104: }
105:
106: public long roundCeiling(long instant) {
107: if (get(instant) == DateTimeConstants.BCE) {
108: return iChronology.setYear(0, 1);
109: } else {
110: return Long.MAX_VALUE;
111: }
112: }
113:
114: public long roundHalfFloor(long instant) {
115: // In reality, the era is infinite, so there is no halfway point.
116: return roundFloor(instant);
117: }
118:
119: public long roundHalfCeiling(long instant) {
120: // In reality, the era is infinite, so there is no halfway point.
121: return roundFloor(instant);
122: }
123:
124: public long roundHalfEven(long instant) {
125: // In reality, the era is infinite, so there is no halfway point.
126: return roundFloor(instant);
127: }
128:
129: public DurationField getDurationField() {
130: return UnsupportedDurationField.getInstance(DurationFieldType
131: .eras());
132: }
133:
134: public DurationField getRangeDurationField() {
135: return null;
136: }
137:
138: public int getMinimumValue() {
139: return DateTimeConstants.BCE;
140: }
141:
142: public int getMaximumValue() {
143: return DateTimeConstants.CE;
144: }
145:
146: public int getMaximumTextLength(Locale locale) {
147: return GJLocaleSymbols.forLocale(locale).getEraMaxTextLength();
148: }
149:
150: /**
151: * Serialization singleton
152: */
153: private Object readResolve() {
154: return iChronology.era();
155: }
156: }
|