Просмотр исходного кода

Exclude two names fields in value object comparison, improved javadoc for support base classes.

peter_backlund 17 лет назад
Родитель
Сommit
88c529e142

+ 6
- 1
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/shared/experimental/EntitySupport.java Просмотреть файл

@@ -1,7 +1,12 @@
1 1
 package se.citerus.dddsample.tracking.core.domain.shared.experimental;
2 2
 
3 3
 /**
4
- * Base class for entities.
4
+ * Supporting base class for entities.
5
+ *
6
+ * While the Entity interface makes the pattern properties explicit,
7
+ * this class is less general and is suited for this particular application.
8
+ * </p>
9
+ * For example, the private id field is meant for autogenerated, surrogate primary keys.
5 10
  */
6 11
 public abstract class EntitySupport<T extends Entity, ID> implements Entity<T, ID> {
7 12
 

+ 12
- 3
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/shared/experimental/ValueObjectSupport.java Просмотреть файл

@@ -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
   /**