1 /*
2  *  Copyright 2001-2014 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.DateTimeFieldType;
19 import org.joda.time.DurationField;
20 import org.joda.time.ReadablePartial;
21 import org.joda.time.field.PreciseDurationDateTimeField;
22
23 /**
24  * Provides time calculations for the day of the month component of time.
25  *
26  * @author Guy Allard
27  * @author Stephen Colebourne
28  * @author Brian S O'Neill
29  * @since 1.1, refactored from GJDayOfMonthDateTimeField
30  */

31 final class BasicDayOfMonthDateTimeField extends PreciseDurationDateTimeField {
32
33     @SuppressWarnings("unused")
34     private static final long serialVersionUID = -4677223814028011723L;
35
36     private final BasicChronology iChronology;
37
38     /**
39      * Restricted constructor.
40      */

41     BasicDayOfMonthDateTimeField(BasicChronology chronology, DurationField days) {
42         super(DateTimeFieldType.dayOfMonth(), days);
43         iChronology = chronology;
44     }
45
46     //-----------------------------------------------------------------------
47     public int get(long instant) {
48         return iChronology.getDayOfMonth(instant);
49     }
50
51     public DurationField getRangeDurationField() {
52         return iChronology.months();
53     }
54
55     public int getMinimumValue() {
56         return 1;
57     }
58
59     public int getMaximumValue() {
60         return iChronology.getDaysInMonthMax();
61     }
62
63     public int getMaximumValue(long instant) {
64         return iChronology.getDaysInMonthMax(instant);
65     }
66
67     public int getMaximumValue(ReadablePartial partial) {
68         if (partial.isSupported(DateTimeFieldType.monthOfYear())) {
69             int month = partial.get(DateTimeFieldType.monthOfYear());
70             if (partial.isSupported(DateTimeFieldType.year())) {
71                 int year = partial.get(DateTimeFieldType.year());
72                 return iChronology.getDaysInYearMonth(year, month);
73             }
74             return iChronology.getDaysInMonthMax(month);
75         }
76         return getMaximumValue();
77     }
78
79     public int getMaximumValue(ReadablePartial partial, int[] values) {
80         int size = partial.size();
81         for (int i = 0; i < size; i++) {
82             if (partial.getFieldType(i) == DateTimeFieldType.monthOfYear()) {
83                 int month = values[i];
84                 for (int j = 0; j < size; j++) {
85                     if (partial.getFieldType(j) == DateTimeFieldType.year()) {
86                         int year = values[j];
87                         return iChronology.getDaysInYearMonth(year, month);
88                     }
89                 }
90                 return iChronology.getDaysInMonthMax(month);
91             }
92         }
93         return getMaximumValue();
94     }
95
96     protected int getMaximumValueForSet(long instant, int value) {
97         return iChronology.getDaysInMonthMaxForSet(instant, value);
98     }
99
100     @Override
101     public boolean isLeap(long instant) {
102         return iChronology.isLeapDay(instant);
103     }
104
105     /**
106      * Serialization singleton
107      */

108     private Object readResolve() {
109         return iChronology.dayOfMonth();
110     }
111 }
112