1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.model;
20
21 import org.apache.commons.lang.builder.CompareToBuilder;
22 import org.apache.commons.lang.builder.EqualsBuilder;
23 import org.apache.commons.lang.builder.HashCodeBuilder;
24
25 import java.io.Serializable;
26 import java.util.Comparator;
27 import java.util.Date;
28
29
30
31
32
33
34
35
36
37
38
39
40 public abstract class AbstractEntity implements Entity {
41
42
43
44
45 private static final long serialVersionUID = 1L;
46
47 public static final Comparator<Entity> ID_COMPARATOR = new IdComparator();
48
49 public static final Comparator<AbstractEntity> CREATE_DATE_COMPARATOR = new CreateDateComparator();
50
51 public static final Comparator<AbstractEntity> LAST_MODIFIED_DATE_COMPARATOR = new LastModifiedDateComparator();
52
53
54
55
56 private Integer id;
57
58
59
60
61 private Date createDate = new Date();
62
63
64
65
66 private Date lastModifiedDate = new Date();
67
68
69
70
71 public AbstractEntity() {
72 }
73
74 public Integer getId() {
75 return id;
76 }
77
78 public void setId(Integer id) {
79 this.id = id;
80 }
81
82
83
84
85 public Date getCreateDate() {
86 if (null == createDate)
87 createDate = new Date();
88 return new Date(createDate.getTime());
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public void setCreateDate(Date dateTime) {
108 if (null == dateTime)
109 return;
110 this.createDate = new Date(dateTime.getTime());
111 }
112
113
114
115
116
117 public Date getLastModifiedDate() {
118 if (null == this.lastModifiedDate)
119 this.lastModifiedDate = new Date();
120 return new Date(lastModifiedDate.getTime());
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public void setLastModifiedDate(Date dateTime) {
137 if (null == dateTime)
138 return;
139 this.lastModifiedDate = new Date(dateTime.getTime());
140 }
141
142
143
144
145
146
147 public boolean isNew() {
148 return (this.getId() == null);
149 }
150
151 @Override
152 public Object clone() throws CloneNotSupportedException {
153 return super.clone();
154 }
155
156
157
158
159 protected static class IdComparator implements Comparator<Entity>, Serializable {
160
161
162
163
164 private static final long serialVersionUID = 1L;
165
166 public int compare(Entity href="../../../org/itracker/model/Entity.html#Entity">Entity a, Entity b) {
167 return new CompareToBuilder().append(a.getId(), b.getId())
168 .toComparison();
169 }
170
171 }
172
173 protected static class CreateDateComparator implements
174 Comparator<AbstractEntity>, Serializable {
175
176
177
178
179 private static final long serialVersionUID = 1L;
180
181 public int compare(AbstractEntity./../../org/itracker/model/AbstractEntity.html#AbstractEntity">AbstractEntity a, AbstractEntity b) {
182 return new CompareToBuilder().append(a.getCreateDate(),
183 b.getCreateDate()).toComparison();
184 }
185
186 }
187
188
189
190
191 protected static class LastModifiedDateComparator implements
192 Comparator<AbstractEntity>, Serializable {
193
194
195
196
197 private static final long serialVersionUID = 1L;
198
199 public int compare(AbstractEntity./../../org/itracker/model/AbstractEntity.html#AbstractEntity">AbstractEntity a, AbstractEntity b) {
200 return new CompareToBuilder().append(a.getLastModifiedDate(),
201 b.getLastModifiedDate()).toComparison();
202 }
203
204 }
205
206 @Override
207 public final boolean equals(Object obj) {
208
209 if (this == obj) {
210 return true;
211 }
212 if (isNew() || null == obj) {
213 return false;
214 }
215
216 if (getClass().equals(obj.getClass())) {
217 Entityref="../../../org/itracker/model/Entity.html#Entity">Entity o = (Entity) obj;
218 return new EqualsBuilder()
219 .append(getId(), o.getId()).isEquals();
220
221 }
222
223 return false;
224
225 }
226
227 public final int compareTo(Entity o) {
228 if (this.equals(o)) {
229 return 0;
230 }
231 return new CompareToBuilder().append(getClass(), o.getClass(), AbstractEntity.CLASS_COMPARATOR).append(
232 getId(), o.getId()).toComparison();
233 }
234
235 @Override
236 public final int hashCode() {
237 return new HashCodeBuilder().append(getClass()).append(getId()).toHashCode();
238
239 }
240
241 private static final Comparator<Class<?>> CLASS_COMPARATOR = new Comparator<Class<?>>() {
242 public int compare(Class<?> o1, Class<?> o2) {
243 return new CompareToBuilder().append(o1.getSimpleName(), o2.getSimpleName()).append(o1.hashCode(), hashCode()).toComparison();
244 }
245 };
246
247 }