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.DateTimeFieldType;
019: import org.joda.time.DurationField;
020: import org.joda.time.ReadablePartial;
021: import org.joda.time.field.PreciseDurationDateTimeField;
022:
023: /**
024: * Provides time calculations for the day of the year component of time.
025: *
026: * @author Guy Allard
027: * @author Stephen Colebourne
028: * @author Brian S O'Neill
029: * @since 1.1, refactored from GJDayOfYearDateTimeField
030: */
031: final class BasicDayOfYearDateTimeField extends
032: PreciseDurationDateTimeField {
033:
034: private static final long serialVersionUID = -6821236822336841037L;
035:
036: private final BasicChronology iChronology;
037:
038: /**
039: * Restricted constructor
040: */
041: BasicDayOfYearDateTimeField(BasicChronology chronology,
042: DurationField days) {
043: super (DateTimeFieldType.dayOfYear(), days);
044: iChronology = chronology;
045: }
046:
047: /**
048: * Get the day of the year component of the specified time instant.
049: *
050: * @param instant the time instant in millis to query.
051: * @return the day of the year extracted from the input.
052: */
053: public int get(long instant) {
054: return iChronology.getDayOfYear(instant);
055: }
056:
057: public DurationField getRangeDurationField() {
058: return iChronology.years();
059: }
060:
061: public int getMinimumValue() {
062: return 1;
063: }
064:
065: public int getMaximumValue() {
066: return iChronology.getDaysInYearMax();
067: }
068:
069: public int getMaximumValue(long instant) {
070: int year = iChronology.getYear(instant);
071: return iChronology.getDaysInYear(year);
072: }
073:
074: public int getMaximumValue(ReadablePartial partial) {
075: if (partial.isSupported(DateTimeFieldType.year())) {
076: int year = partial.get(DateTimeFieldType.year());
077: return iChronology.getDaysInYear(year);
078: }
079: return iChronology.getDaysInYearMax();
080: }
081:
082: public int getMaximumValue(ReadablePartial partial, int[] values) {
083: int size = partial.size();
084: for (int i = 0; i < size; i++) {
085: if (partial.getFieldType(i) == DateTimeFieldType.year()) {
086: int year = values[i];
087: return iChronology.getDaysInYear(year);
088: }
089: }
090: return iChronology.getDaysInYearMax();
091: }
092:
093: protected int getMaximumValueForSet(long instant, int value) {
094: int maxLessOne = iChronology.getDaysInYearMax() - 1;
095: return (value > maxLessOne || value < 1) ? getMaximumValue(instant)
096: : maxLessOne;
097: }
098:
099: /**
100: * Serialization singleton
101: */
102: private Object readResolve() {
103: return iChronology.dayOfYear();
104: }
105: }
|