1 package se.citerus.dddsample.domain.shared.experimental;
2
3 import org.apache.commons.lang.builder.EqualsBuilder;
4 import org.apache.commons.lang.builder.HashCodeBuilder;
5 import se.citerus.dddsample.domain.shared.ValueObject;
6
7
8
9
10
11
12 public abstract class ValueObjectSupport<T extends ValueObject> implements ValueObject<T> {
13
14 private transient int cachedHashCode = 0;
15
16
17
18
19
20 @Override
21 public final boolean sameValueAs(final T other) {
22 return other != null && EqualsBuilder.reflectionEquals(this, other, false);
23 }
24
25
26
27
28 @Override
29 public final int hashCode() {
30
31
32
33
34
35
36
37 int h = cachedHashCode;
38 if (h == 0) {
39
40
41 h = HashCodeBuilder.reflectionHashCode(this, false);
42 cachedHashCode = h;
43 }
44
45 return h;
46 }
47
48
49
50
51
52 @Override
53 public final boolean equals(final Object o) {
54 if (this == o) return true;
55 if (o == null || getClass() != o.getClass()) return false;
56
57 return sameValueAs((T) o);
58 }
59
60 }