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, DurationFieldType type) { 052 super(type); 053 if (field == null) { 054 throw new IllegalArgumentException("The field must not be null"); 055 } 056 if (!field.isSupported()) { 057 throw new IllegalArgumentException("The field must be supported"); 058 } 059 iField = field; 060 } 061 062 //----------------------------------------------------------------------- 063 /** 064 * Gets the wrapped duration field. 065 * 066 * @return the wrapped DurationField 067 */ 068 public final DurationField getWrappedField() { 069 return iField; 070 } 071 072 public boolean isPrecise() { 073 return iField.isPrecise(); 074 } 075 076 public long getValueAsLong(long duration, long instant) { 077 return iField.getValueAsLong(duration, instant); 078 } 079 080 public long getMillis(int value, long instant) { 081 return iField.getMillis(value, instant); 082 } 083 084 public long getMillis(long value, long instant) { 085 return iField.getMillis(value, instant); 086 } 087 088 public long add(long instant, int value) { 089 return iField.add(instant, value); 090 } 091 092 public long add(long instant, long value) { 093 return iField.add(instant, value); 094 } 095 096 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 097 return iField.getDifferenceAsLong(minuendInstant, subtrahendInstant); 098 } 099 100 public long getUnitMillis() { 101 return iField.getUnitMillis(); 102 } 103 104 }