01: /*
02: * Copyright 2001-2007 Stephen Colebourne
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.field;
17:
18: import org.joda.time.Chronology;
19: import org.joda.time.DateTimeField;
20:
21: /**
22: * Converts a strict DateTimeField into a lenient one. By being lenient, the
23: * set method accepts out of bounds values, performing an addition instead.
24: * <p>
25: * LenientDateTimeField is thread-safe and immutable.
26: *
27: * @author Brian S O'Neill
28: * @see org.joda.time.chrono.LenientChronology
29: * @see StrictDateTimeField
30: * @since 1.0
31: */
32: public class LenientDateTimeField extends DelegatedDateTimeField {
33:
34: private static final long serialVersionUID = 8714085824173290599L;
35:
36: private final Chronology iBase;
37:
38: /**
39: * Returns a lenient version of the given field. If it is already lenient,
40: * then it is returned as-is. Otherwise, a new LenientDateTimeField is
41: * returned.
42: */
43: public static DateTimeField getInstance(DateTimeField field,
44: Chronology base) {
45: if (field == null) {
46: return null;
47: }
48: if (field instanceof StrictDateTimeField) {
49: field = ((StrictDateTimeField) field).getWrappedField();
50: }
51: if (field.isLenient()) {
52: return field;
53: }
54: return new LenientDateTimeField(field, base);
55: }
56:
57: protected LenientDateTimeField(DateTimeField field, Chronology base) {
58: super (field);
59: iBase = base;
60: }
61:
62: public final boolean isLenient() {
63: return true;
64: }
65:
66: /**
67: * Set values which may be out of bounds by adding the difference between
68: * the new value and the current value.
69: */
70: public long set(long instant, int value) {
71: // lenient needs to handle time zone chronologies
72: // so we do the calculation using local milliseconds
73: long localInstant = iBase.getZone().convertUTCToLocal(instant);
74: long difference = FieldUtils.safeSubtract(value, get(instant));
75: localInstant = getType().getField(iBase.withUTC()).add(
76: localInstant, difference);
77: return iBase.getZone().convertLocalToUTC(localInstant, false);
78: }
79: }
|