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 java.io.Serializable;
019: import java.util.HashMap;
020: import java.util.Locale;
021: import org.joda.time.DateTimeField;
022: import org.joda.time.DateTimeFieldType;
023: import org.joda.time.DurationField;
024: import org.joda.time.ReadablePartial;
025:
026: /**
027: * A placeholder implementation to use when a datetime field is not supported.
028: * <p>
029: * UnsupportedDateTimeField is thread-safe and immutable.
030: *
031: * @author Brian S O'Neill
032: * @since 1.0
033: */
034: public final class UnsupportedDateTimeField extends DateTimeField
035: implements Serializable {
036:
037: /** Serialilzation version */
038: private static final long serialVersionUID = -1934618396111902255L;
039:
040: /** The cache of unsupported datetime field instances */
041: private static HashMap cCache;
042:
043: /**
044: * Gets an instance of UnsupportedDateTimeField for a specific named field.
045: * Names should be of standard format, such as 'monthOfYear' or 'hourOfDay'.
046: * The returned instance is cached.
047: *
048: * @param type the type to obtain
049: * @return the instance
050: * @throws IllegalArgumentException if durationField is null
051: */
052: public static synchronized UnsupportedDateTimeField getInstance(
053: DateTimeFieldType type, DurationField durationField) {
054:
055: UnsupportedDateTimeField field;
056: if (cCache == null) {
057: cCache = new HashMap(7);
058: field = null;
059: } else {
060: field = (UnsupportedDateTimeField) cCache.get(type);
061: if (field != null
062: && field.getDurationField() != durationField) {
063: field = null;
064: }
065: }
066: if (field == null) {
067: field = new UnsupportedDateTimeField(type, durationField);
068: cCache.put(type, field);
069: }
070: return field;
071: }
072:
073: /** The field type */
074: private final DateTimeFieldType iType;
075: /** The duration of the datetime field */
076: private final DurationField iDurationField;
077:
078: /**
079: * Constructor.
080: *
081: * @param type the field type
082: * @param durationField the duration to use
083: */
084: private UnsupportedDateTimeField(DateTimeFieldType type,
085: DurationField durationField) {
086: if (type == null || durationField == null) {
087: throw new IllegalArgumentException();
088: }
089: iType = type;
090: iDurationField = durationField;
091: }
092:
093: //-----------------------------------------------------------------------
094: // Design note: Simple accessors return a suitable value, but methods
095: // intended to perform calculations throw an UnsupportedOperationException.
096:
097: public DateTimeFieldType getType() {
098: return iType;
099: }
100:
101: public String getName() {
102: return iType.getName();
103: }
104:
105: /**
106: * This field is not supported.
107: *
108: * @return false always
109: */
110: public boolean isSupported() {
111: return false;
112: }
113:
114: /**
115: * This field is not lenient.
116: *
117: * @return false always
118: */
119: public boolean isLenient() {
120: return false;
121: }
122:
123: /**
124: * Always throws UnsupportedOperationException
125: *
126: * @throws UnsupportedOperationException
127: */
128: public int get(long instant) {
129: throw unsupported();
130: }
131:
132: /**
133: * Always throws UnsupportedOperationException
134: *
135: * @throws UnsupportedOperationException
136: */
137: public String getAsText(long instant, Locale locale) {
138: throw unsupported();
139: }
140:
141: /**
142: * Always throws UnsupportedOperationException
143: *
144: * @throws UnsupportedOperationException
145: */
146: public String getAsText(long instant) {
147: throw unsupported();
148: }
149:
150: /**
151: * Always throws UnsupportedOperationException
152: *
153: * @throws UnsupportedOperationException
154: */
155: public String getAsText(ReadablePartial partial, int fieldValue,
156: Locale locale) {
157: throw unsupported();
158: }
159:
160: /**
161: * Always throws UnsupportedOperationException
162: *
163: * @throws UnsupportedOperationException
164: */
165: public String getAsText(ReadablePartial partial, Locale locale) {
166: throw unsupported();
167: }
168:
169: /**
170: * Always throws UnsupportedOperationException
171: *
172: * @throws UnsupportedOperationException
173: */
174: public String getAsText(int fieldValue, Locale locale) {
175: throw unsupported();
176: }
177:
178: /**
179: * Always throws UnsupportedOperationException
180: *
181: * @throws UnsupportedOperationException
182: */
183: public String getAsShortText(long instant, Locale locale) {
184: throw unsupported();
185: }
186:
187: /**
188: * Always throws UnsupportedOperationException
189: *
190: * @throws UnsupportedOperationException
191: */
192: public String getAsShortText(long instant) {
193: throw unsupported();
194: }
195:
196: /**
197: * Always throws UnsupportedOperationException
198: *
199: * @throws UnsupportedOperationException
200: */
201: public String getAsShortText(ReadablePartial partial,
202: int fieldValue, Locale locale) {
203: throw unsupported();
204: }
205:
206: /**
207: * Always throws UnsupportedOperationException
208: *
209: * @throws UnsupportedOperationException
210: */
211: public String getAsShortText(ReadablePartial partial, Locale locale) {
212: throw unsupported();
213: }
214:
215: /**
216: * Always throws UnsupportedOperationException
217: *
218: * @throws UnsupportedOperationException
219: */
220: public String getAsShortText(int fieldValue, Locale locale) {
221: throw unsupported();
222: }
223:
224: /**
225: * Delegates to the duration field.
226: *
227: * @throws UnsupportedOperationException if the duration is unsupported
228: */
229: public long add(long instant, int value) {
230: return getDurationField().add(instant, value);
231: }
232:
233: /**
234: * Delegates to the duration field.
235: *
236: * @throws UnsupportedOperationException if the duration is unsupported
237: */
238: public long add(long instant, long value) {
239: return getDurationField().add(instant, value);
240: }
241:
242: /**
243: * Always throws UnsupportedOperationException
244: *
245: * @throws UnsupportedOperationException
246: */
247: public int[] add(ReadablePartial instant, int fieldIndex,
248: int[] values, int valueToAdd) {
249: throw unsupported();
250: }
251:
252: /**
253: * Always throws UnsupportedOperationException
254: *
255: * @throws UnsupportedOperationException
256: */
257: public int[] addWrapPartial(ReadablePartial instant,
258: int fieldIndex, int[] values, int valueToAdd) {
259: throw unsupported();
260: }
261:
262: /**
263: * Always throws UnsupportedOperationException
264: *
265: * @throws UnsupportedOperationException
266: */
267: public long addWrapField(long instant, int value) {
268: throw unsupported();
269: }
270:
271: /**
272: * Always throws UnsupportedOperationException
273: *
274: * @throws UnsupportedOperationException
275: */
276: public int[] addWrapField(ReadablePartial instant, int fieldIndex,
277: int[] values, int valueToAdd) {
278: throw unsupported();
279: }
280:
281: /**
282: * Delegates to the duration field.
283: *
284: * @throws UnsupportedOperationException if the duration is unsupported
285: */
286: public int getDifference(long minuendInstant, long subtrahendInstant) {
287: return getDurationField().getDifference(minuendInstant,
288: subtrahendInstant);
289: }
290:
291: /**
292: * Delegates to the duration field.
293: *
294: * @throws UnsupportedOperationException if the duration is unsupported
295: */
296: public long getDifferenceAsLong(long minuendInstant,
297: long subtrahendInstant) {
298: return getDurationField().getDifferenceAsLong(minuendInstant,
299: subtrahendInstant);
300: }
301:
302: /**
303: * Always throws UnsupportedOperationException
304: *
305: * @throws UnsupportedOperationException
306: */
307: public long set(long instant, int value) {
308: throw unsupported();
309: }
310:
311: /**
312: * Always throws UnsupportedOperationException
313: *
314: * @throws UnsupportedOperationException
315: */
316: public int[] set(ReadablePartial instant, int fieldIndex,
317: int[] values, int newValue) {
318: throw unsupported();
319: }
320:
321: /**
322: * Always throws UnsupportedOperationException
323: *
324: * @throws UnsupportedOperationException
325: */
326: public long set(long instant, String text, Locale locale) {
327: throw unsupported();
328: }
329:
330: /**
331: * Always throws UnsupportedOperationException
332: *
333: * @throws UnsupportedOperationException
334: */
335: public long set(long instant, String text) {
336: throw unsupported();
337: }
338:
339: /**
340: * Always throws UnsupportedOperationException
341: *
342: * @throws UnsupportedOperationException
343: */
344: public int[] set(ReadablePartial instant, int fieldIndex,
345: int[] values, String text, Locale locale) {
346: throw unsupported();
347: }
348:
349: /**
350: * Even though this DateTimeField is unsupported, the duration field might
351: * be supported.
352: *
353: * @return a possibly supported DurationField
354: */
355: public DurationField getDurationField() {
356: return iDurationField;
357: }
358:
359: /**
360: * Always returns null.
361: *
362: * @return null always
363: */
364: public DurationField getRangeDurationField() {
365: return null;
366: }
367:
368: /**
369: * Always throws UnsupportedOperationException
370: *
371: * @throws UnsupportedOperationException
372: */
373: public boolean isLeap(long instant) {
374: throw unsupported();
375: }
376:
377: /**
378: * Always throws UnsupportedOperationException
379: *
380: * @throws UnsupportedOperationException
381: */
382: public int getLeapAmount(long instant) {
383: throw unsupported();
384: }
385:
386: /**
387: * Always returns null.
388: *
389: * @return null always
390: */
391: public DurationField getLeapDurationField() {
392: return null;
393: }
394:
395: /**
396: * Always throws UnsupportedOperationException
397: *
398: * @throws UnsupportedOperationException
399: */
400: public int getMinimumValue() {
401: throw unsupported();
402: }
403:
404: /**
405: * Always throws UnsupportedOperationException
406: *
407: * @throws UnsupportedOperationException
408: */
409: public int getMinimumValue(long instant) {
410: throw unsupported();
411: }
412:
413: /**
414: * Always throws UnsupportedOperationException
415: *
416: * @throws UnsupportedOperationException
417: */
418: public int getMinimumValue(ReadablePartial instant) {
419: throw unsupported();
420: }
421:
422: /**
423: * Always throws UnsupportedOperationException
424: *
425: * @throws UnsupportedOperationException
426: */
427: public int getMinimumValue(ReadablePartial instant, int[] values) {
428: throw unsupported();
429: }
430:
431: /**
432: * Always throws UnsupportedOperationException
433: *
434: * @throws UnsupportedOperationException
435: */
436: public int getMaximumValue() {
437: throw unsupported();
438: }
439:
440: /**
441: * Always throws UnsupportedOperationException
442: *
443: * @throws UnsupportedOperationException
444: */
445: public int getMaximumValue(long instant) {
446: throw unsupported();
447: }
448:
449: /**
450: * Always throws UnsupportedOperationException
451: *
452: * @throws UnsupportedOperationException
453: */
454: public int getMaximumValue(ReadablePartial instant) {
455: throw unsupported();
456: }
457:
458: /**
459: * Always throws UnsupportedOperationException
460: *
461: * @throws UnsupportedOperationException
462: */
463: public int getMaximumValue(ReadablePartial instant, int[] values) {
464: throw unsupported();
465: }
466:
467: /**
468: * Always throws UnsupportedOperationException
469: *
470: * @throws UnsupportedOperationException
471: */
472: public int getMaximumTextLength(Locale locale) {
473: throw unsupported();
474: }
475:
476: /**
477: * Always throws UnsupportedOperationException
478: *
479: * @throws UnsupportedOperationException
480: */
481: public int getMaximumShortTextLength(Locale locale) {
482: throw unsupported();
483: }
484:
485: /**
486: * Always throws UnsupportedOperationException
487: *
488: * @throws UnsupportedOperationException
489: */
490: public long roundFloor(long instant) {
491: throw unsupported();
492: }
493:
494: /**
495: * Always throws UnsupportedOperationException
496: *
497: * @throws UnsupportedOperationException
498: */
499: public long roundCeiling(long instant) {
500: throw unsupported();
501: }
502:
503: /**
504: * Always throws UnsupportedOperationException
505: *
506: * @throws UnsupportedOperationException
507: */
508: public long roundHalfFloor(long instant) {
509: throw unsupported();
510: }
511:
512: /**
513: * Always throws UnsupportedOperationException
514: *
515: * @throws UnsupportedOperationException
516: */
517: public long roundHalfCeiling(long instant) {
518: throw unsupported();
519: }
520:
521: /**
522: * Always throws UnsupportedOperationException
523: *
524: * @throws UnsupportedOperationException
525: */
526: public long roundHalfEven(long instant) {
527: throw unsupported();
528: }
529:
530: /**
531: * Always throws UnsupportedOperationException
532: *
533: * @throws UnsupportedOperationException
534: */
535: public long remainder(long instant) {
536: throw unsupported();
537: }
538:
539: //------------------------------------------------------------------------
540: /**
541: * Get a suitable debug string.
542: *
543: * @return debug string
544: */
545: public String toString() {
546: return "UnsupportedDateTimeField";
547: }
548:
549: /**
550: * Ensure proper singleton serialization
551: */
552: private Object readResolve() {
553: return getInstance(iType, iDurationField);
554: }
555:
556: private UnsupportedOperationException unsupported() {
557: return new UnsupportedOperationException(iType
558: + " field is unsupported");
559: }
560:
561: }
|