|
|
@@ -4,15 +4,24 @@ import org.apache.commons.lang.builder.EqualsBuilder;
|
|
4
|
4
|
import org.apache.commons.lang.builder.HashCodeBuilder;
|
|
5
|
5
|
|
|
6
|
6
|
/**
|
|
7
|
|
- * Base class for value objects.
|
|
|
7
|
+ * Supporting base class for value objects.
|
|
|
8
|
+ *
|
|
|
9
|
+ * While the ValueObject interface makes the pattern properties explicit,
|
|
|
10
|
+ * this class is less general and is suited for this particular application.
|
|
|
11
|
+ * </p>
|
|
|
12
|
+ * For example, the private id field is meant for autogenerated, surrogate primary keys.
|
|
|
13
|
+ * Also, you may want more flexibility in selecting significant fields for comparision,
|
|
|
14
|
+ * or you may need to be able to calculate equals millions of times every second,
|
|
|
15
|
+ * in which case reflection might not be fast enough.
|
|
8
|
16
|
*
|
|
9
|
17
|
* @param <T>
|
|
10
|
18
|
*/
|
|
11
|
19
|
public abstract class ValueObjectSupport<T extends ValueObject> implements ValueObject<T> {
|
|
12
|
20
|
|
|
13
|
|
- private transient int cachedHashCode = 0;
|
|
14
|
21
|
@SuppressWarnings("UnusedDeclaration")
|
|
15
|
22
|
private final Long id = null;
|
|
|
23
|
+ private transient int cachedHashCode = 0;
|
|
|
24
|
+ private final static String[] EXCLUDED_FIELDS = {"id", "cachedHashCode"};
|
|
16
|
25
|
|
|
17
|
26
|
/**
|
|
18
|
27
|
* @param other The other value object.
|
|
|
@@ -20,7 +29,7 @@ public abstract class ValueObjectSupport<T extends ValueObject> implements Value
|
|
20
|
29
|
*/
|
|
21
|
30
|
@Override
|
|
22
|
31
|
public final boolean sameValueAs(final T other) {
|
|
23
|
|
- return other != null && EqualsBuilder.reflectionEquals(this, other, false);
|
|
|
32
|
+ return other != null && EqualsBuilder.reflectionEquals(this, other, EXCLUDED_FIELDS);
|
|
24
|
33
|
}
|
|
25
|
34
|
|
|
26
|
35
|
/**
|