001/*
002 *  Copyright 2001-2009 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;
019
020import org.joda.time.DurationField;
021import org.joda.time.DurationFieldType;
022
023/**
024 * <code>DelegatedDurationField</code> delegates each method call to the
025 * duration field it wraps.
026 * <p>
027 * DelegatedDurationField is thread-safe and immutable, and its subclasses must
028 * be as well.
029 *
030 * @author Brian S O'Neill
031 * @see DecoratedDurationField
032 * @since 1.0
033 */
034public class DelegatedDurationField extends DurationField implements 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, DurationFieldType type) {
060        super();
061        if (field == null) {
062            throw new IllegalArgumentException("The field must not be null");
063        }
064        iField = field;
065        iType = (type == null ? field.getType() : type);
066    }
067
068    //-----------------------------------------------------------------------
069    /**
070     * Gets the wrapped duration field.
071     * 
072     * @return the wrapped DurationField
073     */
074    public final DurationField getWrappedField() {
075        return iField;
076    }
077
078    public DurationFieldType getType() {
079        return iType;
080    }
081
082    public String getName() {
083        return iType.getName();
084    }
085
086    /**
087     * Returns true if this field is supported.
088     */
089    public boolean isSupported() {
090        return iField.isSupported();
091    }
092
093    public boolean isPrecise() {
094        return iField.isPrecise();
095    }
096    
097    public int getValue(long duration) {
098        return iField.getValue(duration);
099    }
100
101    public long getValueAsLong(long duration) {
102        return iField.getValueAsLong(duration);
103    }
104
105    public int getValue(long duration, long instant) {
106        return iField.getValue(duration, instant);
107    }
108
109    public long getValueAsLong(long duration, long instant) {
110        return iField.getValueAsLong(duration, instant);
111    }
112
113    public long getMillis(int value) {
114        return iField.getMillis(value);
115    }
116
117    public long getMillis(long value) {
118        return iField.getMillis(value);
119    }
120
121    public long getMillis(int value, long instant) {
122        return iField.getMillis(value, instant);
123    }
124
125    public long getMillis(long value, long instant) {
126        return iField.getMillis(value, instant);
127    }
128
129    public long add(long instant, int value) {
130        return iField.add(instant, value);
131    }
132
133    public long add(long instant, long value) {
134        return iField.add(instant, value);
135    }
136
137    public int getDifference(long minuendInstant, long subtrahendInstant) {
138        return iField.getDifference(minuendInstant, subtrahendInstant);
139    }
140
141    public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
142        return iField.getDifferenceAsLong(minuendInstant, subtrahendInstant);
143    }
144
145    public long getUnitMillis() {
146        return iField.getUnitMillis();
147    }
148
149    public int compareTo(DurationField durationField) {
150        return iField.compareTo(durationField);
151    }
152
153    public boolean equals(Object obj) {
154        if (obj instanceof DelegatedDurationField) {
155            return iField.equals(((DelegatedDurationField) obj).iField);
156        }
157        return false;
158    }
159
160    public int hashCode() {
161        return iField.hashCode() ^ iType.hashCode();
162    }
163
164    public String toString() {
165        return (iType == null) ? iField.toString() :
166            ("DurationField[" + iType + ']');
167    }
168
169}