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 java.util.Locale;
19
20 import org.joda.time.DateTimeConstants;
21 import org.joda.time.DateTimeFieldType;
22 import org.joda.time.DurationField;
23 import org.joda.time.field.PreciseDurationDateTimeField;
24
25 /**
26  * GJDayOfWeekDateTimeField provides time calculations for the
27  * day of the week component of time.
28  *
29  * @since 1.0
30  * @author Guy Allard
31  * @author Stephen Colebourne
32  * @author Brian S O'Neill
33  */

34 final class GJDayOfWeekDateTimeField extends PreciseDurationDateTimeField {
35     
36     /** Serialization version */
37     @SuppressWarnings("unused")
38     private static final long serialVersionUID = -3857947176719041436L;
39
40     private final BasicChronology iChronology;
41
42     /**
43      * Restricted constructor.
44      */

45     GJDayOfWeekDateTimeField(BasicChronology chronology, DurationField days) {
46         super(DateTimeFieldType.dayOfWeek(), days);
47         iChronology = chronology;
48     }
49
50     /**
51      * Get the value of the specified time instant.
52      * 
53      * @param instant  the time instant in millis to query
54      * @return the day of the week extracted from the input
55      */

56     public int get(long instant) {
57         return iChronology.getDayOfWeek(instant);
58     }
59
60     /**
61      * Get the textual value of the specified time instant.
62      * 
63      * @param fieldValue  the field value to query
64      * @param locale  the locale to use
65      * @return the day of the week, such as 'Monday'
66      */

67     public String getAsText(int fieldValue, Locale locale) {
68         return GJLocaleSymbols.forLocale(locale).dayOfWeekValueToText(fieldValue);
69     }
70
71     /**
72      * Get the abbreviated textual value of the specified time instant.
73      * 
74      * @param fieldValue  the field value to query
75      * @param locale  the locale to use
76      * @return the day of the week, such as 'Mon'
77      */

78     public String getAsShortText(int fieldValue, Locale locale) {
79         return GJLocaleSymbols.forLocale(locale).dayOfWeekValueToShortText(fieldValue);
80     }
81
82     /**
83      * Convert the specified text and locale into a value.
84      * 
85      * @param text  the text to convert
86      * @param locale  the locale to convert using
87      * @return the value extracted from the text
88      * @throws IllegalArgumentException if the text is invalid
89      */

90     protected int convertText(String text, Locale locale) {
91         return GJLocaleSymbols.forLocale(locale).dayOfWeekTextToValue(text);
92     }
93
94     public DurationField getRangeDurationField() {
95         return iChronology.weeks();
96     }
97
98     /**
99      * Get the minimum value that this field can have.
100      * 
101      * @return the field's minimum value
102      */

103     public int getMinimumValue() {
104         return DateTimeConstants.MONDAY;
105     }
106
107     /**
108      * Get the maximum value that this field can have.
109      * 
110      * @return the field's maximum value
111      */

112     public int getMaximumValue() {
113         return DateTimeConstants.SUNDAY;
114     }
115
116     /**
117      * Get the maximum length of the text returned by this field.
118      * 
119      * @param locale  the locale to use
120      * @return the maximum textual length
121      */

122     public int getMaximumTextLength(Locale locale) {
123         return GJLocaleSymbols.forLocale(locale).getDayOfWeekMaxTextLength();
124     }
125
126     /**
127      * Get the maximum length of the abbreviated text returned by this field.
128      * 
129      * @param locale  the locale to use
130      * @return the maximum abbreviated textual length
131      */

132     public int getMaximumShortTextLength(Locale locale) {
133         return GJLocaleSymbols.forLocale(locale).getDayOfWeekMaxShortTextLength();
134     }
135
136     /**
137      * Serialization singleton
138      */

139     private Object readResolve() {
140         return iChronology.dayOfWeek();
141     }
142 }
143