KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > calendar > DateSpan


1 /*
2  * $Id: DateSpan.java,v 1.1 2004/08/06 22:54:49 dmouse Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package org.jdesktop.swing.calendar;
8
9 import java.util.Date JavaDoc;
10
11 /**
12  * An immutable representation of a time range. The time range is
13  * internally represented as two longs. The methods that take and return
14  * <code>Date</code>s create the <code>Date</code>s as needed, so that
15  * if you modify returned <code>Date</code>s you will <b>not</b> effect
16  * the <code>DateSpan</code>. The end points are inclusive.
17  *
18  * @version $Revision: 1.1 $
19  */

20 public class DateSpan {
21     private long _start;
22     private long _end;
23
24     /**
25      * Creates a <code>DateSpan</code> between the two end points.
26      *
27      * @param start Beginning date
28      * @param end Ending date
29      * @throws IllegalArgumentException if <code>start</code> is after
30      * <code>end</code>
31      */

32     public DateSpan(long start, long end) {
33         _start = start;
34         _end = end;
35         if (_start > _end) {
36             throw new IllegalArgumentException JavaDoc(
37                              "Start date must be before end date");
38         }
39     }
40
41     /**
42      * Creates a <code>DateSpan</code> between the two end points. This
43      * is a conveniance constructor that is equivalent to
44      * <code>new Date(start.getTime(), end.getTime());</code>.
45      *
46      * @param start Beginning date
47      * @param end Ending date
48      */

49     public DateSpan(Date JavaDoc start, Date JavaDoc end) {
50         this(start.getTime(), end.getTime());
51     }
52
53     /**
54      * Returns the start of the date span.
55      *
56      * @return start of the span.
57      */

58     public long getStart() {
59         return _start;
60     }
61
62     /**
63      * Returns the end of the date span.
64      *
65      * @return end of the span.
66      */

67     public long getEnd() {
68         return _end;
69     }
70
71     /**
72      * Returns the start of the date span as a <code>Date</code>.
73      *
74      * @return start of the span.
75      */

76     public Date JavaDoc getStartAsDate() {
77         return new Date JavaDoc(getStart());
78     }
79
80     /**
81      * Returns the end of the date span as a <code>Date</code>.
82      *
83      * @return end of the span.
84      */

85     public Date JavaDoc getEndAsDate() {
86         return new Date JavaDoc(getEnd());
87     }
88
89     /**
90      * Returns true if this <code>DateSpan</code> contains the specified
91      * <code>DateSpan</code>.
92      *
93      * @param span Date to check
94      * @return true if this DateSpan contains <code>span</code>.
95      */

96     public boolean contains(DateSpan span) {
97         return (contains(span.getStart()) && contains(span.getEnd()));
98     }
99
100     /**
101      * Returns whether or not this <code>DateSpan</code> contains the specified
102      * time.
103      *
104      * @param time time check
105      * @return true if this DateSpan contains <code>time</code>.
106      */

107     public boolean contains(long time) {
108         return (time >= getStart() && time <= getEnd());
109     }
110
111     /**
112      * Returns whether or not this <code>DateSpan</code> contains the
113      * specified date span.
114      *
115      * @param start Start of time span
116      * @param end End of time
117      * @return true if this <code>DateSpan</code> contains the specified
118      * date span.
119      */

120     public boolean contains(long start, long end) {
121         return (start >= getStart() && end <= getEnd());
122     }
123
124     /**
125      * Returns true if the this <code>DateSpan</code> intersects with the
126      * specified time.
127      *
128      * @param start Start time
129      * @param end End time
130      * @return true if this <code>DateSpan</code> intersects with the specified
131      * time.
132      */

133     public boolean intersects(long start, long end) {
134         return (start <= getEnd() && end >= getStart());
135     }
136
137     /**
138      * Returns true if the this <code>DateSpan</code> intersects with the
139      * specified <code>DateSpan</code>.
140      *
141      * @param span DateSpan to compare to
142      * @return true if this <code>DateSpan</code> intersects with the specified
143      * time.
144      */

145     public boolean intersects(DateSpan span) {
146         return intersects(span.getStart(), span.getEnd());
147     }
148
149     /**
150      * Returns a new <code>DateSpan</code> that is the union of this
151      * <code>DateSpan</code> and <code>span</code>.
152      *
153      * @param span DateSpan to add
154      * @return union of this DateSpan and <code>span</code>
155      */

156     public DateSpan add(DateSpan span) {
157         return add(span.getStart(), span.getEnd());
158     }
159
160     /**
161      * Returns a new <code>DateSpan</code> that is the union of this
162      * <code>DateSpan</code> and the passed in span.
163      *
164      * @param start Start of region to add
165      * @param end End of region to end
166      * @return union of this DateSpan and <code>start</code>, <code>end</code>
167      */

168     public DateSpan add(long start, long end) {
169         return new DateSpan(Math.min(start, getStart()),
170                             Math.max(end, getEnd()));
171     }
172
173
174     public boolean equals(Object JavaDoc o) {
175         if (o == this) {
176             return true;
177         }
178         if (o instanceof DateSpan) {
179             DateSpan ds = (DateSpan)o;
180             return (_start == ds.getStart() && _end == ds.getEnd());
181         }
182         return false;
183     }
184
185     public int hashCode() {
186         int result = 17;
187         result = 37 * result + (int)(_start ^ (_start >>> 32));
188         result = 37 * result + (int)(_end ^ (_end >>> 32));
189         return result;
190     }
191
192     public String JavaDoc toString() {
193         return "DateSpan [" + getStartAsDate() + "-" + getEndAsDate() + "]";
194     }
195 }
196
Popular Tags