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 org.joda.time.DurationField;
021: import org.joda.time.DurationFieldType;
022:
023: /**
024: * A placeholder implementation to use when a duration field is not supported.
025: * <p>
026: * UnsupportedDurationField is thread-safe and immutable.
027: *
028: * @author Brian S O'Neill
029: * @since 1.0
030: */
031: public final class UnsupportedDurationField extends DurationField
032: implements Serializable {
033:
034: /** Serialization lock. */
035: private static final long serialVersionUID = -6390301302770925357L;
036:
037: /** The cache of unsupported duration field instances */
038: private static HashMap cCache;
039:
040: /**
041: * Gets an instance of UnsupportedDurationField for a specific named field.
042: * The returned instance is cached.
043: *
044: * @param type the type to obtain
045: * @return the instance
046: */
047: public static synchronized UnsupportedDurationField getInstance(
048: DurationFieldType type) {
049: UnsupportedDurationField field;
050: if (cCache == null) {
051: cCache = new HashMap(7);
052: field = null;
053: } else {
054: field = (UnsupportedDurationField) cCache.get(type);
055: }
056: if (field == null) {
057: field = new UnsupportedDurationField(type);
058: cCache.put(type, field);
059: }
060: return field;
061: }
062:
063: /** The name of the field */
064: private final DurationFieldType iType;
065:
066: /**
067: * Constructor.
068: *
069: * @param type the type to use
070: */
071: private UnsupportedDurationField(DurationFieldType type) {
072: iType = type;
073: }
074:
075: //-----------------------------------------------------------------------
076: // Design note: Simple Accessors return a suitable value, but methods
077: // intended to perform calculations throw an UnsupportedOperationException.
078:
079: public final DurationFieldType getType() {
080: return iType;
081: }
082:
083: public String getName() {
084: return iType.getName();
085: }
086:
087: /**
088: * This field is not supported.
089: *
090: * @return false always
091: */
092: public boolean isSupported() {
093: return false;
094: }
095:
096: /**
097: * This field is precise.
098: *
099: * @return true always
100: */
101: public boolean isPrecise() {
102: return true;
103: }
104:
105: /**
106: * Always throws UnsupportedOperationException
107: *
108: * @throws UnsupportedOperationException
109: */
110: public int getValue(long duration) {
111: throw unsupported();
112: }
113:
114: /**
115: * Always throws UnsupportedOperationException
116: *
117: * @throws UnsupportedOperationException
118: */
119: public long getValueAsLong(long duration) {
120: throw unsupported();
121: }
122:
123: /**
124: * Always throws UnsupportedOperationException
125: *
126: * @throws UnsupportedOperationException
127: */
128: public int getValue(long duration, long instant) {
129: throw unsupported();
130: }
131:
132: /**
133: * Always throws UnsupportedOperationException
134: *
135: * @throws UnsupportedOperationException
136: */
137: public long getValueAsLong(long duration, long instant) {
138: throw unsupported();
139: }
140:
141: /**
142: * Always throws UnsupportedOperationException
143: *
144: * @throws UnsupportedOperationException
145: */
146: public long getMillis(int value) {
147: throw unsupported();
148: }
149:
150: /**
151: * Always throws UnsupportedOperationException
152: *
153: * @throws UnsupportedOperationException
154: */
155: public long getMillis(long value) {
156: throw unsupported();
157: }
158:
159: /**
160: * Always throws UnsupportedOperationException
161: *
162: * @throws UnsupportedOperationException
163: */
164: public long getMillis(int value, long instant) {
165: throw unsupported();
166: }
167:
168: /**
169: * Always throws UnsupportedOperationException
170: *
171: * @throws UnsupportedOperationException
172: */
173: public long getMillis(long value, long instant) {
174: throw unsupported();
175: }
176:
177: /**
178: * Always throws UnsupportedOperationException
179: *
180: * @throws UnsupportedOperationException
181: */
182: public long add(long instant, int value) {
183: throw unsupported();
184: }
185:
186: /**
187: * Always throws UnsupportedOperationException
188: *
189: * @throws UnsupportedOperationException
190: */
191: public long add(long instant, long value) {
192: throw unsupported();
193: }
194:
195: /**
196: * Always throws UnsupportedOperationException
197: *
198: * @throws UnsupportedOperationException
199: */
200: public int getDifference(long minuendInstant, long subtrahendInstant) {
201: throw unsupported();
202: }
203:
204: /**
205: * Always throws UnsupportedOperationException
206: *
207: * @throws UnsupportedOperationException
208: */
209: public long getDifferenceAsLong(long minuendInstant,
210: long subtrahendInstant) {
211: throw unsupported();
212: }
213:
214: /**
215: * Always returns zero.
216: *
217: * @return zero always
218: */
219: public long getUnitMillis() {
220: return 0;
221: }
222:
223: /**
224: * Always returns zero, indicating that sort order is not relevent.
225: *
226: * @return zero always
227: */
228: public int compareTo(Object durationField) {
229: return 0;
230: }
231:
232: //------------------------------------------------------------------------
233: /**
234: * Compares this duration field to another.
235: *
236: * @param obj the object to compare to
237: * @return true if equal
238: */
239: public boolean equals(Object obj) {
240: if (this == obj) {
241: return true;
242: } else if (obj instanceof UnsupportedDurationField) {
243: UnsupportedDurationField other = (UnsupportedDurationField) obj;
244: if (other.getName() == null) {
245: return (getName() == null);
246: }
247: return (other.getName().equals(getName()));
248: }
249: return false;
250: }
251:
252: /**
253: * Gets a suitable hashcode.
254: *
255: * @return the hashcode
256: */
257: public int hashCode() {
258: return getName().hashCode();
259: }
260:
261: /**
262: * Get a suitable debug string.
263: *
264: * @return debug string
265: */
266: public String toString() {
267: return "UnsupportedDurationField[" + getName() + ']';
268: }
269:
270: /**
271: * Ensure proper singleton serialization
272: */
273: private Object readResolve() {
274: return getInstance(iType);
275: }
276:
277: private UnsupportedOperationException unsupported() {
278: return new UnsupportedOperationException(iType
279: + " field is unsupported");
280: }
281:
282: }
|