001: /*
002: *
003: * $Id: DateSpan.java,v 1.1 2007/08/15 23:28:24 suricate Exp $
004: *
005: * This code comes from the dormant jdnc project at https://jdnc.dev.java.net/. It is licensed with the LGPL
006: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
007: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
008: */
009: package org.jdesktop.swing.calendar;
010:
011: import java.util.Date;
012:
013: import com.projity.pm.time.HasStartAndEnd;
014:
015: /**
016: * An immutable representation of a time range. The time range is
017: * internally represented as two longs. The methods that take and return
018: * <code>Date</code>s create the <code>Date</code>s as needed, so that
019: * if you modify returned <code>Date</code>s you will <b>not</b> effect
020: * the <code>DateSpan</code>. The end points are inclusive.
021: *
022: * @version $Revision: 1.1 $
023: */
024: public class DateSpan implements HasStartAndEnd {
025: private long _start;
026: private long _end;
027:
028: /**
029: * Creates a <code>DateSpan</code> between the two end points.
030: *
031: * @param start Beginning date
032: * @param end Ending date
033: * @throws IllegalArgumentException if <code>start</code> is after
034: * <code>end</code>
035: */
036: public DateSpan(long start, long end) {
037: _start = start;
038: _end = end;
039: if (_start > _end) {
040: throw new IllegalArgumentException(
041: "Start date must be before end date");
042: }
043: }
044:
045: /**
046: * Creates a <code>DateSpan</code> between the two end points. This
047: * is a conveniance constructor that is equivalent to
048: * <code>new Date(start.getTime(), end.getTime());</code>.
049: *
050: * @param start Beginning date
051: * @param end Ending date
052: */
053: public DateSpan(Date start, Date end) {
054: this (start.getTime(), end.getTime());
055: }
056:
057: /**
058: * Returns the start of the date span.
059: *
060: * @return start of the span.
061: */
062: public long getStart() {
063: return _start;
064: }
065:
066: /**
067: * Returns the end of the date span.
068: *
069: * @return end of the span.
070: */
071: public long getEnd() {
072: return _end;
073: }
074:
075: /**
076: * Returns the start of the date span as a <code>Date</code>.
077: *
078: * @return start of the span.
079: */
080: public Date getStartAsDate() {
081: return new Date(getStart());
082: }
083:
084: /**
085: * Returns the end of the date span as a <code>Date</code>.
086: *
087: * @return end of the span.
088: */
089: public Date getEndAsDate() {
090: return new Date(getEnd());
091: }
092:
093: /**
094: * Returns true if this <code>DateSpan</code> contains the specified
095: * <code>DateSpan</code>.
096: *
097: * @param span Date to check
098: * @return true if this DateSpan contains <code>span</code>.
099: */
100: public boolean contains(DateSpan span) {
101: return (contains(span.getStart()) && contains(span.getEnd()));
102: }
103:
104: /**
105: * Returns whether or not this <code>DateSpan</code> contains the specified
106: * time.
107: *
108: * @param time time check
109: * @return true if this DateSpan contains <code>time</code>.
110: */
111: public boolean contains(long time) {
112: return (time >= getStart() && time <= getEnd());
113: }
114:
115: /**
116: * Returns whether or not this <code>DateSpan</code> contains the
117: * specified date span.
118: *
119: * @param start Start of time span
120: * @param end End of time
121: * @return true if this <code>DateSpan</code> contains the specified
122: * date span.
123: */
124: public boolean contains(long start, long end) {
125: return (start >= getStart() && end <= getEnd());
126: }
127:
128: /**
129: * Returns true if the this <code>DateSpan</code> intersects with the
130: * specified time.
131: *
132: * @param start Start time
133: * @param end End time
134: * @return true if this <code>DateSpan</code> intersects with the specified
135: * time.
136: */
137: public boolean intersects(long start, long end) {
138: return (start <= getEnd() && end >= getStart());
139: }
140:
141: /**
142: * Returns true if the this <code>DateSpan</code> intersects with the
143: * specified <code>DateSpan</code>.
144: *
145: * @param span DateSpan to compare to
146: * @return true if this <code>DateSpan</code> intersects with the specified
147: * time.
148: */
149: public boolean intersects(DateSpan span) {
150: return intersects(span.getStart(), span.getEnd());
151: }
152:
153: /**
154: * Returns a new <code>DateSpan</code> that is the union of this
155: * <code>DateSpan</code> and <code>span</code>.
156: *
157: * @param span DateSpan to add
158: * @return union of this DateSpan and <code>span</code>
159: */
160: public DateSpan add(DateSpan span) {
161: return add(span.getStart(), span.getEnd());
162: }
163:
164: /**
165: * Returns a new <code>DateSpan</code> that is the union of this
166: * <code>DateSpan</code> and the passed in span.
167: *
168: * @param start Start of region to add
169: * @param end End of region to end
170: * @return union of this DateSpan and <code>start</code>, <code>end</code>
171: */
172: public DateSpan add(long start, long end) {
173: return new DateSpan(Math.min(start, getStart()), Math.max(end,
174: getEnd()));
175: }
176:
177: public boolean equals(Object o) {
178: if (o == this ) {
179: return true;
180: }
181: if (o instanceof DateSpan) {
182: DateSpan ds = (DateSpan) o;
183: return (_start == ds.getStart() && _end == ds.getEnd());
184: }
185: return false;
186: }
187:
188: public int hashCode() {
189: int result = 17;
190: result = 37 * result + (int) (_start ^ (_start >>> 32));
191: result = 37 * result + (int) (_end ^ (_end >>> 32));
192: return result;
193: }
194:
195: public String toString() {
196: return "DateSpan [" + getStartAsDate() + "-" + getEndAsDate()
197: + "]";
198: }
199: }
|