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 month component of time.
025: *
026: * @author Guy Allard
027: * @author Stephen Colebourne
028: * @author Brian S O'Neill
029: * @since 1.1, refactored from GJDayOfMonthDateTimeField
030: */
031: final class BasicDayOfMonthDateTimeField extends
032: PreciseDurationDateTimeField {
033:
034: private static final long serialVersionUID = -4677223814028011723L;
035:
036: private final BasicChronology iChronology;
037:
038: /**
039: * Restricted constructor.
040: */
041: BasicDayOfMonthDateTimeField(BasicChronology chronology,
042: DurationField days) {
043: super (DateTimeFieldType.dayOfMonth(), days);
044: iChronology = chronology;
045: }
046:
047: //-----------------------------------------------------------------------
048: public int get(long instant) {
049: return iChronology.getDayOfMonth(instant);
050: }
051:
052: public DurationField getRangeDurationField() {
053: return iChronology.months();
054: }
055:
056: public int getMinimumValue() {
057: return 1;
058: }
059:
060: public int getMaximumValue() {
061: return iChronology.getDaysInMonthMax();
062: }
063:
064: public int getMaximumValue(long instant) {
065: return iChronology.getDaysInMonthMax(instant);
066: }
067:
068: public int getMaximumValue(ReadablePartial partial) {
069: if (partial.isSupported(DateTimeFieldType.monthOfYear())) {
070: int month = partial.get(DateTimeFieldType.monthOfYear());
071: if (partial.isSupported(DateTimeFieldType.year())) {
072: int year = partial.get(DateTimeFieldType.year());
073: return iChronology.getDaysInYearMonth(year, month);
074: }
075: return iChronology.getDaysInMonthMax(month);
076: }
077: return getMaximumValue();
078: }
079:
080: public int getMaximumValue(ReadablePartial partial, int[] values) {
081: int size = partial.size();
082: for (int i = 0; i < size; i++) {
083: if (partial.getFieldType(i) == DateTimeFieldType
084: .monthOfYear()) {
085: int month = values[i];
086: for (int j = 0; j < size; j++) {
087: if (partial.getFieldType(j) == DateTimeFieldType
088: .year()) {
089: int year = values[j];
090: return iChronology.getDaysInYearMonth(year,
091: month);
092: }
093: }
094: return iChronology.getDaysInMonthMax(month);
095: }
096: }
097: return getMaximumValue();
098: }
099:
100: protected int getMaximumValueForSet(long instant, int value) {
101: return iChronology.getDaysInMonthMaxForSet(instant, value);
102: }
103:
104: /**
105: * Serialization singleton
106: */
107: private Object readResolve() {
108: return iChronology.dayOfMonth();
109: }
110: }
|