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.field;
017:
018: import org.joda.time.DateTimeField;
019: import org.joda.time.DateTimeFieldType;
020: import org.joda.time.DurationField;
021: import org.joda.time.ReadablePartial;
022:
023: /**
024: * Wraps another field such that zero values are replaced with one more than
025: * it's maximum. This is particularly useful for implementing an clockhourOfDay
026: * field, where the midnight value of 0 is replaced with 24.
027: * <p>
028: * ZeroIsMaxDateTimeField is thread-safe and immutable.
029: *
030: * @author Brian S O'Neill
031: * @since 1.0
032: */
033: public final class ZeroIsMaxDateTimeField extends
034: DecoratedDateTimeField {
035:
036: private static final long serialVersionUID = 961749798233026866L;
037:
038: /**
039: * Constructor.
040: *
041: * @param field the base field
042: * @param type the field type this field will actually use
043: * @throws IllegalArgumentException if wrapped field's minimum value is not zero
044: */
045: public ZeroIsMaxDateTimeField(DateTimeField field,
046: DateTimeFieldType type) {
047: super (field, type);
048: if (field.getMinimumValue() != 0) {
049: throw new IllegalArgumentException(
050: "Wrapped field's minumum value must be zero");
051: }
052: }
053:
054: public int get(long instant) {
055: int value = getWrappedField().get(instant);
056: if (value == 0) {
057: value = getMaximumValue();
058: }
059: return value;
060: }
061:
062: public long add(long instant, int value) {
063: return getWrappedField().add(instant, value);
064: }
065:
066: public long add(long instant, long value) {
067: return getWrappedField().add(instant, value);
068: }
069:
070: public long addWrapField(long instant, int value) {
071: return getWrappedField().addWrapField(instant, value);
072: }
073:
074: public int[] addWrapField(ReadablePartial instant, int fieldIndex,
075: int[] values, int valueToAdd) {
076: return getWrappedField().addWrapField(instant, fieldIndex,
077: values, valueToAdd);
078: }
079:
080: public int getDifference(long minuendInstant, long subtrahendInstant) {
081: return getWrappedField().getDifference(minuendInstant,
082: subtrahendInstant);
083: }
084:
085: public long getDifferenceAsLong(long minuendInstant,
086: long subtrahendInstant) {
087: return getWrappedField().getDifferenceAsLong(minuendInstant,
088: subtrahendInstant);
089: }
090:
091: public long set(long instant, int value) {
092: int max = getMaximumValue();
093: FieldUtils.verifyValueBounds(this , value, 1, max);
094: if (value == max) {
095: value = 0;
096: }
097: return getWrappedField().set(instant, value);
098: }
099:
100: public boolean isLeap(long instant) {
101: return getWrappedField().isLeap(instant);
102: }
103:
104: public int getLeapAmount(long instant) {
105: return getWrappedField().getLeapAmount(instant);
106: }
107:
108: public DurationField getLeapDurationField() {
109: return getWrappedField().getLeapDurationField();
110: }
111:
112: /**
113: * Always returns 1.
114: *
115: * @return the minimum value of 1
116: */
117: public int getMinimumValue() {
118: return 1;
119: }
120:
121: /**
122: * Always returns 1.
123: *
124: * @return the minimum value of 1
125: */
126: public int getMinimumValue(long instant) {
127: return 1;
128: }
129:
130: /**
131: * Always returns 1.
132: *
133: * @return the minimum value of 1
134: */
135: public int getMinimumValue(ReadablePartial instant) {
136: return 1;
137: }
138:
139: /**
140: * Always returns 1.
141: *
142: * @return the minimum value of 1
143: */
144: public int getMinimumValue(ReadablePartial instant, int[] values) {
145: return 1;
146: }
147:
148: /**
149: * Get the maximum value for the field, which is one more than the wrapped
150: * field's maximum value.
151: *
152: * @return the maximum value
153: */
154: public int getMaximumValue() {
155: return getWrappedField().getMaximumValue() + 1;
156: }
157:
158: /**
159: * Get the maximum value for the field, which is one more than the wrapped
160: * field's maximum value.
161: *
162: * @return the maximum value
163: */
164: public int getMaximumValue(long instant) {
165: return getWrappedField().getMaximumValue(instant) + 1;
166: }
167:
168: /**
169: * Get the maximum value for the field, which is one more than the wrapped
170: * field's maximum value.
171: *
172: * @return the maximum value
173: */
174: public int getMaximumValue(ReadablePartial instant) {
175: return getWrappedField().getMaximumValue(instant) + 1;
176: }
177:
178: /**
179: * Get the maximum value for the field, which is one more than the wrapped
180: * field's maximum value.
181: *
182: * @return the maximum value
183: */
184: public int getMaximumValue(ReadablePartial instant, int[] values) {
185: return getWrappedField().getMaximumValue(instant, values) + 1;
186: }
187:
188: public long roundFloor(long instant) {
189: return getWrappedField().roundFloor(instant);
190: }
191:
192: public long roundCeiling(long instant) {
193: return getWrappedField().roundCeiling(instant);
194: }
195:
196: public long roundHalfFloor(long instant) {
197: return getWrappedField().roundHalfFloor(instant);
198: }
199:
200: public long roundHalfCeiling(long instant) {
201: return getWrappedField().roundHalfCeiling(instant);
202: }
203:
204: public long roundHalfEven(long instant) {
205: return getWrappedField().roundHalfEven(instant);
206: }
207:
208: public long remainder(long instant) {
209: return getWrappedField().remainder(instant);
210: }
211:
212: }
|