2
* Copyright 2001-2005 Stephen Colebourne
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.joda.time.field;
18
import java.io.Serializable;
19
import java.util.HashMap;
20
import org.joda.time.DurationField;
21
import org.joda.time.DurationFieldType;
24
* A placeholder implementation to use when a duration field is not supported.
26
* UnsupportedDurationField is thread-safe and immutable.
28
* @author Brian S O'Neill
31
public final class UnsupportedDurationField extends DurationField implements Serializable {
33
/** Serialization lock. */
34
private static final long serialVersionUID = -6390301302770925357L;
36
/** The cache of unsupported duration field instances */
37
private static HashMap cCache;
40
* Gets an instance of UnsupportedDurationField for a specific named field.
41
* The returned instance is cached.
43
* @param type the type to obtain
44
* @return the instance
46
public static synchronized UnsupportedDurationField getInstance(DurationFieldType type) {
47
UnsupportedDurationField field;
49
cCache = new HashMap(7);
52
field = (UnsupportedDurationField) cCache.get(type);
55
field = new UnsupportedDurationField(type);
56
cCache.put(type, field);
61
/** The name of the field */
62
private final DurationFieldType iType;
67
* @param type the type to use
69
private UnsupportedDurationField(DurationFieldType type) {
73
//-----------------------------------------------------------------------
74
// Design note: Simple Accessors return a suitable value, but methods
75
// intended to perform calculations throw an UnsupportedOperationException.
77
public final DurationFieldType getType() {
81
public String getName() {
82
return iType.getName();
86
* This field is not supported.
88
* @return false always
90
public boolean isSupported() {
95
* This field is precise.
99
public boolean isPrecise() {
104
* Always throws UnsupportedOperationException
106
* @throws UnsupportedOperationException
108
public int getValue(long duration) {
113
* Always throws UnsupportedOperationException
115
* @throws UnsupportedOperationException
117
public long getValueAsLong(long duration) {
122
* Always throws UnsupportedOperationException
124
* @throws UnsupportedOperationException
126
public int getValue(long duration, long instant) {
131
* Always throws UnsupportedOperationException
133
* @throws UnsupportedOperationException
135
public long getValueAsLong(long duration, long instant) {
140
* Always throws UnsupportedOperationException
142
* @throws UnsupportedOperationException
144
public long getMillis(int value) {
149
* Always throws UnsupportedOperationException
151
* @throws UnsupportedOperationException
153
public long getMillis(long value) {
158
* Always throws UnsupportedOperationException
160
* @throws UnsupportedOperationException
162
public long getMillis(int value, long instant) {
167
* Always throws UnsupportedOperationException
169
* @throws UnsupportedOperationException
171
public long getMillis(long value, long instant) {
176
* Always throws UnsupportedOperationException
178
* @throws UnsupportedOperationException
180
public long add(long instant, int value) {
185
* Always throws UnsupportedOperationException
187
* @throws UnsupportedOperationException
189
public long add(long instant, long value) {
194
* Always throws UnsupportedOperationException
196
* @throws UnsupportedOperationException
198
public int getDifference(long minuendInstant, long subtrahendInstant) {
203
* Always throws UnsupportedOperationException
205
* @throws UnsupportedOperationException
207
public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
212
* Always returns zero.
214
* @return zero always
216
public long getUnitMillis() {
221
* Always returns zero, indicating that sort order is not relevent.
223
* @return zero always
225
public int compareTo(Object durationField) {
229
//------------------------------------------------------------------------
231
* Compares this duration field to another.
233
* @param obj the object to compare to
234
* @return true if equal
236
public boolean equals(Object obj) {
239
} else if (obj instanceof UnsupportedDurationField) {
240
UnsupportedDurationField other = (UnsupportedDurationField) obj;
241
if (other.getName() == null) {
242
return (getName() == null);
244
return (other.getName().equals(getName()));
250
* Gets a suitable hashcode.
252
* @return the hashcode
254
public int hashCode() {
255
return getName().hashCode();
259
* Get a suitable debug string.
261
* @return debug string
263
public String toString() {
264
return "UnsupportedDurationField[" + getName() + ']';
268
* Ensure proper singleton serialization
270
private Object readResolve() {
271
return getInstance(iType);
274
private UnsupportedOperationException unsupported() {
275
return new UnsupportedOperationException(iType + " field is unsupported");