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.Chronology;
019: import org.joda.time.DateTimeField;
020:
021: /**
022: * Wraps another field such that a certain value is added back into
023: * the sequence of numbers.
024: * <p>
025: * This reverses the effect of SkipDateTimeField. This isn't very
026: * elegant.
027: * <p>
028: * SkipUndoDateTimeField is thread-safe and immutable.
029: *
030: * @author Brian S O'Neill
031: * @author Stephen Colebourne
032: * @since 1.0
033: */
034: public final class SkipUndoDateTimeField extends DelegatedDateTimeField {
035:
036: /** Serialization version. */
037: private static final long serialVersionUID = -5875876968979L;
038:
039: /** The chronology to wrap. */
040: private final Chronology iChronology;
041: /** The value to skip. */
042: private final int iSkip;
043: /** The calculated minimum value. */
044: private transient int iMinValue;
045:
046: /**
047: * Constructor that reinserts zero.
048: *
049: * @param chronology the chronoogy to use
050: * @param field the field to skip zero on
051: */
052: public SkipUndoDateTimeField(Chronology chronology,
053: DateTimeField field) {
054: this (chronology, field, 0);
055: }
056:
057: /**
058: * Constructor.
059: *
060: * @param chronology the chronoogy to use
061: * @param field the field to skip zero on
062: * @param skip the value to skip
063: */
064: public SkipUndoDateTimeField(Chronology chronology,
065: DateTimeField field, int skip) {
066: super (field);
067: iChronology = chronology;
068: int min = super .getMinimumValue();
069: if (min < skip) {
070: iMinValue = min + 1;
071: } else if (min == skip + 1) {
072: iMinValue = skip;
073: } else {
074: iMinValue = min;
075: }
076: iSkip = skip;
077: }
078:
079: //-----------------------------------------------------------------------
080: public int get(long millis) {
081: int value = super .get(millis);
082: if (value < iSkip) {
083: value++;
084: }
085: return value;
086: }
087:
088: public long set(long millis, int value) {
089: FieldUtils.verifyValueBounds(this , value, iMinValue,
090: getMaximumValue());
091: if (value <= iSkip) {
092: value--;
093: }
094: return super .set(millis, value);
095: }
096:
097: public int getMinimumValue() {
098: return iMinValue;
099: }
100:
101: private Object readResolve() {
102: return getType().getField(iChronology);
103: }
104:
105: }
|