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.DateTimeConstants;
019: import org.joda.time.DateTimeFieldType;
020: import org.joda.time.DurationField;
021: import org.joda.time.ReadablePartial;
022: import org.joda.time.field.PreciseDurationDateTimeField;
023:
024: /**
025: * Provides time calculations for the week of a week based year component of time.
026: *
027: * @author Guy Allard
028: * @author Stephen Colebourne
029: * @author Brian S O'Neill
030: * @since 1.1, refactored from GJWeekOfWeekyearDateTimeField
031: */
032: final class BasicWeekOfWeekyearDateTimeField extends
033: PreciseDurationDateTimeField {
034:
035: private static final long serialVersionUID = -1587436826395135328L;
036:
037: private final BasicChronology iChronology;
038:
039: /**
040: * Restricted constructor
041: */
042: BasicWeekOfWeekyearDateTimeField(BasicChronology chronology,
043: DurationField weeks) {
044: super (DateTimeFieldType.weekOfWeekyear(), weeks);
045: iChronology = chronology;
046: }
047:
048: /**
049: * Get the week of a week based year component of the specified time instant.
050: *
051: * @see org.joda.time.DateTimeField#get(long)
052: * @param instant the time instant in millis to query.
053: * @return the week of the year extracted from the input.
054: */
055: public int get(long instant) {
056: return iChronology.getWeekOfWeekyear(instant);
057: }
058:
059: public DurationField getRangeDurationField() {
060: return iChronology.weekyears();
061: }
062:
063: // 1970-01-01 is day of week 4, Thursday. The rounding methods need to
064: // apply a corrective alignment since weeks begin on day of week 1, Monday.
065:
066: public long roundFloor(long instant) {
067: return super .roundFloor(instant + 3
068: * DateTimeConstants.MILLIS_PER_DAY)
069: - 3 * DateTimeConstants.MILLIS_PER_DAY;
070: }
071:
072: public long roundCeiling(long instant) {
073: return super .roundCeiling(instant + 3
074: * DateTimeConstants.MILLIS_PER_DAY)
075: - 3 * DateTimeConstants.MILLIS_PER_DAY;
076: }
077:
078: public long remainder(long instant) {
079: return super .remainder(instant + 3
080: * DateTimeConstants.MILLIS_PER_DAY);
081: }
082:
083: public int getMinimumValue() {
084: return 1;
085: }
086:
087: public int getMaximumValue() {
088: return 53;
089: }
090:
091: public int getMaximumValue(long instant) {
092: int weekyear = iChronology.getWeekyear(instant);
093: return iChronology.getWeeksInYear(weekyear);
094: }
095:
096: public int getMaximumValue(ReadablePartial partial) {
097: if (partial.isSupported(DateTimeFieldType.weekyear())) {
098: int weekyear = partial.get(DateTimeFieldType.weekyear());
099: return iChronology.getWeeksInYear(weekyear);
100: }
101: return 53;
102: }
103:
104: public int getMaximumValue(ReadablePartial partial, int[] values) {
105: int size = partial.size();
106: for (int i = 0; i < size; i++) {
107: if (partial.getFieldType(i) == DateTimeFieldType.weekyear()) {
108: int weekyear = values[i];
109: return iChronology.getWeeksInYear(weekyear);
110: }
111: }
112: return 53;
113: }
114:
115: protected int getMaximumValueForSet(long instant, int value) {
116: return value > 52 ? getMaximumValue(instant) : 52;
117: }
118:
119: /**
120: * Serialization singleton
121: */
122: private Object readResolve() {
123: return iChronology.weekOfWeekyear();
124: }
125: }
|