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 org.joda.time.DurationField;
020: import org.joda.time.DurationFieldType;
021:
022: /**
023: * <code>DelegatedDurationField</code> delegates each method call to the
024: * duration field it wraps.
025: * <p>
026: * DelegatedDurationField is thread-safe and immutable, and its subclasses must
027: * be as well.
028: *
029: * @author Brian S O'Neill
030: * @see DecoratedDurationField
031: * @since 1.0
032: */
033: public class DelegatedDurationField extends DurationField implements
034: Serializable {
035:
036: /** Serialization lock. */
037: private static final long serialVersionUID = -5576443481242007829L;
038:
039: /** The DurationField being wrapped */
040: private final DurationField iField;
041: /** The field type */
042: private final DurationFieldType iType;
043:
044: /**
045: * Constructor.
046: *
047: * @param field the base field
048: */
049: protected DelegatedDurationField(DurationField field) {
050: this (field, null);
051: }
052:
053: /**
054: * Constructor.
055: *
056: * @param field the base field
057: * @param type the field type to use
058: */
059: protected DelegatedDurationField(DurationField field,
060: DurationFieldType type) {
061: super ();
062: if (field == null) {
063: throw new IllegalArgumentException(
064: "The field must not be null");
065: }
066: iField = field;
067: iType = (type == null ? field.getType() : type);
068: }
069:
070: //-----------------------------------------------------------------------
071: /**
072: * Gets the wrapped duration field.
073: *
074: * @return the wrapped DurationField
075: */
076: public final DurationField getWrappedField() {
077: return iField;
078: }
079:
080: public DurationFieldType getType() {
081: return iType;
082: }
083:
084: public String getName() {
085: return iType.getName();
086: }
087:
088: /**
089: * Returns true if this field is supported.
090: */
091: public boolean isSupported() {
092: return iField.isSupported();
093: }
094:
095: public boolean isPrecise() {
096: return iField.isPrecise();
097: }
098:
099: public int getValue(long duration) {
100: return iField.getValue(duration);
101: }
102:
103: public long getValueAsLong(long duration) {
104: return iField.getValueAsLong(duration);
105: }
106:
107: public int getValue(long duration, long instant) {
108: return iField.getValue(duration, instant);
109: }
110:
111: public long getValueAsLong(long duration, long instant) {
112: return iField.getValueAsLong(duration, instant);
113: }
114:
115: public long getMillis(int value) {
116: return iField.getMillis(value);
117: }
118:
119: public long getMillis(long value) {
120: return iField.getMillis(value);
121: }
122:
123: public long getMillis(int value, long instant) {
124: return iField.getMillis(value, instant);
125: }
126:
127: public long getMillis(long value, long instant) {
128: return iField.getMillis(value, instant);
129: }
130:
131: public long add(long instant, int value) {
132: return iField.add(instant, value);
133: }
134:
135: public long add(long instant, long value) {
136: return iField.add(instant, value);
137: }
138:
139: public int getDifference(long minuendInstant, long subtrahendInstant) {
140: return iField.getDifference(minuendInstant, subtrahendInstant);
141: }
142:
143: public long getDifferenceAsLong(long minuendInstant,
144: long subtrahendInstant) {
145: return iField.getDifferenceAsLong(minuendInstant,
146: subtrahendInstant);
147: }
148:
149: public long getUnitMillis() {
150: return iField.getUnitMillis();
151: }
152:
153: public int compareTo(Object durationField) {
154: return iField.compareTo(durationField);
155: }
156:
157: public String toString() {
158: return (iType == null) ? iField.toString() : ("DurationField["
159: + iType + ']');
160: }
161:
162: }
|