1 46 47 package org.jfree.data.time; 48 49 import java.io.Serializable ; 50 import java.util.Date ; 51 52 59 public class SimpleTimePeriod implements TimePeriod, Comparable , Serializable { 60 61 62 private static final long serialVersionUID = 8684672361131829554L; 63 64 65 private Date start; 66 67 68 private Date end; 69 70 76 public SimpleTimePeriod(long start, long end) { 77 this(new Date (start), new Date (end)); 78 } 79 80 86 public SimpleTimePeriod(Date start, Date end) { 87 if (start.getTime() > end.getTime()) { 88 throw new IllegalArgumentException ("Requires end >= start."); 89 } 90 this.start = start; 91 this.end = end; 92 } 93 94 99 public Date getStart() { 100 return this.start; 101 } 102 103 108 public Date getEnd() { 109 return this.end; 110 } 111 112 121 public boolean equals(Object obj) { 122 if (obj == this) { 123 return true; 124 } 125 if (!(obj instanceof TimePeriod)) { 126 return false; 127 } 128 TimePeriod that = (TimePeriod) obj; 129 if (!this.start.equals(that.getStart())) { 130 return false; 131 } 132 if (!this.end.equals(that.getEnd())) { 133 return false; 134 } 135 return true; 136 } 137 138 149 public int compareTo(Object obj) { 150 TimePeriod that = (TimePeriod) obj; 151 long t0 = getStart().getTime(); 152 long t1 = getEnd().getTime(); 153 long m0 = t0 + (t1 - t0) / 2L; 154 long t2 = that.getStart().getTime(); 155 long t3 = that.getEnd().getTime(); 156 long m1 = t2 + (t3 - t2) / 2L; 157 if (m0 < m1) { 158 return -1; 159 } 160 else if (m0 > m1) { 161 return 1; 162 } 163 else { 164 if (t0 < t2) { 165 return -1; 166 } 167 else if (t0 > t2) { 168 return 1; 169 } 170 else { 171 if (t1 < t3) { 172 return -1; 173 } 174 else if (t1 > t3) { 175 return 1; 176 } 177 else { 178 return 0; 179 } 180 } 181 } 182 } 183 184 193 public int hashCode() { 194 int result = 17; 195 result = 37 * result + this.start.hashCode(); 196 result = 37 * result + this.end.hashCode(); 197 return result; 198 } 199 200 } 201 | Popular Tags |