EntityEquals.java
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package fi.insomnia.bortal.model;
import java.util.Date;
import java.util.Random;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
@MappedSuperclass
public abstract class EntityEquals {
@Override
public final String toString() {
return new StringBuilder(this.getClass().getCanonicalName()).append("[").append(getId()).append("]").toString();
}
@Transient
private Integer rndid;
protected abstract Object getId();
@Override
public boolean equals(Object o) {
boolean ret = false;
if (o != null && o instanceof EntityEquals && this.getClass().getCanonicalName().equals(o.getClass().getCanonicalName())) {
EntityEquals oobj = (EntityEquals) o;
if (getId() == null) {
ret = (getRndid().equals(oobj.rndid));
} else {
ret = getId().equals(oobj.getId());
}
}
return ret;
}
private Integer getRndid() {
if (rndid == null) {
Random rng = new Random(new Date().getTime());
rndid = Integer.valueOf(rng.nextInt());
}
return rndid;
}
@Override
public final int hashCode() {
return ((rndid != null || getId() == null) ? getRndid() : getId().hashCode());
}
}