KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > ReadableDuration


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;
17
18 /**
19  * Defines an exact duration of time in milliseconds.
20  * <p>
21  * The implementation of this interface may be mutable or immutable. This
22  * interface only gives access to retrieve data, never to change it.
23  * <p>
24  * Methods that are passed a duration as a parameter will treat <code>null</code>
25  * as a zero length duration.
26  *
27  * @see ReadableInterval
28  * @see ReadablePeriod
29  * @author Brian S O'Neill
30  * @author Stephen Colebourne
31  * @since 1.0
32  */

33 public interface ReadableDuration extends Comparable JavaDoc {
34
35     /**
36      * Gets the total length of this duration in milliseconds.
37      *
38      * @return the total length of the time duration in milliseconds.
39      */

40     long getMillis();
41
42     //-----------------------------------------------------------------------
43
/**
44      * Get this duration as an immutable <code>Duration</code> object.
45      * <p>
46      * This will either typecast this instance, or create a new <code>Duration</code>.
47      *
48      * @return a Duration created using the millisecond duration from this instance
49      */

50     Duration toDuration();
51
52     //-----------------------------------------------------------------------
53
/**
54      * Converts this duration to a Period instance using the standard period type
55      * and the ISO chronology.
56      * <p>
57      * Only precise fields in the period type will be used. Thus, only the hour,
58      * minute, second and millisecond fields on the period will be used.
59      * The year, month, week and day fields will not be populated.
60      * <p>
61      * If the duration is small, less than one day, then this method will perform
62      * as you might expect and split the fields evenly.
63      * If the duration is larger than one day then all the remaining duration will
64      * be stored in the largest available field, hours in this case.
65      * <p>
66      * For example, a duration effectively equal to (365 + 60 + 5) days will be
67      * converted to ((365 + 60 + 5) * 24) hours by this constructor.
68      * <p>
69      * For more control over the conversion process, you must pair the duration with
70      * an instant, see {@link Period#Period(ReadableInstant,ReadableDuration)}.
71      *
72      * @return a Period created using the millisecond duration from this instance
73      */

74     Period toPeriod();
75
76     //-----------------------------------------------------------------------
77
/**
78      * Compares this duration with the specified duration based on length.
79      *
80      * @param obj a duration to check against
81      * @return negative value if this is less, 0 if equal, or positive value if greater
82      * @throws NullPointerException if the object is null
83      * @throws ClassCastException if the given object is not supported
84      */

85     int compareTo(Object JavaDoc obj);
86
87     /**
88      * Is the length of this duration equal to the duration passed in.
89      *
90      * @param duration another duration to compare to, null means zero milliseconds
91      * @return true if this duration is equal to than the duration passed in
92      */

93     boolean isEqual(ReadableDuration duration);
94
95     /**
96      * Is the length of this duration longer than the duration passed in.
97      *
98      * @param duration another duration to compare to, null means zero milliseconds
99      * @return true if this duration is equal to than the duration passed in
100      */

101     boolean isLongerThan(ReadableDuration duration);
102
103     /**
104      * Is the length of this duration shorter than the duration passed in.
105      *
106      * @param duration another duration to compare to, null means zero milliseconds
107      * @return true if this duration is equal to than the duration passed in
108      */

109     boolean isShorterThan(ReadableDuration duration);
110
111     //-----------------------------------------------------------------------
112
/**
113      * Compares this object with the specified object for equality based
114      * on the millisecond length. All ReadableDuration instances are accepted.
115      *
116      * @param readableDuration a readable duration to check against
117      * @return true if the length of the duration is equal
118      */

119     boolean equals(Object JavaDoc readableDuration);
120
121     /**
122      * Gets a hash code for the duration that is compatable with the
123      * equals method.
124      * The following formula must be used:
125      * <pre>
126      * long len = getMillis();
127      * return (int) (len ^ (len >>> 32));
128      * </pre>
129      *
130      * @return a hash code
131      */

132     int hashCode();
133
134     //-----------------------------------------------------------------------
135
/**
136      * Gets the value as a String in the ISO8601 duration format using hours,
137      * minutes and seconds (including fractional milliseconds).
138      * <p>
139      * For example, "PT6H3M7S" represents 6 hours, 3 minutes, 7 seconds.
140      *
141      * @return the value as an ISO8601 string
142      */

143     String JavaDoc toString();
144
145 }
146
Popular Tags