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 MonthAdaptor implements EquatableComparable { 9 10 private Date _date; 11 private static SimpleDateFormat formatter = new SimpleDateFormat("MM/yyyy"); 12 13 14 public EquatableComparable earliest() { 15 Calendar calendar = asCalendar(); 16 calendar.add(Calendar.YEAR, -1); 17 return new MonthAdaptor(calendar.getTime()); 18 } 19 20 public EquatableComparable previous() { 21 Calendar calendar = new GregorianCalendar(_date.getYear()+1900,_date.getMonth(), _date.getDate()); 22 calendar.add(Calendar.MONTH, -1); 23 return new MonthAdaptor(calendar.getTime()); 24 } 25 26 private Calendar asCalendar() { 27 return new GregorianCalendar(_date.getYear()+1900,_date.getMonth(), _date.getDate()); 28 } 29 30 public String toString() { 31 if (_date == null) return ""; 32 return formatter.format(_date); 33 } 34 35 public int hashCode() { 36 if (_date == null) { 37 return 0; 38 } 39 return _date.getYear() ^ _date.getMonth(); 40 } 41 42 public MonthAdaptor(Date date) { 43 _date = date; 44 } 45 46 public boolean isNull() { 47 return _date == null; 48 } 49 50 public boolean equals(Object other) { 51 if (other == null) return false; 52 if (_date == null) return false; 53 if (!(other instanceof EquatableComparable)) 54 return false; 55 MonthAdaptor otherDate = (MonthAdaptor) other; 56 if (otherDate._date == null) return false; 57 if (_date.getYear() == otherDate._date.getYear() 58 && _date.getMonth() == otherDate._date.getMonth()) { 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 MonthAdaptor otherDate = (MonthAdaptor) 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 return 0; 76 } 77 78 }