1 49 50 package org.jfree.chart.axis; 51 52 import java.io.Serializable ; 53 import java.text.DateFormat ; 54 import java.util.Calendar ; 55 import java.util.Date ; 56 57 import org.jfree.util.ObjectUtilities; 58 59 64 public class DateTickUnit extends TickUnit implements Serializable { 65 66 67 private static final long serialVersionUID = -7289292157229621901L; 68 69 70 public static final int YEAR = 0; 71 72 73 public static final int MONTH = 1; 74 75 76 public static final int DAY = 2; 77 78 79 public static final int HOUR = 3; 80 81 82 public static final int MINUTE = 4; 83 84 85 public static final int SECOND = 5; 86 87 88 public static final int MILLISECOND = 6; 89 90 91 private int unit; 92 93 94 private int count; 95 96 97 private int rollUnit; 98 99 100 private int rollCount; 101 102 103 private DateFormat formatter; 104 105 112 public DateTickUnit(int unit, int count) { 113 this(unit, count, null); 114 } 115 116 125 public DateTickUnit(int unit, int count, DateFormat formatter) { 126 127 this(unit, count, unit, count, formatter); 128 129 } 130 131 140 public DateTickUnit(int unit, int count, int rollUnit, int rollCount, 141 DateFormat formatter) { 142 super(DateTickUnit.getMillisecondCount(unit, count)); 143 this.unit = unit; 144 this.count = count; 145 this.rollUnit = rollUnit; 146 this.rollCount = rollCount; 147 this.formatter = formatter; 148 if (formatter == null) { 149 this.formatter = DateFormat.getDateInstance(DateFormat.SHORT); 150 } 151 } 152 153 163 public int getUnit() { 164 return this.unit; 165 } 166 167 172 public int getCount() { 173 return this.count; 174 } 175 176 184 public int getRollUnit() { 185 return this.rollUnit; 186 } 187 188 193 public int getRollCount() { 194 return this.rollCount; 195 } 196 197 204 public String valueToString(double milliseconds) { 205 return this.formatter.format(new Date ((long) milliseconds)); 206 } 207 208 215 public String dateToString(Date date) { 216 return this.formatter.format(date); 217 } 218 219 226 public Date addToDate(Date base) { 227 228 Calendar calendar = Calendar.getInstance(); 229 calendar.setTime(base); 230 calendar.add(getCalendarField(this.unit), this.count); 231 return calendar.getTime(); 232 233 } 234 235 243 public Date rollDate(Date base) { 244 Calendar calendar = Calendar.getInstance(); 245 calendar.setTime(base); 246 calendar.add(getCalendarField(this.rollUnit), this.rollCount); 247 return calendar.getTime(); 248 } 249 250 256 public int getCalendarField() { 257 return getCalendarField(this.unit); 258 } 259 260 270 private int getCalendarField(int tickUnit) { 271 272 switch (tickUnit) { 273 case (YEAR): 274 return Calendar.YEAR; 275 case (MONTH): 276 return Calendar.MONTH; 277 case (DAY): 278 return Calendar.DATE; 279 case (HOUR): 280 return Calendar.HOUR_OF_DAY; 281 case (MINUTE): 282 return Calendar.MINUTE; 283 case (SECOND): 284 return Calendar.SECOND; 285 case (MILLISECOND): 286 return Calendar.MILLISECOND; 287 default: 288 return Calendar.MILLISECOND; 289 } 290 291 } 292 293 305 private static long getMillisecondCount(int unit, int count) { 306 307 switch (unit) { 308 case (YEAR): 309 return (365L * 24L * 60L * 60L * 1000L) * count; 310 case (MONTH): 311 return (31L * 24L * 60L * 60L * 1000L) * count; 312 case (DAY): 313 return (24L * 60L * 60L * 1000L) * count; 314 case (HOUR): 315 return (60L * 60L * 1000L) * count; 316 case (MINUTE): 317 return (60L * 1000L) * count; 318 case (SECOND): 319 return 1000L * count; 320 case (MILLISECOND): 321 return count; 322 default: 323 throw new IllegalArgumentException ( 324 "DateTickUnit.getMillisecondCount() : unit must " 325 + "be one of the constants YEAR, MONTH, DAY, HOUR, MINUTE, " 326 + "SECOND or MILLISECOND defined in the DateTickUnit " 327 + "class. Do *not* use the constants defined in " 328 + "java.util.Calendar." 329 ); 330 } 331 332 } 333 334 341 public boolean equals(Object obj) { 342 if (obj == this) { 343 return true; 344 } 345 if (!(obj instanceof DateTickUnit)) { 346 return false; 347 } 348 if (!super.equals(obj)) { 349 return false; 350 } 351 DateTickUnit that = (DateTickUnit) obj; 352 if (this.unit != that.unit) { 353 return false; 354 } 355 if (this.count != that.count) { 356 return false; 357 } 358 if (!ObjectUtilities.equal(this.formatter, that.formatter)) { 359 return false; 360 } 361 return true; 362 } 363 364 369 public int hashCode() { 370 int result = 19; 371 result = 37 * result + this.unit; 372 result = 37 * result + this.count; 373 result = 37 * result + this.formatter.hashCode(); 374 return result; 375 } 376 377 } 378 | Popular Tags |