1 7 package org.jdesktop.swing.calendar; 8 9 import java.util.Date ; 10 11 20 public class DateSpan { 21 private long _start; 22 private long _end; 23 24 32 public DateSpan(long start, long end) { 33 _start = start; 34 _end = end; 35 if (_start > _end) { 36 throw new IllegalArgumentException ( 37 "Start date must be before end date"); 38 } 39 } 40 41 49 public DateSpan(Date start, Date end) { 50 this(start.getTime(), end.getTime()); 51 } 52 53 58 public long getStart() { 59 return _start; 60 } 61 62 67 public long getEnd() { 68 return _end; 69 } 70 71 76 public Date getStartAsDate() { 77 return new Date (getStart()); 78 } 79 80 85 public Date getEndAsDate() { 86 return new Date (getEnd()); 87 } 88 89 96 public boolean contains(DateSpan span) { 97 return (contains(span.getStart()) && contains(span.getEnd())); 98 } 99 100 107 public boolean contains(long time) { 108 return (time >= getStart() && time <= getEnd()); 109 } 110 111 120 public boolean contains(long start, long end) { 121 return (start >= getStart() && end <= getEnd()); 122 } 123 124 133 public boolean intersects(long start, long end) { 134 return (start <= getEnd() && end >= getStart()); 135 } 136 137 145 public boolean intersects(DateSpan span) { 146 return intersects(span.getStart(), span.getEnd()); 147 } 148 149 156 public DateSpan add(DateSpan span) { 157 return add(span.getStart(), span.getEnd()); 158 } 159 160 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 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 toString() { 193 return "DateSpan [" + getStartAsDate() + "-" + getEndAsDate() + "]"; 194 } 195 } 196 | Popular Tags |