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 org.joda.time.DurationField;
019: import org.joda.time.DurationFieldType;
020:
021: /**
022: * <code>DecoratedDurationField</code> extends {@link BaseDurationField},
023: * implementing only the minimum required set of methods. These implemented
024: * methods delegate to a wrapped field.
025: * <p>
026: * This design allows new DurationField types to be defined that piggyback on
027: * top of another, inheriting all the safe method implementations from
028: * BaseDurationField. Should any method require pure delegation to the
029: * wrapped field, simply override and use the provided getWrappedField method.
030: * <p>
031: * DecoratedDurationField is thread-safe and immutable, and its subclasses must
032: * be as well.
033: *
034: * @author Brian S O'Neill
035: * @see DelegatedDurationField
036: * @since 1.0
037: */
038: public class DecoratedDurationField extends BaseDurationField {
039:
040: private static final long serialVersionUID = 8019982251647420015L;
041:
042: /** The DurationField being wrapped */
043: private final DurationField iField;
044:
045: /**
046: * Constructor.
047: *
048: * @param field the base field
049: * @param type the type to actually use
050: */
051: public DecoratedDurationField(DurationField field,
052: DurationFieldType type) {
053: super (type);
054: if (field == null) {
055: throw new IllegalArgumentException(
056: "The field must not be null");
057: }
058: if (!field.isSupported()) {
059: throw new IllegalArgumentException(
060: "The field must be supported");
061: }
062: iField = field;
063: }
064:
065: //-----------------------------------------------------------------------
066: /**
067: * Gets the wrapped duration field.
068: *
069: * @return the wrapped DurationField
070: */
071: public final DurationField getWrappedField() {
072: return iField;
073: }
074:
075: public boolean isPrecise() {
076: return iField.isPrecise();
077: }
078:
079: public long getValueAsLong(long duration, long instant) {
080: return iField.getValueAsLong(duration, instant);
081: }
082:
083: public long getMillis(int value, long instant) {
084: return iField.getMillis(value, instant);
085: }
086:
087: public long getMillis(long value, long instant) {
088: return iField.getMillis(value, instant);
089: }
090:
091: public long add(long instant, int value) {
092: return iField.add(instant, value);
093: }
094:
095: public long add(long instant, long value) {
096: return iField.add(instant, value);
097: }
098:
099: public long getDifferenceAsLong(long minuendInstant,
100: long subtrahendInstant) {
101: return iField.getDifferenceAsLong(minuendInstant,
102: subtrahendInstant);
103: }
104:
105: public long getUnitMillis() {
106: return iField.getUnitMillis();
107: }
108:
109: }
|