KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > Interval


1 package org.oddjob.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.text.SimpleDateFormat JavaDoc;
5 import java.util.Date JavaDoc;
6
7 /**
8  * A representation of an interval between two dates.
9  *
10  * @author Rob Gordon
11  */

12
13 public class Interval implements Serializable JavaDoc {
14     private static final long serialVersionUID = 20050226;
15     
16     public static final long NEVER_AGAIN = Long.MAX_VALUE;
17     public static final long ALWAYS = Long.MIN_VALUE;
18     
19     
20     private final Date JavaDoc fromDate;
21     private final Date JavaDoc toDate;
22     
23     
24     /**
25      * Constructor for an interval between two dates.
26      *
27      * @param from The starting date.
28      * @param to The ending date.
29      */

30     public Interval(Date JavaDoc from, Date JavaDoc to) {
31         this(from.getTime(), to.getTime());
32     }
33     
34     
35     /**
36      * Constructor for an interval between two dates given as milliseconds.
37      *
38      * @param fromTime
39      * @param toTime
40      */

41     public Interval(long fromTime, long toTime) {
42         fromDate = new Date JavaDoc(fromTime);
43         toDate = new Date JavaDoc(toTime);
44         if (toTime < fromTime) {
45             throw new IllegalStateException JavaDoc("An interval can not have a to [" +
46                     toDate + "] before the from [" + fromDate + "]");
47         }
48     }
49
50     /**
51      * The copy constructor. This is a shallow copy.
52      *
53      * @param other The other interval.
54      */

55     public Interval(Interval other) {
56         this(other.fromDate.getTime(), other.toDate.getTime());
57     }
58
59     /**
60      * Get the from date of the interval.
61      *
62      * @return The from date.
63      */

64     public Date JavaDoc getFromDate() {
65         return fromDate;
66     }
67     
68     /**
69      * Get the to date of the interval.
70      *
71      * @return The to date.
72      */

73     public Date JavaDoc getToDate() {
74         return toDate;
75     }
76     
77     /**
78      * Crude implementation of hashCode, so intervals could
79      * be stored in HashSets.
80      *
81      */

82     public int hashCode() {
83         return fromDate.hashCode() + toDate.hashCode();
84     }
85
86     /**
87      * Test if two interval are equivelent.
88      * <p>
89      * Intervals are equivelant if there start and end times are the same.
90      *
91      * @param other The interval to test against.
92      * @return true if they are equal.
93      */

94     public boolean equals(Object JavaDoc other) {
95         if (!(other instanceof Interval)) {
96             return false;
97         }
98         return this.toDate.equals(((Interval)other).toDate)
99                 && this.fromDate.equals(((Interval)other).fromDate);
100     }
101     
102     /**
103      * Test if this interval is before the other interval.
104      * <p>
105      * This happens when this from time is less than other from time.
106      * <pre>
107      * this: ---- or ----
108      * other: ---- ----
109      * </pre>
110      * @param other
111      * @return true if this is before the other.
112      */

113
114     public boolean isBefore(Interval other) {
115         if (other == null) {
116             return true;
117         }
118         return this.fromDate.getTime() < other.fromDate.getTime();
119     }
120     
121     /**
122      * Test if this interval is past the other interval.
123      * <p>
124      * This happens when the this from time is greater than the other ones to time.
125      * <pre>
126      * this: ----
127      * other: ----
128      * </pre>
129      * @return true if this is past the other.
130      */

131     
132     public boolean isPast(Interval other) {
133         if (other == null) {
134             return true;
135         }
136         return this.fromDate.getTime() > other.toDate.getTime();
137     }
138             
139     /**
140      * Limit the interval.
141      *
142      * @param other The other interval. may be null.
143      * @return The new interval.
144      */

145
146     public Interval limit(Interval limit) {
147         if (limit == null) {
148             return this;
149         }
150         if (limit.fromDate.compareTo(this.fromDate) > 0
151            || this.fromDate.compareTo(limit.toDate) > 0) {
152             return null;
153         }
154         return this;
155     }
156
157     /**
158      * Is this interval really a point in time, not an interval.
159      *
160      * @return true if it is a point in time.
161      */

162     public boolean isPoint() {
163         return fromDate.equals(toDate);
164     }
165     
166     /**
167      * Return a string representation of this interval.
168      */

169     public String JavaDoc toString() {
170         String JavaDoc fromString;
171         if (fromDate.getTime() == ALWAYS) {
172             fromString = "Always";
173         } else {
174             SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm:ss:SSS");
175             if (fromDate.getTime() % 1000 == 0) {
176                 // no milliseconds - then miss them off.
177
format = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm:ss");
178             }
179             fromString = format.format(fromDate);
180         }
181         
182         String JavaDoc toString;
183         if (toDate.getTime() == NEVER_AGAIN) {
184             toString = "Never Again";
185         }
186         else {
187             SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm:ss:SSS");
188             if (toDate.getTime() + 1 % 1000 == 0) {
189                 // no milliseconds - then miss them off.
190
format = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm:ss");
191             }
192             toString = format.format(toDate);
193         }
194         return fromString + " to " + toString;
195     }
196 }
197
Popular Tags