View Javadoc
1   package ejava.examples.txhotel.bo;
2   
3   import java.io.Serializable;
4   import java.util.Calendar;
5   import java.util.Date;
6   import java.util.GregorianCalendar;
7   
8   @SuppressWarnings("serial")
9   public class Reservation implements Serializable {
10      private long id;
11      private long version;
12      private String confirmation;
13      private Date startDate;
14      private Date endDate;
15      private Person person;
16      
17      public Reservation() {}
18      public Reservation(
19              long id, long version, String confirmation, Person person,
20              Date startDate, Date endDate) {
21          setId(id);
22          setVersion(version);
23          setConfirmation(confirmation);
24          setPerson(person);        
25          setStartDate(startDate);
26          setEndDate(endDate);
27      }
28      
29      public long getId() {
30          return id;
31      }
32      private void setId(long id) {
33          this.id = id;
34      }
35      public String getConfirmation() {
36          return confirmation;
37      }
38      public void setConfirmation(String confirmation) {
39          this.confirmation = confirmation;
40      }
41      public Person getPerson() {
42          return person;
43      }
44      public void setPerson(Person person) {
45          this.person = person;
46      }
47      public long getVersion() {
48          return version;
49      }
50      public void setVersion(long version) {
51          this.version = version;
52      }
53      public Date getEndDate() {
54          return endDate;
55      }
56      public void setEndDate(Date endDate) {
57          this.endDate = endDate;
58      }
59      public Date getStartDate() {
60          return startDate;
61      }
62      public void setStartDate(Date startDate) {
63          this.startDate = startDate;
64      }
65      @Override
66  	public boolean equals(Object obj) {
67      	try {
68      		if (this==obj) return true;
69      		Reservation rhs = (Reservation)obj;
70      		return person.equals(rhs.person) &&
71      				sameDate(startDate, rhs.startDate) &&
72      				sameDate(startDate, rhs.startDate) &&
73      				endDate.getTime()==rhs.endDate.getTime();
74      	} catch (Exception ex) {
75      		return false;
76      	}
77  	}
78      private static boolean sameDate(Date lhsDate, Date rhsDate) {
79      	if (lhsDate==null && rhsDate==null) { 
80      		return true;
81      	}
82      	else if (lhsDate==null || rhsDate==null) {
83      		return false;
84      	}
85      	
86  		Calendar lhs = new GregorianCalendar();
87  		lhs.setTime(lhsDate);
88  		Calendar rhs = new GregorianCalendar();
89  		rhs.setTime(rhsDate);
90  		return lhs.get(Calendar.YEAR)==rhs.get(Calendar.YEAR) &&
91  				lhs.get(Calendar.DAY_OF_YEAR)==rhs.get(Calendar.DAY_OF_YEAR);
92      }
93      
94      
95  	@Override
96  	public int hashCode() {
97  		return person.hashCode()+startDate.hashCode()+endDate.hashCode();
98  	}
99      
100 	public String toString() {
101         StringBuilder text = new StringBuilder();
102         text.append("id=" + id);
103         text.append(", version=" + version);
104         text.append(", conf#" + confirmation);
105         text.append(", person={" + person);
106         text.append("}, start=" + startDate);
107         text.append(", end=" + endDate);
108         return text.toString();
109     }
110 }