| 1 | /* |
| 2 | * Copyright 2001-2005 Stephen Colebourne |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.joda.time.chrono; |
| 17 | |
| 18 | import org.joda.time.DateTimeConstants; |
| 19 | import org.joda.time.DateTimeFieldType; |
| 20 | import org.joda.time.DurationField; |
| 21 | import org.joda.time.ReadablePartial; |
| 22 | import org.joda.time.field.PreciseDurationDateTimeField; |
| 23 | |
| 24 | /** |
| 25 | * Provides time calculations for the week of a week based year component of time. |
| 26 | * |
| 27 | * @author Guy Allard |
| 28 | * @author Stephen Colebourne |
| 29 | * @author Brian S O'Neill |
| 30 | * @since 1.1, refactored from GJWeekOfWeekyearDateTimeField |
| 31 | */ |
| 32 | final class BasicWeekOfWeekyearDateTimeField extends PreciseDurationDateTimeField { |
| 33 | |
| 34 | private static final long serialVersionUID = -1587436826395135328L; |
| 35 | |
| 36 | private final BasicChronology iChronology; |
| 37 | |
| 38 | /** |
| 39 | * Restricted constructor |
| 40 | */ |
| 41 | BasicWeekOfWeekyearDateTimeField(BasicChronology chronology, DurationField weeks) { |
| 42 | super(DateTimeFieldType.weekOfWeekyear(), weeks); |
| 43 | iChronology = chronology; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get the week of a week based year component of the specified time instant. |
| 48 | * |
| 49 | * @see org.joda.time.DateTimeField#get(long) |
| 50 | * @param instant the time instant in millis to query. |
| 51 | * @return the week of the year extracted from the input. |
| 52 | */ |
| 53 | public int get(long instant) { |
| 54 | return iChronology.getWeekOfWeekyear(instant); |
| 55 | } |
| 56 | |
| 57 | public DurationField getRangeDurationField() { |
| 58 | return iChronology.weekyears(); |
| 59 | } |
| 60 | |
| 61 | // 1970-01-01 is day of week 4, Thursday. The rounding methods need to |
| 62 | // apply a corrective alignment since weeks begin on day of week 1, Monday. |
| 63 | |
| 64 | public long roundFloor(long instant) { |
| 65 | return super.roundFloor(instant + 3 * DateTimeConstants.MILLIS_PER_DAY) |
| 66 | - 3 * DateTimeConstants.MILLIS_PER_DAY; |
| 67 | } |
| 68 | |
| 69 | public long roundCeiling(long instant) { |
| 70 | return super.roundCeiling(instant + 3 * DateTimeConstants.MILLIS_PER_DAY) |
| 71 | - 3 * DateTimeConstants.MILLIS_PER_DAY; |
| 72 | } |
| 73 | |
| 74 | public long remainder(long instant) { |
| 75 | return super.remainder(instant + 3 * DateTimeConstants.MILLIS_PER_DAY); |
| 76 | } |
| 77 | |
| 78 | public int getMinimumValue() { |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | public int getMaximumValue() { |
| 83 | return 53; |
| 84 | } |
| 85 | |
| 86 | public int getMaximumValue(long instant) { |
| 87 | int weekyear = iChronology.getWeekyear(instant); |
| 88 | return iChronology.getWeeksInYear(weekyear); |
| 89 | } |
| 90 | |
| 91 | public int getMaximumValue(ReadablePartial partial) { |
| 92 | if (partial.isSupported(DateTimeFieldType.weekyear())) { |
| 93 | int weekyear = partial.get(DateTimeFieldType.weekyear()); |
| 94 | return iChronology.getWeeksInYear(weekyear); |
| 95 | } |
| 96 | return 53; |
| 97 | } |
| 98 | |
| 99 | public int getMaximumValue(ReadablePartial partial, int[] values) { |
| 100 | int size = partial.size(); |
| 101 | for (int i = 0; i < size; i++) { |
| 102 | if (partial.getFieldType(i) == DateTimeFieldType.weekyear()) { |
| 103 | int weekyear = values[i]; |
| 104 | return iChronology.getWeeksInYear(weekyear); |
| 105 | } |
| 106 | } |
| 107 | return 53; |
| 108 | } |
| 109 | |
| 110 | protected int getMaximumValueForSet(long instant, int value) { |
| 111 | return value > 52 ? getMaximumValue(instant) : 52; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Serialization singleton |
| 116 | */ |
| 117 | private Object readResolve() { |
| 118 | return iChronology.weekOfWeekyear(); |
| 119 | } |
| 120 | } |