001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.chrono;
017:
018: import java.util.Locale;
019:
020: import org.joda.time.DateTimeConstants;
021: import org.joda.time.DateTimeFieldType;
022: import org.joda.time.DurationField;
023: import org.joda.time.field.PreciseDurationDateTimeField;
024:
025: /**
026: * GJDayOfWeekDateTimeField provides time calculations for the
027: * day of the week component of time.
028: *
029: * @since 1.0
030: * @author Guy Allard
031: * @author Stephen Colebourne
032: * @author Brian S O'Neill
033: */
034: final class GJDayOfWeekDateTimeField extends
035: PreciseDurationDateTimeField {
036:
037: /** Serialization version */
038: private static final long serialVersionUID = -3857947176719041436L;
039:
040: private final BasicChronology iChronology;
041:
042: /**
043: * Restricted constructor.
044: */
045: GJDayOfWeekDateTimeField(BasicChronology chronology,
046: DurationField days) {
047: super (DateTimeFieldType.dayOfWeek(), days);
048: iChronology = chronology;
049: }
050:
051: /**
052: * Get the value of the specified time instant.
053: *
054: * @param instant the time instant in millis to query
055: * @return the day of the week extracted from the input
056: */
057: public int get(long instant) {
058: return iChronology.getDayOfWeek(instant);
059: }
060:
061: /**
062: * Get the textual value of the specified time instant.
063: *
064: * @param fieldValue the field value to query
065: * @param locale the locale to use
066: * @return the day of the week, such as 'Monday'
067: */
068: public String getAsText(int fieldValue, Locale locale) {
069: return GJLocaleSymbols.forLocale(locale).dayOfWeekValueToText(
070: fieldValue);
071: }
072:
073: /**
074: * Get the abbreviated textual value of the specified time instant.
075: *
076: * @param fieldValue the field value to query
077: * @param locale the locale to use
078: * @return the day of the week, such as 'Mon'
079: */
080: public String getAsShortText(int fieldValue, Locale locale) {
081: return GJLocaleSymbols.forLocale(locale)
082: .dayOfWeekValueToShortText(fieldValue);
083: }
084:
085: /**
086: * Convert the specified text and locale into a value.
087: *
088: * @param text the text to convert
089: * @param locale the locale to convert using
090: * @return the value extracted from the text
091: * @throws IllegalArgumentException if the text is invalid
092: */
093: protected int convertText(String text, Locale locale) {
094: return GJLocaleSymbols.forLocale(locale).dayOfWeekTextToValue(
095: text);
096: }
097:
098: public DurationField getRangeDurationField() {
099: return iChronology.weeks();
100: }
101:
102: /**
103: * Get the minimum value that this field can have.
104: *
105: * @return the field's minimum value
106: */
107: public int getMinimumValue() {
108: return DateTimeConstants.MONDAY;
109: }
110:
111: /**
112: * Get the maximum value that this field can have.
113: *
114: * @return the field's maximum value
115: */
116: public int getMaximumValue() {
117: return DateTimeConstants.SUNDAY;
118: }
119:
120: /**
121: * Get the maximum length of the text returned by this field.
122: *
123: * @param locale the locale to use
124: * @return the maximum textual length
125: */
126: public int getMaximumTextLength(Locale locale) {
127: return GJLocaleSymbols.forLocale(locale)
128: .getDayOfWeekMaxTextLength();
129: }
130:
131: /**
132: * Get the maximum length of the abbreviated text returned by this field.
133: *
134: * @param locale the locale to use
135: * @return the maximum abbreviated textual length
136: */
137: public int getMaximumShortTextLength(Locale locale) {
138: return GJLocaleSymbols.forLocale(locale)
139: .getDayOfWeekMaxShortTextLength();
140: }
141:
142: /**
143: * Serialization singleton
144: */
145: private Object readResolve() {
146: return iChronology.dayOfWeek();
147: }
148: }
|