View Javadoc

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