| 1 | /* |
| 2 | * Copyright 2001-2005 Stephen Colebourne |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | package org.joda.time.field; |
| 17 | |
| 18 | import org.joda.time.DurationField; |
| 19 | import org.joda.time.DurationFieldType; |
| 20 | |
| 21 | /** |
| 22 | * <code>DecoratedDurationField</code> extends {@link BaseDurationField}, |
| 23 | * implementing only the minimum required set of methods. These implemented |
| 24 | * methods delegate to a wrapped field. |
| 25 | * <p> |
| 26 | * This design allows new DurationField types to be defined that piggyback on |
| 27 | * top of another, inheriting all the safe method implementations from |
| 28 | * BaseDurationField. Should any method require pure delegation to the |
| 29 | * wrapped field, simply override and use the provided getWrappedField method. |
| 30 | * <p> |
| 31 | * DecoratedDurationField is thread-safe and immutable, and its subclasses must |
| 32 | * be as well. |
| 33 | * |
| 34 | * @author Brian S O'Neill |
| 35 | * @see DelegatedDurationField |
| 36 | * @since 1.0 |
| 37 | */ |
| 38 | public class DecoratedDurationField extends BaseDurationField { |
| 39 | |
| 40 | private static final long serialVersionUID = 8019982251647420015L; |
| 41 | |
| 42 | /** The DurationField being wrapped */ |
| 43 | private final DurationField iField; |
| 44 | |
| 45 | /** |
| 46 | * Constructor. |
| 47 | * |
| 48 | * @param field the base field |
| 49 | * @param type the type to actually use |
| 50 | */ |
| 51 | public DecoratedDurationField(DurationField field, DurationFieldType type) { |
| 52 | super(type); |
| 53 | if (field == null) { |
| 54 | throw new IllegalArgumentException("The field must not be null"); |
| 55 | } |
| 56 | if (!field.isSupported()) { |
| 57 | throw new IllegalArgumentException("The field must be supported"); |
| 58 | } |
| 59 | iField = field; |
| 60 | } |
| 61 | |
| 62 | //----------------------------------------------------------------------- |
| 63 | /** |
| 64 | * Gets the wrapped duration field. |
| 65 | * |
| 66 | * @return the wrapped DurationField |
| 67 | */ |
| 68 | public final DurationField getWrappedField() { |
| 69 | return iField; |
| 70 | } |
| 71 | |
| 72 | public boolean isPrecise() { |
| 73 | return iField.isPrecise(); |
| 74 | } |
| 75 | |
| 76 | public long getValueAsLong(long duration, long instant) { |
| 77 | return iField.getValueAsLong(duration, instant); |
| 78 | } |
| 79 | |
| 80 | public long getMillis(int value, long instant) { |
| 81 | return iField.getMillis(value, instant); |
| 82 | } |
| 83 | |
| 84 | public long getMillis(long value, long instant) { |
| 85 | return iField.getMillis(value, instant); |
| 86 | } |
| 87 | |
| 88 | public long add(long instant, int value) { |
| 89 | return iField.add(instant, value); |
| 90 | } |
| 91 | |
| 92 | public long add(long instant, long value) { |
| 93 | return iField.add(instant, value); |
| 94 | } |
| 95 | |
| 96 | public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { |
| 97 | return iField.getDifferenceAsLong(minuendInstant, subtrahendInstant); |
| 98 | } |
| 99 | |
| 100 | public long getUnitMillis() { |
| 101 | return iField.getUnitMillis(); |
| 102 | } |
| 103 | |
| 104 | } |