1 package org.paneris.jammyjoes.mvp; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 import java.util.GregorianCalendar; 7 8 public class DayAdaptor implements EquatableComparable { 9 10 private Date _date; 11 private static SimpleDateFormat formatter = new SimpleDateFormat("dd"); 12 13 public EquatableComparable earliest() { 14 Calendar calendar = asCalendar(); 15 calendar.add(Calendar.MONTH, -1); 16 return new DayAdaptor(calendar.getTime()); 17 } 18 19 public EquatableComparable previous() { 20 Calendar calendar = asCalendar(); 21 calendar.add(Calendar.DATE, -1); 22 return new DayAdaptor(calendar.getTime()); 23 } 24 25 private Calendar asCalendar() { 26 return new GregorianCalendar(_date.getYear()+1900,_date.getMonth(), _date.getDate()); 27 } 28 29 public String toString() { 30 if (_date == null) return ""; 31 return formatter.format(_date); 32 } 33 34 public int hashCode() { 35 if (_date == null) { 36 return 0; 37 } 38 return _date.getYear() ^ _date.getMonth( )^ _date.getDate(); 39 } 40 41 public boolean isNull() { 42 return _date == null; 43 } 44 45 public DayAdaptor(Date date) { 46 _date = date; 47 } 48 49 public boolean equals(Object other) { 50 if (other == null) return false; 51 if (_date == null) return false; 52 if (!(other instanceof EquatableComparable)) 53 return false; 54 DayAdaptor otherDate = (DayAdaptor) other; 55 if (otherDate._date == null) return false; 56 if (_date.getYear() == otherDate._date.getYear() 57 && _date.getMonth() == otherDate._date.getMonth() 58 && _date.getDate() == otherDate._date.getDate()) { 59 return true; 60 } 61 return false; 62 } 63 64 public int compareTo(Object other) { 65 if (other == null) return 1; 66 if (_date == null) return -1; 67 if (!(other instanceof EquatableComparable)) 68 return 1; 69 DayAdaptor otherDate = (DayAdaptor) other; 70 if (otherDate._date == null) return 1; 71 if (_date.getYear() > otherDate._date.getYear()) return 1; 72 if (_date.getYear() < otherDate._date.getYear()) return -1; 73 if (_date.getMonth() > otherDate._date.getMonth()) return 1; 74 if (_date.getMonth() < otherDate._date.getMonth()) return -1; 75 if (_date.getDate() > otherDate._date.getDate()) return 1; 76 if (_date.getDate() < otherDate._date.getDate()) return -1; 77 return 0; 78 } 79 80 }