1 /*
2 * Copyright 2001-2005 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.field;
17
18 import org.joda.time.Chronology;
19 import org.joda.time.DateTimeField;
20
21 /**
22 * Wraps another field such that a certain value is added back into
23 * the sequence of numbers.
24 * <p>
25 * This reverses the effect of SkipDateTimeField. This isn't very
26 * elegant.
27 * <p>
28 * SkipUndoDateTimeField is thread-safe and immutable.
29 *
30 * @author Brian S O'Neill
31 * @author Stephen Colebourne
32 * @since 1.0
33 */
34 public final class SkipUndoDateTimeField extends DelegatedDateTimeField {
35
36 /** Serialization version. */
37 private static final long serialVersionUID = -5875876968979L;
38
39 /** The chronology to wrap. */
40 private final Chronology iChronology;
41 /** The value to skip. */
42 private final int iSkip;
43 /** The calculated minimum value. */
44 private transient int iMinValue;
45
46 /**
47 * Constructor that reinserts zero.
48 *
49 * @param chronology the chronoogy to use
50 * @param field the field to skip zero on
51 */
52 public SkipUndoDateTimeField(Chronology chronology, DateTimeField field) {
53 this(chronology, field, 0);
54 }
55
56 /**
57 * Constructor.
58 *
59 * @param chronology the chronoogy to use
60 * @param field the field to skip zero on
61 * @param skip the value to skip
62 */
63 public SkipUndoDateTimeField(Chronology chronology, DateTimeField field, int skip) {
64 super(field);
65 iChronology = chronology;
66 int min = super.getMinimumValue();
67 if (min < skip) {
68 iMinValue = min + 1;
69 } else if (min == skip + 1) {
70 iMinValue = skip;
71 } else {
72 iMinValue = min;
73 }
74 iSkip = skip;
75 }
76
77 //-----------------------------------------------------------------------
78 public int get(long millis) {
79 int value = super.get(millis);
80 if (value < iSkip) {
81 value++;
82 }
83 return value;
84 }
85
86 public long set(long millis, int value) {
87 FieldUtils.verifyValueBounds(this, value, iMinValue, getMaximumValue());
88 if (value <= iSkip) {
89 value--;
90 }
91 return super.set(millis, value);
92 }
93
94 public int getMinimumValue() {
95 return iMinValue;
96 }
97
98 private Object readResolve() {
99 return getType().getField(iChronology);
100 }
101
102 }