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 | * BaseDurationField provides the common behaviour for DurationField |
24 | * implementations. |
25 | * <p> |
26 | * This class should generally not be used directly by API users. The |
27 | * DurationField class should be used when different kinds of DurationField |
28 | * objects are to be referenced. |
29 | * <p> |
30 | * BaseDurationField is thread-safe and immutable, and its subclasses must |
31 | * be as well. |
32 | * |
33 | * @author Brian S O'Neill |
34 | * @see DecoratedDurationField |
35 | * @since 1.0 |
36 | */ |
37 | public abstract class BaseDurationField extends DurationField implements Serializable { |
38 | |
39 | /** Serialization lock. */ |
40 | private static final long serialVersionUID = -2554245107589433218L; |
41 | |
42 | /** A desriptive name for the field. */ |
43 | private final DurationFieldType iType; |
44 | |
45 | protected BaseDurationField(DurationFieldType type) { |
46 | super(); |
47 | if (type == null) { |
48 | throw new IllegalArgumentException("The type must not be null"); |
49 | } |
50 | iType = type; |
51 | } |
52 | |
53 | public final DurationFieldType getType() { |
54 | return iType; |
55 | } |
56 | |
57 | public final String getName() { |
58 | return iType.getName(); |
59 | } |
60 | |
61 | /** |
62 | * @return true always |
63 | */ |
64 | public final boolean isSupported() { |
65 | return true; |
66 | } |
67 | |
68 | //------------------------------------------------------------------------ |
69 | /** |
70 | * Get the value of this field from the milliseconds, which is approximate |
71 | * if this field is imprecise. |
72 | * |
73 | * @param duration the milliseconds to query, which may be negative |
74 | * @return the value of the field, in the units of the field, which may be |
75 | * negative |
76 | */ |
77 | public int getValue(long duration) { |
78 | return FieldUtils.safeToInt(getValueAsLong(duration)); |
79 | } |
80 | |
81 | /** |
82 | * Get the value of this field from the milliseconds, which is approximate |
83 | * if this field is imprecise. |
84 | * |
85 | * @param duration the milliseconds to query, which may be negative |
86 | * @return the value of the field, in the units of the field, which may be |
87 | * negative |
88 | */ |
89 | public long getValueAsLong(long duration) { |
90 | return duration / getUnitMillis(); |
91 | } |
92 | |
93 | /** |
94 | * Get the value of this field from the milliseconds relative to an |
95 | * instant. |
96 | * |
97 | * <p>If the milliseconds is positive, then the instant is treated as a |
98 | * "start instant". If negative, the instant is treated as an "end |
99 | * instant". |
100 | * |
101 | * <p>The default implementation returns |
102 | * <code>Utils.safeToInt(getAsLong(millisDuration, instant))</code>. |
103 | * |
104 | * @param duration the milliseconds to query, which may be negative |
105 | * @param instant the start instant to calculate relative to |
106 | * @return the value of the field, in the units of the field, which may be |
107 | * negative |
108 | */ |
109 | public int getValue(long duration, long instant) { |
110 | return FieldUtils.safeToInt(getValueAsLong(duration, instant)); |
111 | } |
112 | |
113 | /** |
114 | * Get the millisecond duration of this field from its value, which is |
115 | * approximate if this field is imprecise. |
116 | * |
117 | * @param value the value of the field, which may be negative |
118 | * @return the milliseconds that the field represents, which may be |
119 | * negative |
120 | */ |
121 | public long getMillis(int value) { |
122 | return value * getUnitMillis(); // safe |
123 | } |
124 | |
125 | /** |
126 | * Get the millisecond duration of this field from its value, which is |
127 | * approximate if this field is imprecise. |
128 | * |
129 | * @param value the value of the field, which may be negative |
130 | * @return the milliseconds that the field represents, which may be |
131 | * negative |
132 | */ |
133 | public long getMillis(long value) { |
134 | return FieldUtils.safeMultiply(value, getUnitMillis()); |
135 | } |
136 | |
137 | // Calculation API |
138 | //------------------------------------------------------------------------ |
139 | public int getDifference(long minuendInstant, long subtrahendInstant) { |
140 | return FieldUtils.safeToInt(getDifferenceAsLong(minuendInstant, subtrahendInstant)); |
141 | } |
142 | |
143 | //------------------------------------------------------------------------ |
144 | public int compareTo(Object durationField) { |
145 | DurationField otherField = (DurationField) durationField; |
146 | long otherMillis = otherField.getUnitMillis(); |
147 | long thisMillis = getUnitMillis(); |
148 | // cannot do (thisMillis - otherMillis) as can overflow |
149 | if (thisMillis == otherMillis) { |
150 | return 0; |
151 | } |
152 | if (thisMillis < otherMillis) { |
153 | return -1; |
154 | } else { |
155 | return 1; |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * Get a suitable debug string. |
161 | * |
162 | * @return debug string |
163 | */ |
164 | public String toString() { |
165 | return "DurationField[" + getName() + ']'; |
166 | } |
167 | |
168 | } |