1 /*
2  *  Copyright 2001-2013 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     @SuppressWarnings("unused")
35     private static final long serialVersionUID = -1587436826395135328L;
36
37     private final BasicChronology iChronology;
38
39     /**
40      * Restricted constructor
41      */

42     BasicWeekOfWeekyearDateTimeField(BasicChronology chronology, DurationField weeks) {
43         super(DateTimeFieldType.weekOfWeekyear(), weeks);
44         iChronology = chronology;
45     }
46
47     /**
48      * Get the week of a week based year component of the specified time instant.
49      * 
50      * @see org.joda.time.DateTimeField#get(long)
51      * @param instant  the time instant in millis to query.
52      * @return the week of the year extracted from the input.
53      */

54     public int get(long instant) {
55         return iChronology.getWeekOfWeekyear(instant);
56     }
57
58     public DurationField getRangeDurationField() {
59         return iChronology.weekyears();
60     }
61
62     // 1970-01-01 is day of week 4, Thursday. The rounding methods need to
63     // apply a corrective alignment since weeks begin on day of week 1, Monday.
64
65     public long roundFloor(long instant) {
66         return super.roundFloor(instant + 3 * DateTimeConstants.MILLIS_PER_DAY)
67             - 3 * DateTimeConstants.MILLIS_PER_DAY;
68     }
69
70     public long roundCeiling(long instant) {
71         return super.roundCeiling(instant + 3 * DateTimeConstants.MILLIS_PER_DAY)
72             - 3 * DateTimeConstants.MILLIS_PER_DAY;
73     }
74
75     public long remainder(long instant) {
76         return super.remainder(instant + 3 * DateTimeConstants.MILLIS_PER_DAY);
77     }
78
79     public int getMinimumValue() {
80         return 1;
81     }
82
83     public int getMaximumValue() {
84         return 53;
85     }
86
87     public int getMaximumValue(long instant) {
88         int weekyear = iChronology.getWeekyear(instant);
89         return iChronology.getWeeksInYear(weekyear);
90     }
91
92     public int getMaximumValue(ReadablePartial partial) {
93         if (partial.isSupported(DateTimeFieldType.weekyear())) {
94             int weekyear = partial.get(DateTimeFieldType.weekyear());
95             return iChronology.getWeeksInYear(weekyear);
96         }
97         return 53;
98     }
99
100     public int getMaximumValue(ReadablePartial partial, int[] values) {
101         int size = partial.size();
102         for (int i = 0; i < size; i++) {
103             if (partial.getFieldType(i) == DateTimeFieldType.weekyear()) {
104                 int weekyear = values[i];
105                 return iChronology.getWeeksInYear(weekyear);
106             }
107         }
108         return 53;
109     }
110
111     protected int getMaximumValueForSet(long instant, int value) {
112         return value > 52 ? getMaximumValue(instant) : 52;
113     }
114
115     /**
116      * Serialization singleton
117      */

118     private Object readResolve() {
119         return iChronology.weekOfWeekyear();
120     }
121 }
122