View Javadoc

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.DateTimeField;
19  import org.joda.time.DateTimeFieldType;
20  import org.joda.time.DurationField;
21  import org.joda.time.ReadablePartial;
22  
23  /**
24   * Wraps another field such that zero values are replaced with one more than
25   * it's maximum. This is particularly useful for implementing an clockhourOfDay
26   * field, where the midnight value of 0 is replaced with 24.
27   * <p>
28   * ZeroIsMaxDateTimeField is thread-safe and immutable.
29   *
30   * @author Brian S O'Neill
31   * @since 1.0
32   */
33  public final class ZeroIsMaxDateTimeField extends DecoratedDateTimeField {
34  
35      private static final long serialVersionUID = 961749798233026866L;
36  
37      /**
38       * Constructor.
39       * 
40       * @param field  the base field
41       * @param type  the field type this field will actually use
42       * @throws IllegalArgumentException if wrapped field's minimum value is not zero
43       */
44      public ZeroIsMaxDateTimeField(DateTimeField field, DateTimeFieldType type) {
45          super(field, type);
46          if (field.getMinimumValue() != 0) {
47              throw new IllegalArgumentException("Wrapped field's minumum value must be zero");
48          }
49      }
50  
51      public int get(long instant) {
52          int value = getWrappedField().get(instant);
53          if (value == 0) {
54              value = getMaximumValue();
55          }
56          return value;
57      }
58  
59      public long add(long instant, int value) {
60          return getWrappedField().add(instant, value);
61      }
62  
63      public long add(long instant, long value) {
64          return getWrappedField().add(instant, value);
65      }
66  
67      public long addWrapField(long instant, int value) {
68          return getWrappedField().addWrapField(instant, value);
69      }
70  
71      public int[] addWrapField(ReadablePartial instant, int fieldIndex, int[] values, int valueToAdd) {
72          return getWrappedField().addWrapField(instant, fieldIndex, values, valueToAdd);
73      }
74  
75      public int getDifference(long minuendInstant, long subtrahendInstant) {
76          return getWrappedField().getDifference(minuendInstant, subtrahendInstant);
77      }
78  
79      public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
80          return getWrappedField().getDifferenceAsLong(minuendInstant, subtrahendInstant);
81      }
82  
83      public long set(long instant, int value) {
84          int max = getMaximumValue();
85          FieldUtils.verifyValueBounds(this, value, 1, max);
86          if (value == max) {
87              value = 0;
88          }
89          return getWrappedField().set(instant, value);
90      }
91  
92      public boolean isLeap(long instant) {
93          return getWrappedField().isLeap(instant);
94      }
95  
96      public int getLeapAmount(long instant) {
97          return getWrappedField().getLeapAmount(instant);
98      }
99  
100     public DurationField getLeapDurationField() {
101         return getWrappedField().getLeapDurationField();
102     }
103 
104     /**
105      * Always returns 1.
106      * 
107      * @return the minimum value of 1
108      */
109     public int getMinimumValue() {
110         return 1;
111     }
112 
113     /**
114      * Always returns 1.
115      * 
116      * @return the minimum value of 1
117      */
118     public int getMinimumValue(long instant) {
119         return 1;
120     }
121 
122     /**
123      * Always returns 1.
124      * 
125      * @return the minimum value of 1
126      */
127     public int getMinimumValue(ReadablePartial instant) {
128         return 1;
129     }
130 
131     /**
132      * Always returns 1.
133      * 
134      * @return the minimum value of 1
135      */
136     public int getMinimumValue(ReadablePartial instant, int[] values) {
137         return 1;
138     }
139 
140     /**
141      * Get the maximum value for the field, which is one more than the wrapped
142      * field's maximum value.
143      * 
144      * @return the maximum value
145      */
146     public int getMaximumValue() {
147         return getWrappedField().getMaximumValue() + 1;
148     }
149 
150     /**
151      * Get the maximum value for the field, which is one more than the wrapped
152      * field's maximum value.
153      * 
154      * @return the maximum value
155      */
156     public int getMaximumValue(long instant) {
157         return getWrappedField().getMaximumValue(instant) + 1;
158     }
159 
160     /**
161      * Get the maximum value for the field, which is one more than the wrapped
162      * field's maximum value.
163      * 
164      * @return the maximum value
165      */
166     public int getMaximumValue(ReadablePartial instant) {
167         return getWrappedField().getMaximumValue(instant) + 1;
168     }
169 
170     /**
171      * Get the maximum value for the field, which is one more than the wrapped
172      * field's maximum value.
173      * 
174      * @return the maximum value
175      */
176     public int getMaximumValue(ReadablePartial instant, int[] values) {
177         return getWrappedField().getMaximumValue(instant, values) + 1;
178     }
179 
180     public long roundFloor(long instant) {
181         return getWrappedField().roundFloor(instant);
182     }
183 
184     public long roundCeiling(long instant) {
185         return getWrappedField().roundCeiling(instant);
186     }
187 
188     public long roundHalfFloor(long instant) {
189         return getWrappedField().roundHalfFloor(instant);
190     }
191 
192     public long roundHalfCeiling(long instant) {
193         return getWrappedField().roundHalfCeiling(instant);
194     }
195 
196     public long roundHalfEven(long instant) {
197         return getWrappedField().roundHalfEven(instant);
198     }
199 
200     public long remainder(long instant) {
201         return getWrappedField().remainder(instant);
202     }
203 
204 }