KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > chrono > BasicDayOfYearDateTimeField


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 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     private static final long serialVersionUID = -6821236822336841037L;
34
35     private final BasicChronology iChronology;
36
37     /**
38      * Restricted constructor
39      */

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

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

99     private Object JavaDoc readResolve() {
100         return iChronology.dayOfYear();
101     }
102 }
103
Popular Tags