-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDurationFieldType_278.java
More file actions
352 lines (321 loc) · 10.5 KB
/
DurationFieldType_278.java
File metadata and controls
352 lines (321 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright 2001-2005 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.time;
import java.io.Serializable;
/**
* Identifies a duration field, such as years or minutes, in a chronology-neutral way.
* <p>
* A duration field type defines the type of the field, such as hours.
* If does not directly enable any calculations, however it does provide a
* {@link #getField(Chronology)} method that returns the actual calculation engine
* for a particular chronology.
* <p>
* Instances of <code>DurationFieldType</code> are singletons.
* They can be compared using <code>==</code>.
* <p>
* If required, you can create your own field, for example a quarters.
* You must create a subclass of <code>DurationFieldType</code> that defines the field type.
* This class returns the actual calculation engine from {@link #getField(Chronology)}.
*
* @author Stephen Colebourne
* @author Brian S O'Neill
* @since 1.0
*/
public abstract class DurationFieldType implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 8765135187319L;
// Ordinals for standard field types.
static final byte
ERAS = 1,
CENTURIES = 2,
WEEKYEARS = 3,
YEARS = 4,
MONTHS = 5,
WEEKS = 6,
DAYS = 7,
HALFDAYS = 8,
HOURS = 9,
MINUTES = 10,
SECONDS = 11,
MILLIS = 12;
/** The eras field type. */
static final DurationFieldType ERAS_TYPE = new StandardDurationFieldType("eras", ERAS);
/** The centuries field type. */
static final DurationFieldType CENTURIES_TYPE = new StandardDurationFieldType("centuries", CENTURIES);
/** The weekyears field type. */
static final DurationFieldType WEEKYEARS_TYPE = new StandardDurationFieldType("weekyears", WEEKYEARS);
/** The years field type. */
static final DurationFieldType YEARS_TYPE = new StandardDurationFieldType("years", YEARS);
/** The months field type. */
static final DurationFieldType MONTHS_TYPE = new StandardDurationFieldType("months", MONTHS);
/** The weeks field type. */
static final DurationFieldType WEEKS_TYPE = new StandardDurationFieldType("weeks", WEEKS);
/** The days field type. */
static final DurationFieldType DAYS_TYPE = new StandardDurationFieldType("days", DAYS);
/** The halfdays field type. */
static final DurationFieldType HALFDAYS_TYPE = new StandardDurationFieldType("halfdays", HALFDAYS);
/** The hours field type. */
static final DurationFieldType HOURS_TYPE = new StandardDurationFieldType("hours", HOURS);
/** The minutes field type. */
static final DurationFieldType MINUTES_TYPE = new StandardDurationFieldType("minutes", MINUTES);
/** The seconds field type. */
static final DurationFieldType SECONDS_TYPE = new StandardDurationFieldType("seconds", SECONDS);
/** The millis field type. */
static final DurationFieldType MILLIS_TYPE = new StandardDurationFieldType("millis", MILLIS);
/** The name of the field type. */
private final String iName;
//-----------------------------------------------------------------------
/**
* Constructor.
*
* @param name the name to use, which by convention, are plural.
*/
protected DurationFieldType(String name) {
super();
iName = name;
}
//-----------------------------------------------------------------------
/**
* Get the millis field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType millis() {
return MILLIS_TYPE;
}
/**
* Get the seconds field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType seconds() {
return SECONDS_TYPE;
}
/**
* Get the minutes field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType minutes() {
return MINUTES_TYPE;
}
/**
* Get the hours field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType hours() {
return HOURS_TYPE;
}
/**
* Get the halfdays field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType halfdays() {
return HALFDAYS_TYPE;
}
//-----------------------------------------------------------------------
/**
* Get the days field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType days() {
return DAYS_TYPE;
}
/**
* Get the weeks field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType weeks() {
return WEEKS_TYPE;
}
/**
* Get the weekyears field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType weekyears() {
return WEEKYEARS_TYPE;
}
/**
* Get the months field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType months() {
return MONTHS_TYPE;
}
/**
* Get the years field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType years() {
return YEARS_TYPE;
}
/**
* Get the centuries field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType centuries() {
return CENTURIES_TYPE;
}
/**
* Get the eras field type.
*
* @return the DateTimeFieldType constant
*/
public static DurationFieldType eras() {
return ERAS_TYPE;
}
//-----------------------------------------------------------------------
/**
* Get the name of the field.
* By convention, names are plural.
*
* @return field name
*/
public String getName() {
return iName;
}
/**
* Gets a suitable field for this type from the given Chronology.
*
* @param chronology the chronology to use, null means ISOChronology in default zone
* @return a suitable field
*/
public abstract DurationField getField(Chronology chronology);
/**
* Checks whether this field supported in the given Chronology.
*
* @param chronology the chronology to use, null means ISOChronology in default zone
* @return true if supported
*/
public boolean isSupported(Chronology chronology) {
return getField(chronology).isSupported();
}
/**
* Get a suitable debug string.
*
* @return debug string
*/
public String toString() {
return getName();
}
private static class StandardDurationFieldType extends DurationFieldType {
/** Serialization version */
private static final long serialVersionUID = 31156755687123L;
/** The ordinal of the standard field type, for switch statements */
private final byte iOrdinal;
/**
* Constructor.
*
* @param name the name to use
*/
StandardDurationFieldType(String name, byte ordinal) {
super(name);
iOrdinal = ordinal;
}
/** @inheritdoc */
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof StandardDurationFieldType) {
return iOrdinal == ((StandardDurationFieldType) obj).iOrdinal;
}
return false;
}
/** @inheritdoc */
@Override
public int hashCode() {
return (1 >> iOrdinal);
}
public DurationField getField(Chronology chronology) {
chronology = DateTimeUtils.getChronology(chronology);
switch (iOrdinal) {
case ERAS:
return chronology.eras();
case CENTURIES:
return chronology.centuries();
case WEEKYEARS:
return chronology.weekyears();
case YEARS:
return chronology.years();
case MONTHS:
return chronology.months();
case WEEKS:
return chronology.weeks();
case DAYS:
return chronology.days();
case HALFDAYS:
return chronology.halfdays();
case HOURS:
return chronology.hours();
case MINUTES:
return chronology.minutes();
case SECONDS:
return chronology.seconds();
case MILLIS:
return chronology.millis();
default:
// Shouldn't happen.
throw new InternalError();
}
}
/**
* Ensure a singleton is returned.
*
* @return the singleton type
*/
private Object readResolve() {
switch (iOrdinal) {
case ERAS:
return ERAS_TYPE;
case CENTURIES:
return CENTURIES_TYPE;
case WEEKYEARS:
return WEEKYEARS_TYPE;
case YEARS:
return YEARS_TYPE;
case MONTHS:
return MONTHS_TYPE;
case WEEKS:
return WEEKS_TYPE;
case DAYS:
return DAYS_TYPE;
case HALFDAYS:
return HALFDAYS_TYPE;
case HOURS:
return HOURS_TYPE;
case MINUTES:
return MINUTES_TYPE;
case SECONDS:
return SECONDS_TYPE;
case MILLIS:
return MILLIS_TYPE;
default:
// Shouldn't happen.
return this;
}
}
}
}