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 year component of time.
25  *
26  * @author Guy Allard
27  * @author Stephen Colebourne
28  * @author Brian S O'Neill
29  * @since 1.1, refactored from GJDayOfYearDateTimeField
30  */

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

41     BasicDayOfYearDateTimeField(BasicChronology chronology, DurationField days) {
42         super(DateTimeFieldType.dayOfYear(), days);
43         iChronology = chronology;
44     }
45
46     /**
47      * Get the day of the year component of the specified time instant.
48      * 
49      * @param instant  the time instant in millis to query.
50      * @return the day of the year extracted from the input.
51      */

52     public int get(long instant) {
53         return iChronology.getDayOfYear(instant);
54     }
55
56     public DurationField getRangeDurationField() {
57         return iChronology.years();
58     }
59
60     public int getMinimumValue() {
61         return 1;
62     }
63
64     public int getMaximumValue() {
65         return iChronology.getDaysInYearMax();
66     }
67
68     public int getMaximumValue(long instant) {
69         int year = iChronology.getYear(instant);
70         return iChronology.getDaysInYear(year);
71     }
72
73     public int getMaximumValue(ReadablePartial partial) {
74         if (partial.isSupported(DateTimeFieldType.year())) {
75             int year = partial.get(DateTimeFieldType.year());
76             return iChronology.getDaysInYear(year);
77         }
78         return iChronology.getDaysInYearMax();
79     }
80
81     public int getMaximumValue(ReadablePartial partial, int[] values) {
82         int size = partial.size();
83         for (int i = 0; i < size; i++) {
84             if (partial.getFieldType(i) == DateTimeFieldType.year()) {
85                 int year = values[i];
86                 return iChronology.getDaysInYear(year);
87             }
88         }
89         return iChronology.getDaysInYearMax();
90     }
91
92     protected int getMaximumValueForSet(long instant, int value) {
93         int maxLessOne = iChronology.getDaysInYearMax() - 1;
94         return (value > maxLessOne || value < 1) ? getMaximumValue(instant) : maxLessOne;
95     }
96
97     @Override
98     public boolean isLeap(long instant) {
99         return iChronology.isLeapDay(instant);
100     }
101
102     /**
103      * Serialization singleton
104      */

105     private Object readResolve() {
106         return iChronology.dayOfYear();
107     }
108 }
109