1 package org.oddjob.schedules; 2 3 import java.io.Serializable ; 4 import java.text.SimpleDateFormat ; 5 import java.util.Date ; 6 7 12 13 public class Interval implements Serializable { 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 fromDate; 21 private final Date toDate; 22 23 24 30 public Interval(Date from, Date to) { 31 this(from.getTime(), to.getTime()); 32 } 33 34 35 41 public Interval(long fromTime, long toTime) { 42 fromDate = new Date (fromTime); 43 toDate = new Date (toTime); 44 if (toTime < fromTime) { 45 throw new IllegalStateException ("An interval can not have a to [" + 46 toDate + "] before the from [" + fromDate + "]"); 47 } 48 } 49 50 55 public Interval(Interval other) { 56 this(other.fromDate.getTime(), other.toDate.getTime()); 57 } 58 59 64 public Date getFromDate() { 65 return fromDate; 66 } 67 68 73 public Date getToDate() { 74 return toDate; 75 } 76 77 82 public int hashCode() { 83 return fromDate.hashCode() + toDate.hashCode(); 84 } 85 86 94 public boolean equals(Object 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 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 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 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 162 public boolean isPoint() { 163 return fromDate.equals(toDate); 164 } 165 166 169 public String toString() { 170 String fromString; 171 if (fromDate.getTime() == ALWAYS) { 172 fromString = "Always"; 173 } else { 174 SimpleDateFormat format = new SimpleDateFormat ("dd-MMM-yy HH:mm:ss:SSS"); 175 if (fromDate.getTime() % 1000 == 0) { 176 format = new SimpleDateFormat ("dd-MMM-yy HH:mm:ss"); 178 } 179 fromString = format.format(fromDate); 180 } 181 182 String toString; 183 if (toDate.getTime() == NEVER_AGAIN) { 184 toString = "Never Again"; 185 } 186 else { 187 SimpleDateFormat format = new SimpleDateFormat ("dd-MMM-yy HH:mm:ss:SSS"); 188 if (toDate.getTime() + 1 % 1000 == 0) { 189 format = new SimpleDateFormat ("dd-MMM-yy HH:mm:ss"); 191 } 192 toString = format.format(toDate); 193 } 194 return fromString + " to " + toString; 195 } 196 } 197 | Popular Tags |