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 java.io.Serializable; |
19 | import org.joda.time.DurationField; |
20 | import org.joda.time.DurationFieldType; |
21 | |
22 | /** |
23 | * <code>DelegatedDurationField</code> delegates each method call to the |
24 | * duration field it wraps. |
25 | * <p> |
26 | * DelegatedDurationField is thread-safe and immutable, and its subclasses must |
27 | * be as well. |
28 | * |
29 | * @author Brian S O'Neill |
30 | * @see DecoratedDurationField |
31 | * @since 1.0 |
32 | */ |
33 | public class DelegatedDurationField extends DurationField implements Serializable { |
34 | |
35 | /** Serialization lock. */ |
36 | private static final long serialVersionUID = -5576443481242007829L; |
37 | |
38 | /** The DurationField being wrapped */ |
39 | private final DurationField iField; |
40 | /** The field type */ |
41 | private final DurationFieldType iType; |
42 | |
43 | /** |
44 | * Constructor. |
45 | * |
46 | * @param field the base field |
47 | */ |
48 | protected DelegatedDurationField(DurationField field) { |
49 | this(field, null); |
50 | } |
51 | |
52 | /** |
53 | * Constructor. |
54 | * |
55 | * @param field the base field |
56 | * @param type the field type to use |
57 | */ |
58 | protected DelegatedDurationField(DurationField field, DurationFieldType type) { |
59 | super(); |
60 | if (field == null) { |
61 | throw new IllegalArgumentException("The field must not be null"); |
62 | } |
63 | iField = field; |
64 | iType = (type == null ? field.getType() : type); |
65 | } |
66 | |
67 | //----------------------------------------------------------------------- |
68 | /** |
69 | * Gets the wrapped duration field. |
70 | * |
71 | * @return the wrapped DurationField |
72 | */ |
73 | public final DurationField getWrappedField() { |
74 | return iField; |
75 | } |
76 | |
77 | public DurationFieldType getType() { |
78 | return iType; |
79 | } |
80 | |
81 | public String getName() { |
82 | return iType.getName(); |
83 | } |
84 | |
85 | /** |
86 | * Returns true if this field is supported. |
87 | */ |
88 | public boolean isSupported() { |
89 | return iField.isSupported(); |
90 | } |
91 | |
92 | public boolean isPrecise() { |
93 | return iField.isPrecise(); |
94 | } |
95 | |
96 | public int getValue(long duration) { |
97 | return iField.getValue(duration); |
98 | } |
99 | |
100 | public long getValueAsLong(long duration) { |
101 | return iField.getValueAsLong(duration); |
102 | } |
103 | |
104 | public int getValue(long duration, long instant) { |
105 | return iField.getValue(duration, instant); |
106 | } |
107 | |
108 | public long getValueAsLong(long duration, long instant) { |
109 | return iField.getValueAsLong(duration, instant); |
110 | } |
111 | |
112 | public long getMillis(int value) { |
113 | return iField.getMillis(value); |
114 | } |
115 | |
116 | public long getMillis(long value) { |
117 | return iField.getMillis(value); |
118 | } |
119 | |
120 | public long getMillis(int value, long instant) { |
121 | return iField.getMillis(value, instant); |
122 | } |
123 | |
124 | public long getMillis(long value, long instant) { |
125 | return iField.getMillis(value, instant); |
126 | } |
127 | |
128 | public long add(long instant, int value) { |
129 | return iField.add(instant, value); |
130 | } |
131 | |
132 | public long add(long instant, long value) { |
133 | return iField.add(instant, value); |
134 | } |
135 | |
136 | public int getDifference(long minuendInstant, long subtrahendInstant) { |
137 | return iField.getDifference(minuendInstant, subtrahendInstant); |
138 | } |
139 | |
140 | public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { |
141 | return iField.getDifferenceAsLong(minuendInstant, subtrahendInstant); |
142 | } |
143 | |
144 | public long getUnitMillis() { |
145 | return iField.getUnitMillis(); |
146 | } |
147 | |
148 | public int compareTo(Object durationField) { |
149 | return iField.compareTo(durationField); |
150 | } |
151 | |
152 | public String toString() { |
153 | return (iType == null) ? iField.toString() : |
154 | ("DurationField[" + iType + ']'); |
155 | } |
156 | |
157 | } |