View Javadoc

1   /*
2    *  Copyright 2001-2009 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 java.io.Serializable;
19  
20  import org.joda.time.DurationField;
21  import org.joda.time.DurationFieldType;
22  
23  /**
24   * <code>DelegatedDurationField</code> delegates each method call to the
25   * duration field it wraps.
26   * <p>
27   * DelegatedDurationField is thread-safe and immutable, and its subclasses must
28   * be as well.
29   *
30   * @author Brian S O'Neill
31   * @see DecoratedDurationField
32   * @since 1.0
33   */
34  public class DelegatedDurationField extends DurationField implements Serializable {
35  
36      /** Serialization lock. */
37      private static final long serialVersionUID = -5576443481242007829L;
38  
39      /** The DurationField being wrapped */
40      private final DurationField iField;
41      /** The field type */
42      private final DurationFieldType iType;
43  
44      /**
45       * Constructor.
46       * 
47       * @param field  the base field
48       */
49      protected DelegatedDurationField(DurationField field) {
50          this(field, null);
51      }
52  
53      /**
54       * Constructor.
55       * 
56       * @param field  the base field
57       * @param type  the field type to use
58       */
59      protected DelegatedDurationField(DurationField field, DurationFieldType type) {
60          super();
61          if (field == null) {
62              throw new IllegalArgumentException("The field must not be null");
63          }
64          iField = field;
65          iType = (type == null ? field.getType() : type);
66      }
67  
68      //-----------------------------------------------------------------------
69      /**
70       * Gets the wrapped duration field.
71       * 
72       * @return the wrapped DurationField
73       */
74      public final DurationField getWrappedField() {
75          return iField;
76      }
77  
78      public DurationFieldType getType() {
79          return iType;
80      }
81  
82      public String getName() {
83          return iType.getName();
84      }
85  
86      /**
87       * Returns true if this field is supported.
88       */
89      public boolean isSupported() {
90          return iField.isSupported();
91      }
92  
93      public boolean isPrecise() {
94          return iField.isPrecise();
95      }
96      
97      public int getValue(long duration) {
98          return iField.getValue(duration);
99      }
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 }