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