001 /*
002 * Copyright 2001-2005 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.time;
017
018 /**
019 * Defines an exact duration of time in milliseconds.
020 * <p>
021 * The implementation of this interface may be mutable or immutable. This
022 * interface only gives access to retrieve data, never to change it.
023 * <p>
024 * Methods that are passed a duration as a parameter will treat <code>null</code>
025 * as a zero length duration.
026 *
027 * @see ReadableInterval
028 * @see ReadablePeriod
029 * @author Brian S O'Neill
030 * @author Stephen Colebourne
031 * @since 1.0
032 */
033 public interface ReadableDuration extends Comparable {
034
035 /**
036 * Gets the total length of this duration in milliseconds.
037 *
038 * @return the total length of the time duration in milliseconds.
039 */
040 long getMillis();
041
042 //-----------------------------------------------------------------------
043 /**
044 * Get this duration as an immutable <code>Duration</code> object.
045 * <p>
046 * This will either typecast this instance, or create a new <code>Duration</code>.
047 *
048 * @return a Duration created using the millisecond duration from this instance
049 */
050 Duration toDuration();
051
052 //-----------------------------------------------------------------------
053 /**
054 * Converts this duration to a Period instance using the standard period type
055 * and the ISO chronology.
056 * <p>
057 * Only precise fields in the period type will be used. Thus, only the hour,
058 * minute, second and millisecond fields on the period will be used.
059 * The year, month, week and day fields will not be populated.
060 * <p>
061 * If the duration is small, less than one day, then this method will perform
062 * as you might expect and split the fields evenly.
063 * If the duration is larger than one day then all the remaining duration will
064 * be stored in the largest available field, hours in this case.
065 * <p>
066 * For example, a duration effectively equal to (365 + 60 + 5) days will be
067 * converted to ((365 + 60 + 5) * 24) hours by this constructor.
068 * <p>
069 * For more control over the conversion process, you must pair the duration with
070 * an instant, see {@link Period#Period(ReadableInstant,ReadableDuration)}.
071 *
072 * @return a Period created using the millisecond duration from this instance
073 */
074 Period toPeriod();
075
076 //-----------------------------------------------------------------------
077 /**
078 * Compares this duration with the specified duration based on length.
079 *
080 * @param obj a duration to check against
081 * @return negative value if this is less, 0 if equal, or positive value if greater
082 * @throws NullPointerException if the object is null
083 * @throws ClassCastException if the given object is not supported
084 */
085 int compareTo(Object obj);
086
087 /**
088 * Is the length of this duration equal to the duration passed in.
089 *
090 * @param duration another duration to compare to, null means zero milliseconds
091 * @return true if this duration is equal to than the duration passed in
092 */
093 boolean isEqual(ReadableDuration duration);
094
095 /**
096 * Is the length of this duration longer than the duration passed in.
097 *
098 * @param duration another duration to compare to, null means zero milliseconds
099 * @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 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 toString();
144
145 }