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