12005
|
1 |
/*
|
|
2 |
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.xml.datatype;
|
|
27 |
|
|
28 |
import java.math.BigDecimal;
|
|
29 |
import java.math.BigInteger;
|
|
30 |
import java.util.Calendar;
|
|
31 |
import java.util.Date;
|
|
32 |
import java.util.GregorianCalendar;
|
|
33 |
|
|
34 |
import javax.xml.namespace.QName;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* <p>Immutable representation of a time span as defined in
|
29999
|
38 |
* the W3C XML Schema 1.0 specification.
|
12005
|
39 |
*
|
|
40 |
* <p>A Duration object represents a period of Gregorian time,
|
|
41 |
* which consists of six fields (years, months, days, hours,
|
29999
|
42 |
* minutes, and seconds) plus a sign (+/-) field.
|
12005
|
43 |
*
|
29999
|
44 |
* <p>The first five fields have non-negative ({@literal >=}0) integers or null
|
12005
|
45 |
* (which represents that the field is not set),
|
|
46 |
* and the seconds field has a non-negative decimal or null.
|
29999
|
47 |
* A negative sign indicates a negative duration.
|
12005
|
48 |
*
|
|
49 |
* <p>This class provides a number of methods that make it easy
|
|
50 |
* to use for the duration datatype of XML Schema 1.0 with
|
29999
|
51 |
* the errata.
|
12005
|
52 |
*
|
|
53 |
* <h2>Order relationship</h2>
|
|
54 |
* <p>Duration objects only have partial order, where two values A and B
|
29999
|
55 |
* maybe either:
|
12005
|
56 |
* <ol>
|
29999
|
57 |
* <li>A{@literal <}B (A is shorter than B)
|
|
58 |
* <li>A{@literal >}B (A is longer than B)
|
12005
|
59 |
* <li>A==B (A and B are of the same duration)
|
29999
|
60 |
* <li>A{@literal <>}B (Comparison between A and B is indeterminate)
|
12005
|
61 |
* </ol>
|
|
62 |
*
|
|
63 |
* <p>For example, 30 days cannot be meaningfully compared to one month.
|
|
64 |
* The {@link #compare(Duration duration)} method implements this
|
29999
|
65 |
* relationship.
|
12005
|
66 |
*
|
|
67 |
* <p>See the {@link #isLongerThan(Duration)} method for details about
|
29999
|
68 |
* the order relationship among {@code Duration} objects.
|
12005
|
69 |
*
|
|
70 |
* <h2>Operations over Duration</h2>
|
|
71 |
* <p>This class provides a set of basic arithmetic operations, such
|
|
72 |
* as addition, subtraction and multiplication.
|
|
73 |
* Because durations don't have total order, an operation could
|
|
74 |
* fail for some combinations of operations. For example, you cannot
|
|
75 |
* subtract 15 days from 1 month. See the javadoc of those methods
|
29999
|
76 |
* for detailed conditions where this could happen.
|
12005
|
77 |
*
|
|
78 |
* <p>Also, division of a duration by a number is not provided because
|
29999
|
79 |
* the {@code Duration} class can only deal with finite precision
|
|
80 |
* decimal numbers. For example, one cannot represent 1 sec divided by 3.
|
12005
|
81 |
*
|
|
82 |
* <p>However, you could substitute a division by 3 with multiplying
|
29999
|
83 |
* by numbers such as 0.3 or 0.333.
|
12005
|
84 |
*
|
|
85 |
* <h2>Range of allowed values</h2>
|
|
86 |
* <p>
|
29999
|
87 |
* Because some operations of {@code Duration} rely on {@link Calendar}
|
12005
|
88 |
* even though {@link Duration} can hold very large or very small values,
|
29999
|
89 |
* some of the methods may not work correctly on such {@code Duration}s.
|
12005
|
90 |
* The impacted methods document their dependency on {@link Calendar}.
|
|
91 |
*
|
|
92 |
* @author <a href="mailto:Joseph.Fialli@Sun.COM">Joseph Fialli</a>
|
|
93 |
* @author <a href="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
|
|
94 |
* @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
|
|
95 |
* @author <a href="mailto:Sunitha.Reddy@Sun.com">Sunitha Reddy</a>
|
|
96 |
* @see XMLGregorianCalendar#add(Duration)
|
|
97 |
* @since 1.5
|
|
98 |
*/
|
|
99 |
public abstract class Duration {
|
|
100 |
|
|
101 |
/**
|
29999
|
102 |
* Debugging {@code true} or {@code false}.
|
12005
|
103 |
*/
|
|
104 |
private static final boolean DEBUG = true;
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Default no-arg constructor.
|
|
108 |
*
|
|
109 |
* <p>Note: Always use the {@link DatatypeFactory} to
|
29999
|
110 |
* construct an instance of {@code Duration}.
|
12005
|
111 |
* The constructor on this class cannot be guaranteed to
|
|
112 |
* produce an object with a consistent state and may be
|
29999
|
113 |
* removed in the future.
|
12005
|
114 |
*/
|
|
115 |
public Duration() {
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
29999
|
119 |
* Return the name of the XML Schema date/time type that this instance
|
12005
|
120 |
* maps to. Type is computed based on fields that are set,
|
29999
|
121 |
* i.e. {@link #isSet(DatatypeConstants.Field field)} == {@code true}.
|
12005
|
122 |
*
|
|
123 |
* <table border="2" rules="all" cellpadding="2">
|
|
124 |
* <thead>
|
|
125 |
* <tr>
|
|
126 |
* <th align="center" colspan="7">
|
29999
|
127 |
* Required fields for XML Schema 1.0 Date/Time Datatypes.<br>
|
12005
|
128 |
* <i>(timezone is optional for all date/time datatypes)</i>
|
|
129 |
* </th>
|
|
130 |
* </tr>
|
|
131 |
* </thead>
|
|
132 |
* <tbody>
|
|
133 |
* <tr>
|
|
134 |
* <td>Datatype</td>
|
|
135 |
* <td>year</td>
|
|
136 |
* <td>month</td>
|
|
137 |
* <td>day</td>
|
|
138 |
* <td>hour</td>
|
|
139 |
* <td>minute</td>
|
|
140 |
* <td>second</td>
|
|
141 |
* </tr>
|
|
142 |
* <tr>
|
|
143 |
* <td>{@link DatatypeConstants#DURATION}</td>
|
|
144 |
* <td>X</td>
|
|
145 |
* <td>X</td>
|
|
146 |
* <td>X</td>
|
|
147 |
* <td>X</td>
|
|
148 |
* <td>X</td>
|
|
149 |
* <td>X</td>
|
|
150 |
* </tr>
|
|
151 |
* <tr>
|
|
152 |
* <td>{@link DatatypeConstants#DURATION_DAYTIME}</td>
|
|
153 |
* <td></td>
|
|
154 |
* <td></td>
|
|
155 |
* <td>X</td>
|
|
156 |
* <td>X</td>
|
|
157 |
* <td>X</td>
|
|
158 |
* <td>X</td>
|
|
159 |
* </tr>
|
|
160 |
* <tr>
|
|
161 |
* <td>{@link DatatypeConstants#DURATION_YEARMONTH}</td>
|
|
162 |
* <td>X</td>
|
|
163 |
* <td>X</td>
|
|
164 |
* <td></td>
|
|
165 |
* <td></td>
|
|
166 |
* <td></td>
|
|
167 |
* <td></td>
|
|
168 |
* </tr>
|
|
169 |
* </tbody>
|
|
170 |
* </table>
|
|
171 |
*
|
|
172 |
* @return one of the following constants:
|
|
173 |
* {@link DatatypeConstants#DURATION},
|
|
174 |
* {@link DatatypeConstants#DURATION_DAYTIME} or
|
|
175 |
* {@link DatatypeConstants#DURATION_YEARMONTH}.
|
|
176 |
*
|
|
177 |
* @throws IllegalStateException If the combination of set fields does not match one of the XML Schema date/time datatypes.
|
|
178 |
*/
|
|
179 |
public QName getXMLSchemaType() {
|
|
180 |
|
|
181 |
boolean yearSet = isSet(DatatypeConstants.YEARS);
|
|
182 |
boolean monthSet = isSet(DatatypeConstants.MONTHS);
|
|
183 |
boolean daySet = isSet(DatatypeConstants.DAYS);
|
|
184 |
boolean hourSet = isSet(DatatypeConstants.HOURS);
|
|
185 |
boolean minuteSet = isSet(DatatypeConstants.MINUTES);
|
|
186 |
boolean secondSet = isSet(DatatypeConstants.SECONDS);
|
|
187 |
|
|
188 |
// DURATION
|
|
189 |
if (yearSet
|
|
190 |
&& monthSet
|
|
191 |
&& daySet
|
|
192 |
&& hourSet
|
|
193 |
&& minuteSet
|
|
194 |
&& secondSet) {
|
|
195 |
return DatatypeConstants.DURATION;
|
|
196 |
}
|
|
197 |
|
|
198 |
// DURATION_DAYTIME
|
|
199 |
if (!yearSet
|
|
200 |
&& !monthSet
|
|
201 |
&& daySet
|
|
202 |
&& hourSet
|
|
203 |
&& minuteSet
|
|
204 |
&& secondSet) {
|
|
205 |
return DatatypeConstants.DURATION_DAYTIME;
|
|
206 |
}
|
|
207 |
|
|
208 |
// DURATION_YEARMONTH
|
|
209 |
if (yearSet
|
|
210 |
&& monthSet
|
|
211 |
&& !daySet
|
|
212 |
&& !hourSet
|
|
213 |
&& !minuteSet
|
|
214 |
&& !secondSet) {
|
|
215 |
return DatatypeConstants.DURATION_YEARMONTH;
|
|
216 |
}
|
|
217 |
|
|
218 |
// nothing matches
|
|
219 |
throw new IllegalStateException(
|
|
220 |
"javax.xml.datatype.Duration#getXMLSchemaType():"
|
|
221 |
+ " this Duration does not match one of the XML Schema date/time datatypes:"
|
|
222 |
+ " year set = " + yearSet
|
|
223 |
+ " month set = " + monthSet
|
|
224 |
+ " day set = " + daySet
|
|
225 |
+ " hour set = " + hourSet
|
|
226 |
+ " minute set = " + minuteSet
|
|
227 |
+ " second set = " + secondSet
|
|
228 |
);
|
|
229 |
}
|
|
230 |
|
|
231 |
/**
|
|
232 |
* Returns the sign of this duration in -1,0, or 1.
|
|
233 |
*
|
|
234 |
* @return
|
|
235 |
* -1 if this duration is negative, 0 if the duration is zero,
|
|
236 |
* and 1 if the duration is positive.
|
|
237 |
*/
|
|
238 |
public abstract int getSign();
|
|
239 |
|
|
240 |
/**
|
29999
|
241 |
* Get the years value of this {@code Duration} as an {@code int} or {@code 0} if not present.
|
12005
|
242 |
*
|
29999
|
243 |
* <p>{@code getYears()} is a convenience method for
|
|
244 |
* {@link #getField(DatatypeConstants.Field field) getField(DatatypeConstants.YEARS)}.
|
12005
|
245 |
*
|
29999
|
246 |
* <p>As the return value is an {@code int}, an incorrect value will be returned for {@code Duration}s
|
|
247 |
* with years that go beyond the range of an {@code int}.
|
|
248 |
* Use {@link #getField(DatatypeConstants.Field field) getField(DatatypeConstants.YEARS)} to avoid possible loss of precision.
|
12005
|
249 |
*
|
29999
|
250 |
* @return If the years field is present, return its value as an {@code int}, else return {@code 0}.
|
12005
|
251 |
*/
|
|
252 |
public int getYears() {
|
|
253 |
return getField(DatatypeConstants.YEARS).intValue();
|
|
254 |
}
|
|
255 |
|
|
256 |
/**
|
|
257 |
* Obtains the value of the MONTHS field as an integer value,
|
|
258 |
* or 0 if not present.
|
|
259 |
*
|
|
260 |
* This method works just like {@link #getYears()} except
|
|
261 |
* that this method works on the MONTHS field.
|
|
262 |
*
|
29999
|
263 |
* @return Months of this {@code Duration}.
|
12005
|
264 |
*/
|
|
265 |
public int getMonths() {
|
|
266 |
return getField(DatatypeConstants.MONTHS).intValue();
|
|
267 |
}
|
|
268 |
|
|
269 |
/**
|
|
270 |
* Obtains the value of the DAYS field as an integer value,
|
|
271 |
* or 0 if not present.
|
|
272 |
*
|
|
273 |
* This method works just like {@link #getYears()} except
|
|
274 |
* that this method works on the DAYS field.
|
|
275 |
*
|
29999
|
276 |
* @return Days of this {@code Duration}.
|
12005
|
277 |
*/
|
|
278 |
public int getDays() {
|
|
279 |
return getField(DatatypeConstants.DAYS).intValue();
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* Obtains the value of the HOURS field as an integer value,
|
|
284 |
* or 0 if not present.
|
|
285 |
*
|
|
286 |
* This method works just like {@link #getYears()} except
|
|
287 |
* that this method works on the HOURS field.
|
|
288 |
*
|
29999
|
289 |
* @return Hours of this {@code Duration}.
|
12005
|
290 |
*
|
|
291 |
*/
|
|
292 |
public int getHours() {
|
|
293 |
return getField(DatatypeConstants.HOURS).intValue();
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* Obtains the value of the MINUTES field as an integer value,
|
|
298 |
* or 0 if not present.
|
|
299 |
*
|
|
300 |
* This method works just like {@link #getYears()} except
|
|
301 |
* that this method works on the MINUTES field.
|
|
302 |
*
|
29999
|
303 |
* @return Minutes of this {@code Duration}.
|
12005
|
304 |
*
|
|
305 |
*/
|
|
306 |
public int getMinutes() {
|
|
307 |
return getField(DatatypeConstants.MINUTES).intValue();
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
|
311 |
* Obtains the value of the SECONDS field as an integer value,
|
|
312 |
* or 0 if not present.
|
|
313 |
*
|
|
314 |
* This method works just like {@link #getYears()} except
|
|
315 |
* that this method works on the SECONDS field.
|
|
316 |
*
|
|
317 |
* @return seconds in the integer value. The fraction of seconds
|
|
318 |
* will be discarded (for example, if the actual value is 2.5,
|
|
319 |
* this method returns 2)
|
|
320 |
*/
|
|
321 |
public int getSeconds() {
|
|
322 |
return getField(DatatypeConstants.SECONDS).intValue();
|
|
323 |
}
|
|
324 |
|
|
325 |
/**
|
29999
|
326 |
* Returns the length of the duration in milli-seconds.
|
12005
|
327 |
*
|
|
328 |
* <p>If the seconds field carries more digits than milli-second order,
|
|
329 |
* those will be simply discarded (or in other words, rounded to zero.)
|
29999
|
330 |
* For example, for any Calendar value {@code x},
|
12005
|
331 |
* <pre>
|
29999
|
332 |
* {@code new Duration("PT10.00099S").getTimeInMills(x) == 10000}
|
|
333 |
* {@code new Duration("-PT10.00099S").getTimeInMills(x) == -10000}
|
12005
|
334 |
* </pre>
|
|
335 |
*
|
|
336 |
* <p>
|
|
337 |
* Note that this method uses the {@link #addTo(Calendar)} method,
|
29999
|
338 |
* which may work incorrectly with {@code Duration} objects with
|
12005
|
339 |
* very large values in its fields. See the {@link #addTo(Calendar)}
|
|
340 |
* method for details.
|
|
341 |
*
|
|
342 |
* @param startInstant
|
29999
|
343 |
* The length of a month/year varies. The {@code startInstant} is
|
12005
|
344 |
* used to disambiguate this variance. Specifically, this method
|
29999
|
345 |
* returns the difference between {@code startInstant} and
|
|
346 |
* {@code startInstant+duration}
|
12005
|
347 |
*
|
29999
|
348 |
* @return milliseconds between {@code startInstant} and
|
|
349 |
* {@code startInstant} plus this {@code Duration}
|
12005
|
350 |
*
|
29999
|
351 |
* @throws NullPointerException if {@code startInstant} parameter
|
12005
|
352 |
* is null.
|
|
353 |
*
|
|
354 |
*/
|
|
355 |
public long getTimeInMillis(final Calendar startInstant) {
|
|
356 |
Calendar cal = (Calendar) startInstant.clone();
|
|
357 |
addTo(cal);
|
|
358 |
return getCalendarTimeInMillis(cal)
|
|
359 |
- getCalendarTimeInMillis(startInstant);
|
|
360 |
}
|
|
361 |
|
|
362 |
/**
|
29999
|
363 |
* Returns the length of the duration in milli-seconds.
|
12005
|
364 |
*
|
|
365 |
* <p>If the seconds field carries more digits than milli-second order,
|
|
366 |
* those will be simply discarded (or in other words, rounded to zero.)
|
29999
|
367 |
* For example, for any {@code Date} value {@code x},
|
12005
|
368 |
* <pre>
|
29999
|
369 |
* {@code new Duration("PT10.00099S").getTimeInMills(x) == 10000}
|
|
370 |
* {@code new Duration("-PT10.00099S").getTimeInMills(x) == -10000}
|
12005
|
371 |
* </pre>
|
|
372 |
*
|
|
373 |
* <p>
|
|
374 |
* Note that this method uses the {@link #addTo(Date)} method,
|
29999
|
375 |
* which may work incorrectly with {@code Duration} objects with
|
12005
|
376 |
* very large values in its fields. See the {@link #addTo(Date)}
|
|
377 |
* method for details.
|
|
378 |
*
|
|
379 |
* @param startInstant
|
29999
|
380 |
* The length of a month/year varies. The {@code startInstant} is
|
12005
|
381 |
* used to disambiguate this variance. Specifically, this method
|
29999
|
382 |
* returns the difference between {@code startInstant} and
|
|
383 |
* {@code startInstant+duration}.
|
12005
|
384 |
*
|
|
385 |
* @throws NullPointerException
|
|
386 |
* If the startInstant parameter is null.
|
|
387 |
*
|
29999
|
388 |
* @return milliseconds between {@code startInstant} and
|
|
389 |
* {@code startInstant} plus this {@code Duration}
|
12005
|
390 |
*
|
|
391 |
* @see #getTimeInMillis(Calendar)
|
|
392 |
*/
|
|
393 |
public long getTimeInMillis(final Date startInstant) {
|
|
394 |
Calendar cal = new GregorianCalendar();
|
|
395 |
cal.setTime(startInstant);
|
|
396 |
this.addTo(cal);
|
|
397 |
return getCalendarTimeInMillis(cal) - startInstant.getTime();
|
|
398 |
}
|
|
399 |
|
|
400 |
/**
|
|
401 |
* Gets the value of a field.
|
|
402 |
*
|
|
403 |
* Fields of a duration object may contain arbitrary large value.
|
|
404 |
* Therefore this method is designed to return a {@link Number} object.
|
|
405 |
*
|
|
406 |
* In case of YEARS, MONTHS, DAYS, HOURS, and MINUTES, the returned
|
|
407 |
* number will be a non-negative integer. In case of seconds,
|
|
408 |
* the returned number may be a non-negative decimal value.
|
|
409 |
*
|
|
410 |
* @param field
|
|
411 |
* one of the six Field constants (YEARS,MONTHS,DAYS,HOURS,
|
|
412 |
* MINUTES, or SECONDS.)
|
|
413 |
* @return
|
|
414 |
* If the specified field is present, this method returns
|
|
415 |
* a non-null non-negative {@link Number} object that
|
|
416 |
* represents its value. If it is not present, return null.
|
|
417 |
* For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method
|
|
418 |
* returns a {@link java.math.BigInteger} object. For SECONDS, this
|
|
419 |
* method returns a {@link java.math.BigDecimal}.
|
|
420 |
*
|
29999
|
421 |
* @throws NullPointerException If the {@code field} is {@code null}.
|
12005
|
422 |
*/
|
|
423 |
public abstract Number getField(final DatatypeConstants.Field field);
|
|
424 |
|
|
425 |
/**
|
|
426 |
* Checks if a field is set.
|
|
427 |
*
|
|
428 |
* A field of a duration object may or may not be present.
|
|
429 |
* This method can be used to test if a field is present.
|
|
430 |
*
|
|
431 |
* @param field
|
|
432 |
* one of the six Field constants (YEARS,MONTHS,DAYS,HOURS,
|
|
433 |
* MINUTES, or SECONDS.)
|
|
434 |
* @return
|
|
435 |
* true if the field is present. false if not.
|
|
436 |
*
|
|
437 |
* @throws NullPointerException
|
|
438 |
* If the field parameter is null.
|
|
439 |
*/
|
|
440 |
public abstract boolean isSet(final DatatypeConstants.Field field);
|
|
441 |
|
|
442 |
/**
|
29999
|
443 |
* Computes a new duration whose value is {@code this+rhs}.
|
12005
|
444 |
*
|
29999
|
445 |
* <p>For example,
|
12005
|
446 |
* <pre>
|
|
447 |
* "1 day" + "-3 days" = "-2 days"
|
|
448 |
* "1 year" + "1 day" = "1 year and 1 day"
|
|
449 |
* "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
|
|
450 |
* "15 hours" + "-3 days" = "-(2 days,9 hours)"
|
|
451 |
* "1 year" + "-1 day" = IllegalStateException
|
|
452 |
* </pre>
|
|
453 |
*
|
|
454 |
* <p>Since there's no way to meaningfully subtract 1 day from 1 month,
|
|
455 |
* there are cases where the operation fails in
|
29999
|
456 |
* {@link IllegalStateException}.
|
12005
|
457 |
*
|
|
458 |
* <p>
|
29999
|
459 |
* Formally, the computation is defined as follows.
|
12005
|
460 |
* <p>
|
29999
|
461 |
* Firstly, we can assume that two {@code Duration}s to be added
|
12005
|
462 |
* are both positive without losing generality (i.e.,
|
29999
|
463 |
* {@code (-X)+Y=Y-X}, {@code X+(-Y)=X-Y},
|
|
464 |
* {@code (-X)+(-Y)=-(X+Y)})
|
12005
|
465 |
*
|
|
466 |
* <p>
|
29999
|
467 |
* Addition of two positive {@code Duration}s are simply defined as
|
12005
|
468 |
* field by field addition where missing fields are treated as 0.
|
|
469 |
* <p>
|
29999
|
470 |
* A field of the resulting {@code Duration} will be unset if and
|
|
471 |
* only if respective fields of two input {@code Duration}s are unset.
|
12005
|
472 |
* <p>
|
29999
|
473 |
* Note that {@code lhs.add(rhs)} will be always successful if
|
|
474 |
* {@code lhs.signum()*rhs.signum()!=-1} or both of them are
|
|
475 |
* normalized.
|
12005
|
476 |
*
|
29999
|
477 |
* @param rhs {@code Duration} to add to this {@code Duration}
|
12005
|
478 |
*
|
|
479 |
* @return
|
|
480 |
* non-null valid Duration object.
|
|
481 |
*
|
|
482 |
* @throws NullPointerException
|
|
483 |
* If the rhs parameter is null.
|
|
484 |
* @throws IllegalStateException
|
|
485 |
* If two durations cannot be meaningfully added. For
|
|
486 |
* example, adding negative one day to one month causes
|
|
487 |
* this exception.
|
|
488 |
*
|
|
489 |
*
|
|
490 |
* @see #subtract(Duration)
|
|
491 |
*/
|
|
492 |
public abstract Duration add(final Duration rhs);
|
|
493 |
|
|
494 |
/**
|
|
495 |
* Adds this duration to a {@link Calendar} object.
|
|
496 |
*
|
|
497 |
* <p>
|
|
498 |
* Calls {@link java.util.Calendar#add(int,int)} in the
|
|
499 |
* order of YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, and MILLISECONDS
|
|
500 |
* if those fields are present. Because the {@link Calendar} class
|
|
501 |
* uses int to hold values, there are cases where this method
|
|
502 |
* won't work correctly (for example if values of fields
|
|
503 |
* exceed the range of int.)
|
|
504 |
*
|
|
505 |
* <p>
|
|
506 |
* Also, since this duration class is a Gregorian duration, this
|
|
507 |
* method will not work correctly if the given {@link Calendar}
|
|
508 |
* object is based on some other calendar systems.
|
|
509 |
*
|
|
510 |
* <p>
|
29999
|
511 |
* Any fractional parts of this {@code Duration} object
|
12005
|
512 |
* beyond milliseconds will be simply ignored. For example, if
|
|
513 |
* this duration is "P1.23456S", then 1 is added to SECONDS,
|
|
514 |
* 234 is added to MILLISECONDS, and the rest will be unused.
|
|
515 |
*
|
|
516 |
* <p>
|
|
517 |
* Note that because {@link Calendar#add(int, int)} is using
|
29999
|
518 |
* {@code int}, {@code Duration} with values beyond the
|
|
519 |
* range of {@code int} in its fields
|
12005
|
520 |
* will cause overflow/underflow to the given {@link Calendar}.
|
|
521 |
* {@link XMLGregorianCalendar#add(Duration)} provides the same
|
|
522 |
* basic operation as this method while avoiding
|
|
523 |
* the overflow/underflow issues.
|
|
524 |
*
|
|
525 |
* @param calendar
|
|
526 |
* A calendar object whose value will be modified.
|
|
527 |
* @throws NullPointerException
|
|
528 |
* if the calendar parameter is null.
|
|
529 |
*/
|
|
530 |
public abstract void addTo(Calendar calendar);
|
|
531 |
|
|
532 |
/**
|
|
533 |
* Adds this duration to a {@link Date} object.
|
|
534 |
*
|
|
535 |
* <p>
|
|
536 |
* The given date is first converted into
|
|
537 |
* a {@link java.util.GregorianCalendar}, then the duration
|
|
538 |
* is added exactly like the {@link #addTo(Calendar)} method.
|
|
539 |
*
|
|
540 |
* <p>
|
|
541 |
* The updated time instant is then converted back into a
|
|
542 |
* {@link Date} object and used to update the given {@link Date} object.
|
|
543 |
*
|
|
544 |
* <p>
|
|
545 |
* This somewhat redundant computation is necessary to unambiguously
|
|
546 |
* determine the duration of months and years.
|
|
547 |
*
|
|
548 |
* @param date
|
|
549 |
* A date object whose value will be modified.
|
|
550 |
* @throws NullPointerException
|
|
551 |
* if the date parameter is null.
|
|
552 |
*/
|
|
553 |
public void addTo(Date date) {
|
|
554 |
|
|
555 |
// check data parameter
|
|
556 |
if (date == null) {
|
|
557 |
throw new NullPointerException(
|
|
558 |
"Cannot call "
|
|
559 |
+ this.getClass().getName()
|
|
560 |
+ "#addTo(Date date) with date == null."
|
|
561 |
);
|
|
562 |
}
|
|
563 |
|
|
564 |
Calendar cal = new GregorianCalendar();
|
|
565 |
cal.setTime(date);
|
|
566 |
this.addTo(cal);
|
|
567 |
date.setTime(getCalendarTimeInMillis(cal));
|
|
568 |
}
|
|
569 |
|
|
570 |
/**
|
29999
|
571 |
* Computes a new duration whose value is {@code this-rhs}.
|
12005
|
572 |
*
|
29999
|
573 |
* <p>For example:
|
12005
|
574 |
* <pre>
|
|
575 |
* "1 day" - "-3 days" = "4 days"
|
|
576 |
* "1 year" - "1 day" = IllegalStateException
|
|
577 |
* "-(1 hour,50 minutes)" - "-20 minutes" = "-(1hours,30 minutes)"
|
|
578 |
* "15 hours" - "-3 days" = "3 days and 15 hours"
|
|
579 |
* "1 year" - "-1 day" = "1 year and 1 day"
|
|
580 |
* </pre>
|
|
581 |
*
|
|
582 |
* <p>Since there's no way to meaningfully subtract 1 day from 1 month,
|
29999
|
583 |
* there are cases where the operation fails in {@link IllegalStateException}.
|
12005
|
584 |
*
|
|
585 |
* <p>Formally the computation is defined as follows.
|
29999
|
586 |
* First, we can assume that two {@code Duration}s are both positive
|
12005
|
587 |
* without losing generality. (i.e.,
|
29999
|
588 |
* {@code (-X)-Y=-(X+Y)}, {@code X-(-Y)=X+Y},
|
|
589 |
* {@code (-X)-(-Y)=-(X-Y)})
|
12005
|
590 |
*
|
|
591 |
* <p>Then two durations are subtracted field by field.
|
29999
|
592 |
* If the sign of any non-zero field {@code F} is different from
|
12005
|
593 |
* the sign of the most significant field,
|
29999
|
594 |
* 1 (if {@code F} is negative) or -1 (otherwise)
|
|
595 |
* will be borrowed from the next bigger unit of {@code F}.
|
12005
|
596 |
*
|
|
597 |
* <p>This process is repeated until all the non-zero fields have
|
29999
|
598 |
* the same sign.
|
12005
|
599 |
*
|
|
600 |
* <p>If a borrow occurs in the days field (in other words, if
|
|
601 |
* the computation needs to borrow 1 or -1 month to compensate
|
|
602 |
* days), then the computation fails by throwing an
|
29999
|
603 |
* {@link IllegalStateException}.
|
12005
|
604 |
*
|
29999
|
605 |
* @param rhs {@code Duration} to subtract from this {@code Duration}.
|
12005
|
606 |
*
|
29999
|
607 |
* @return New {@code Duration} created from subtracting {@code rhs} from this {@code Duration}.
|
12005
|
608 |
*
|
|
609 |
* @throws IllegalStateException
|
|
610 |
* If two durations cannot be meaningfully subtracted. For
|
|
611 |
* example, subtracting one day from one month causes
|
|
612 |
* this exception.
|
|
613 |
*
|
|
614 |
* @throws NullPointerException
|
|
615 |
* If the rhs parameter is null.
|
|
616 |
*
|
|
617 |
* @see #add(Duration)
|
|
618 |
*/
|
|
619 |
public Duration subtract(final Duration rhs) {
|
|
620 |
return add(rhs.negate());
|
|
621 |
}
|
|
622 |
|
|
623 |
/**
|
29999
|
624 |
* Computes a new duration whose value is {@code factor} times
|
|
625 |
* longer than the value of this duration.
|
12005
|
626 |
*
|
|
627 |
* <p>This method is provided for the convenience.
|
29999
|
628 |
* It is functionally equivalent to the following code:
|
12005
|
629 |
* <pre>
|
|
630 |
* multiply(new BigDecimal(String.valueOf(factor)))
|
|
631 |
* </pre>
|
|
632 |
*
|
29999
|
633 |
* @param factor Factor times longer of new {@code Duration} to create.
|
12005
|
634 |
*
|
29999
|
635 |
* @return New {@code Duration} that is {@code factor}times longer than this {@code Duration}.
|
12005
|
636 |
*
|
|
637 |
* @see #multiply(BigDecimal)
|
|
638 |
*/
|
|
639 |
public Duration multiply(int factor) {
|
|
640 |
return multiply(new BigDecimal(String.valueOf(factor)));
|
|
641 |
}
|
|
642 |
|
|
643 |
/**
|
29999
|
644 |
* Computes a new duration whose value is {@code factor} times
|
12005
|
645 |
* longer than the value of this duration.
|
|
646 |
*
|
|
647 |
* <p>
|
|
648 |
* For example,
|
|
649 |
* <pre>
|
|
650 |
* "P1M" (1 month) * "12" = "P12M" (12 months)
|
|
651 |
* "PT1M" (1 min) * "0.3" = "PT18S" (18 seconds)
|
|
652 |
* "P1M" (1 month) * "1.5" = IllegalStateException
|
|
653 |
* </pre>
|
|
654 |
*
|
|
655 |
* <p>
|
29999
|
656 |
* Since the {@code Duration} class is immutable, this method
|
12005
|
657 |
* doesn't change the value of this object. It simply computes
|
|
658 |
* a new Duration object and returns it.
|
|
659 |
*
|
|
660 |
* <p>
|
|
661 |
* The operation will be performed field by field with the precision
|
|
662 |
* of {@link BigDecimal}. Since all the fields except seconds are
|
|
663 |
* restricted to hold integers,
|
|
664 |
* any fraction produced by the computation will be
|
|
665 |
* carried down toward the next lower unit. For example,
|
|
666 |
* if you multiply "P1D" (1 day) with "0.5", then it will be 0.5 day,
|
|
667 |
* which will be carried down to "PT12H" (12 hours).
|
|
668 |
* When fractions of month cannot be meaningfully carried down
|
|
669 |
* to days, or year to months, this will cause an
|
|
670 |
* {@link IllegalStateException} to be thrown.
|
29999
|
671 |
* For example if you multiple one month by 0.5.
|
12005
|
672 |
*
|
|
673 |
* <p>
|
|
674 |
* To avoid {@link IllegalStateException}, use
|
|
675 |
* the {@link #normalizeWith(Calendar)} method to remove the years
|
|
676 |
* and months fields.
|
|
677 |
*
|
|
678 |
* @param factor to multiply by
|
|
679 |
*
|
|
680 |
* @return
|
29999
|
681 |
* returns a non-null valid {@code Duration} object
|
12005
|
682 |
*
|
|
683 |
* @throws IllegalStateException if operation produces fraction in
|
|
684 |
* the months field.
|
|
685 |
*
|
29999
|
686 |
* @throws NullPointerException if the {@code factor} parameter is
|
|
687 |
* {@code null}.
|
12005
|
688 |
*
|
|
689 |
*/
|
|
690 |
public abstract Duration multiply(final BigDecimal factor);
|
|
691 |
|
|
692 |
/**
|
29999
|
693 |
* Returns a new {@code Duration} object whose
|
|
694 |
* value is {@code -this}.
|
12005
|
695 |
*
|
|
696 |
* <p>
|
29999
|
697 |
* Since the {@code Duration} class is immutable, this method
|
12005
|
698 |
* doesn't change the value of this object. It simply computes
|
|
699 |
* a new Duration object and returns it.
|
|
700 |
*
|
|
701 |
* @return
|
29999
|
702 |
* always return a non-null valid {@code Duration} object.
|
12005
|
703 |
*/
|
|
704 |
public abstract Duration negate();
|
|
705 |
|
|
706 |
/**
|
29999
|
707 |
* Converts the years and months fields into the days field
|
|
708 |
* by using a specific time instant as the reference point.
|
12005
|
709 |
*
|
|
710 |
* <p>For example, duration of one month normalizes to 31 days
|
29999
|
711 |
* given the start time instance "July 8th 2003, 17:40:32".
|
12005
|
712 |
*
|
29999
|
713 |
* <p>Formally, the computation is done as follows:
|
12005
|
714 |
* <ol>
|
|
715 |
* <li>the given Calendar object is cloned</li>
|
|
716 |
* <li>the years, months and days fields will be added to the {@link Calendar} object
|
|
717 |
* by using the {@link Calendar#add(int,int)} method</li>
|
|
718 |
* <li>the difference between the two Calendars in computed in milliseconds and converted to days,
|
|
719 |
* if a remainder occurs due to Daylight Savings Time, it is discarded</li>
|
|
720 |
* <li>the computed days, along with the hours, minutes and seconds
|
|
721 |
* fields of this duration object is used to construct a new
|
|
722 |
* Duration object.</li>
|
|
723 |
* </ol>
|
|
724 |
*
|
29999
|
725 |
* <p>Note that since the Calendar class uses {@code int} to
|
12005
|
726 |
* hold the value of year and month, this method may produce
|
|
727 |
* an unexpected result if this duration object holds
|
29999
|
728 |
* a very large value in the years or months fields.
|
12005
|
729 |
*
|
29999
|
730 |
* @param startTimeInstant {@code Calendar} reference point.
|
12005
|
731 |
*
|
29999
|
732 |
* @return {@code Duration} of years and months of this {@code Duration} as days.
|
12005
|
733 |
*
|
|
734 |
* @throws NullPointerException If the startTimeInstant parameter is null.
|
|
735 |
*/
|
|
736 |
public abstract Duration normalizeWith(final Calendar startTimeInstant);
|
|
737 |
|
|
738 |
/**
|
29999
|
739 |
* Partial order relation comparison with this {@code Duration} instance.
|
12005
|
740 |
*
|
|
741 |
* <p>Comparison result must be in accordance with
|
|
742 |
* <a href="http://www.w3.org/TR/xmlschema-2/#duration-order">W3C XML Schema 1.0 Part 2, Section 3.2.7.6.2,
|
29999
|
743 |
* <i>Order relation on duration</i></a>.
|
12005
|
744 |
*
|
29999
|
745 |
* <p>Return:
|
12005
|
746 |
* <ul>
|
29999
|
747 |
* <li>{@link DatatypeConstants#LESSER} if this {@code Duration} is shorter than {@code duration} parameter</li>
|
|
748 |
* <li>{@link DatatypeConstants#EQUAL} if this {@code Duration} is equal to {@code duration} parameter</li>
|
|
749 |
* <li>{@link DatatypeConstants#GREATER} if this {@code Duration} is longer than {@code duration} parameter</li>
|
12005
|
750 |
* <li>{@link DatatypeConstants#INDETERMINATE} if a conclusive partial order relation cannot be determined</li>
|
|
751 |
* </ul>
|
|
752 |
*
|
|
753 |
* @param duration to compare
|
|
754 |
*
|
29999
|
755 |
* @return the relationship between {@code this Duration} and {@code duration} parameter as
|
12005
|
756 |
* {@link DatatypeConstants#LESSER}, {@link DatatypeConstants#EQUAL}, {@link DatatypeConstants#GREATER}
|
|
757 |
* or {@link DatatypeConstants#INDETERMINATE}.
|
|
758 |
*
|
|
759 |
* @throws UnsupportedOperationException If the underlying implementation
|
|
760 |
* cannot reasonably process the request, e.g. W3C XML Schema allows for
|
|
761 |
* arbitrarily large/small/precise values, the request may be beyond the
|
|
762 |
* implementations capability.
|
29999
|
763 |
* @throws NullPointerException if {@code duration} is {@code null}.
|
12005
|
764 |
*
|
|
765 |
* @see #isShorterThan(Duration)
|
|
766 |
* @see #isLongerThan(Duration)
|
|
767 |
*/
|
|
768 |
public abstract int compare(final Duration duration);
|
|
769 |
|
|
770 |
/**
|
29999
|
771 |
* Checks if this duration object is strictly longer than
|
|
772 |
* another {@code Duration} object.
|
12005
|
773 |
*
|
29999
|
774 |
* <p>Duration X is "longer" than Y if and only if X {@literal >} Y
|
12005
|
775 |
* as defined in the section 3.2.6.2 of the XML Schema 1.0
|
29999
|
776 |
* specification.
|
12005
|
777 |
*
|
29999
|
778 |
* <p>For example, "P1D" (one day) {@literal >} "PT12H" (12 hours) and
|
|
779 |
* "P2Y" (two years) {@literal >} "P23M" (23 months).
|
12005
|
780 |
*
|
29999
|
781 |
* @param duration {@code Duration} to test this {@code Duration} against.
|
12005
|
782 |
*
|
|
783 |
* @throws UnsupportedOperationException If the underlying implementation
|
|
784 |
* cannot reasonably process the request, e.g. W3C XML Schema allows for
|
|
785 |
* arbitrarily large/small/precise values, the request may be beyond the
|
|
786 |
* implementations capability.
|
29999
|
787 |
* @throws NullPointerException If {@code duration} is null.
|
12005
|
788 |
*
|
|
789 |
* @return
|
|
790 |
* true if the duration represented by this object
|
|
791 |
* is longer than the given duration. false otherwise.
|
|
792 |
*
|
|
793 |
* @see #isShorterThan(Duration)
|
|
794 |
* @see #compare(Duration duration)
|
|
795 |
*/
|
|
796 |
public boolean isLongerThan(final Duration duration) {
|
|
797 |
return compare(duration) == DatatypeConstants.GREATER;
|
|
798 |
}
|
|
799 |
|
|
800 |
/**
|
29999
|
801 |
* Checks if this duration object is strictly shorter than
|
|
802 |
* another {@code Duration} object.
|
12005
|
803 |
*
|
29999
|
804 |
* @param duration {@code Duration} to test this {@code Duration} against.
|
12005
|
805 |
*
|
29999
|
806 |
* @return {@code true} if {@code duration} parameter is shorter than this {@code Duration},
|
|
807 |
* else {@code false}.
|
12005
|
808 |
*
|
|
809 |
* @throws UnsupportedOperationException If the underlying implementation
|
|
810 |
* cannot reasonably process the request, e.g. W3C XML Schema allows for
|
|
811 |
* arbitrarily large/small/precise values, the request may be beyond the
|
|
812 |
* implementations capability.
|
29999
|
813 |
* @throws NullPointerException if {@code duration} is null.
|
12005
|
814 |
*
|
|
815 |
* @see #isLongerThan(Duration duration)
|
|
816 |
* @see #compare(Duration duration)
|
|
817 |
*/
|
|
818 |
public boolean isShorterThan(final Duration duration) {
|
|
819 |
return compare(duration) == DatatypeConstants.LESSER;
|
|
820 |
}
|
|
821 |
|
|
822 |
/**
|
29999
|
823 |
* Checks if this duration object has the same duration
|
|
824 |
* as another {@code Duration} object.
|
12005
|
825 |
*
|
29999
|
826 |
* <p>For example, "P1D" (1 day) is equal to "PT24H" (24 hours).
|
12005
|
827 |
*
|
|
828 |
* <p>Duration X is equal to Y if and only if time instant
|
|
829 |
* t+X and t+Y are the same for all the test time instants
|
|
830 |
* specified in the section 3.2.6.2 of the XML Schema 1.0
|
29999
|
831 |
* specification.
|
12005
|
832 |
*
|
29999
|
833 |
* <p>Note that there are cases where two {@code Duration}s are
|
12005
|
834 |
* "incomparable" to each other, like one month and 30 days.
|
29999
|
835 |
* For example,
|
12005
|
836 |
* <pre>
|
|
837 |
* !new Duration("P1M").isShorterThan(new Duration("P30D"))
|
|
838 |
* !new Duration("P1M").isLongerThan(new Duration("P30D"))
|
|
839 |
* !new Duration("P1M").equals(new Duration("P30D"))
|
|
840 |
* </pre>
|
|
841 |
*
|
|
842 |
* @param duration
|
29999
|
843 |
* The object to compare this {@code Duration} against.
|
12005
|
844 |
*
|
|
845 |
* @return
|
29999
|
846 |
* {@code true} if this duration is the same length as
|
|
847 |
* {@code duration}.
|
|
848 |
* {@code false} if {@code duration} is {@code null},
|
12005
|
849 |
* is not a
|
29999
|
850 |
* {@code Duration} object,
|
12005
|
851 |
* or its length is different from this duration.
|
|
852 |
*
|
|
853 |
* @throws UnsupportedOperationException If the underlying implementation
|
|
854 |
* cannot reasonably process the request, e.g. W3C XML Schema allows for
|
|
855 |
* arbitrarily large/small/precise values, the request may be beyond the
|
|
856 |
* implementations capability.
|
|
857 |
*
|
|
858 |
* @see #compare(Duration duration)
|
|
859 |
*/
|
|
860 |
public boolean equals(final Object duration) {
|
|
861 |
|
|
862 |
if (duration == null || !(duration instanceof Duration)) {
|
|
863 |
return false;
|
|
864 |
}
|
|
865 |
|
|
866 |
return compare((Duration) duration) == DatatypeConstants.EQUAL;
|
|
867 |
}
|
|
868 |
|
|
869 |
/**
|
|
870 |
* Returns a hash code consistent with the definition of the equals method.
|
|
871 |
*
|
|
872 |
* @see Object#hashCode()
|
|
873 |
*/
|
|
874 |
public abstract int hashCode();
|
|
875 |
|
|
876 |
/**
|
29999
|
877 |
* Returns a {@code String} representation of this {@code Duration Object}.
|
12005
|
878 |
*
|
29999
|
879 |
* <p>The result is formatted according to the XML Schema 1.0 spec
|
|
880 |
* and can be always parsed back later into the
|
|
881 |
* equivalent {@code Duration Object} by {@link DatatypeFactory#newDuration(String lexicalRepresentation)}.
|
12005
|
882 |
*
|
29999
|
883 |
* <p>Formally, the following holds for any {@code Duration}
|
|
884 |
* {@code Object} x:
|
12005
|
885 |
* <pre>
|
|
886 |
* new Duration(x.toString()).equals(x)
|
|
887 |
* </pre>
|
|
888 |
*
|
29999
|
889 |
* @return A non-{@code null} valid {@code String} representation of this {@code Duration}.
|
12005
|
890 |
*/
|
|
891 |
public String toString() {
|
|
892 |
|
|
893 |
StringBuffer buf = new StringBuffer();
|
|
894 |
|
|
895 |
if (getSign() < 0) {
|
|
896 |
buf.append('-');
|
|
897 |
}
|
|
898 |
buf.append('P');
|
|
899 |
|
|
900 |
BigInteger years = (BigInteger) getField(DatatypeConstants.YEARS);
|
|
901 |
if (years != null) {
|
|
902 |
buf.append(years + "Y");
|
|
903 |
}
|
|
904 |
|
|
905 |
BigInteger months = (BigInteger) getField(DatatypeConstants.MONTHS);
|
|
906 |
if (months != null) {
|
|
907 |
buf.append(months + "M");
|
|
908 |
}
|
|
909 |
|
|
910 |
BigInteger days = (BigInteger) getField(DatatypeConstants.DAYS);
|
|
911 |
if (days != null) {
|
|
912 |
buf.append(days + "D");
|
|
913 |
}
|
|
914 |
|
|
915 |
BigInteger hours = (BigInteger) getField(DatatypeConstants.HOURS);
|
|
916 |
BigInteger minutes = (BigInteger) getField(DatatypeConstants.MINUTES);
|
|
917 |
BigDecimal seconds = (BigDecimal) getField(DatatypeConstants.SECONDS);
|
|
918 |
if (hours != null || minutes != null || seconds != null) {
|
|
919 |
buf.append('T');
|
|
920 |
if (hours != null) {
|
|
921 |
buf.append(hours + "H");
|
|
922 |
}
|
|
923 |
if (minutes != null) {
|
|
924 |
buf.append(minutes + "M");
|
|
925 |
}
|
|
926 |
if (seconds != null) {
|
|
927 |
buf.append(toString(seconds) + "S");
|
|
928 |
}
|
|
929 |
}
|
|
930 |
|
|
931 |
return buf.toString();
|
|
932 |
}
|
|
933 |
|
|
934 |
/**
|
29999
|
935 |
* Turns {@link BigDecimal} to a string representation.
|
12005
|
936 |
*
|
|
937 |
* <p>Due to a behavior change in the {@link BigDecimal#toString()}
|
29999
|
938 |
* method in JDK1.5, this had to be implemented here.
|
12005
|
939 |
*
|
29999
|
940 |
* @param bd {@code BigDecimal} to format as a {@code String}
|
12005
|
941 |
*
|
29999
|
942 |
* @return {@code String} representation of {@code BigDecimal}
|
12005
|
943 |
*/
|
|
944 |
private String toString(BigDecimal bd) {
|
|
945 |
String intString = bd.unscaledValue().toString();
|
|
946 |
int scale = bd.scale();
|
|
947 |
|
|
948 |
if (scale == 0) {
|
|
949 |
return intString;
|
|
950 |
}
|
|
951 |
|
|
952 |
/* Insert decimal point */
|
|
953 |
StringBuffer buf;
|
|
954 |
int insertionPoint = intString.length() - scale;
|
|
955 |
if (insertionPoint == 0) { /* Point goes right before intVal */
|
|
956 |
return "0." + intString;
|
|
957 |
} else if (insertionPoint > 0) { /* Point goes inside intVal */
|
|
958 |
buf = new StringBuffer(intString);
|
|
959 |
buf.insert(insertionPoint, '.');
|
|
960 |
} else { /* We must insert zeros between point and intVal */
|
|
961 |
buf = new StringBuffer(3 - insertionPoint + intString.length());
|
|
962 |
buf.append("0.");
|
|
963 |
for (int i = 0; i < -insertionPoint; i++) {
|
|
964 |
buf.append('0');
|
|
965 |
}
|
|
966 |
buf.append(intString);
|
|
967 |
}
|
|
968 |
return buf.toString();
|
|
969 |
}
|
|
970 |
|
|
971 |
|
|
972 |
/**
|
29999
|
973 |
* Calls the {@link Calendar#getTimeInMillis} method.
|
12005
|
974 |
* Prior to JDK1.4, this method was protected and therefore
|
29999
|
975 |
* cannot be invoked directly.
|
12005
|
976 |
*
|
29999
|
977 |
* <p>TODO: In future, this should be replaced by {@code cal.getTimeInMillis()}.
|
12005
|
978 |
*
|
29999
|
979 |
* @param cal {@code Calendar} to get time in milliseconds.
|
12005
|
980 |
*
|
29999
|
981 |
* @return Milliseconds of {@code cal}.
|
12005
|
982 |
*/
|
|
983 |
private static long getCalendarTimeInMillis(final Calendar cal) {
|
|
984 |
return cal.getTime().getTime();
|
|
985 |
}
|
|
986 |
}
|