Explorar el Código

A few minor changes to the experimental parts of the domain layer support

peter_backlund hace 17 años
padre
commit
59e3c2b819

+ 5
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/shared/experimental/Entity.java Ver fichero

@@ -14,6 +14,11 @@ public interface Entity<T,ID> {
14 14
    */
15 15
   boolean sameIdentityAs(T other);
16 16
 
17
+  /**
18
+   * Entities have an identity.
19
+   *
20
+   * @return The identity of this entity.
21
+   */
17 22
   ID identity();
18 23
 
19 24
 }

+ 1
- 1
dddsample/src/main/java/se/citerus/dddsample/domain/shared/experimental/EntitySupport.java Ver fichero

@@ -3,7 +3,7 @@ package se.citerus.dddsample.domain.shared.experimental;
3 3
 import java.lang.reflect.Field;
4 4
 
5 5
 /**
6
- *
6
+ * Base class for entities.
7 7
  *
8 8
  */
9 9
 public abstract class EntitySupport<T extends Entity, ID> implements Entity<T, ID> {

+ 1
- 1
dddsample/src/main/java/se/citerus/dddsample/domain/shared/experimental/Identity.java Ver fichero

@@ -12,4 +12,4 @@ import java.lang.annotation.Target;
12 12
 @Retention(RetentionPolicy.RUNTIME)
13 13
 @Target(ElementType.FIELD)
14 14
 public @interface Identity {
15
-}
15
+}

+ 7
- 6
dddsample/src/main/java/se/citerus/dddsample/domain/shared/experimental/ValueObject.java Ver fichero

@@ -1,12 +1,10 @@
1 1
 package se.citerus.dddsample.domain.shared.experimental;
2 2
 
3
-import java.io.Serializable;
4
-
5 3
 /**
6 4
  * A value object.
7 5
  *
8 6
  */
9
-public interface ValueObject<T> extends Serializable {
7
+public interface ValueObject<T> {
10 8
 
11 9
   /**
12 10
    * Value objects compare by the values of their attributes, they don't have an identity.
@@ -16,8 +14,11 @@ public interface ValueObject<T> extends Serializable {
16 14
    */
17 15
   boolean sameValueAs(T other);
18 16
 
19
-
20
-  // Value objects can be safely copied:
21
-  // T copy();
17
+  /**
18
+   * Value objects can be freely copied.
19
+   *
20
+   * @return A safe, deep copy of this value object.
21
+   */
22
+  T copy();
22 23
 
23 24
 }

+ 51
- 33
dddsample/src/main/java/se/citerus/dddsample/domain/shared/experimental/ValueObjectSupport.java Ver fichero

@@ -2,41 +2,59 @@ package se.citerus.dddsample.domain.shared.experimental;
2 2
 
3 3
 import org.apache.commons.lang.builder.EqualsBuilder;
4 4
 import org.apache.commons.lang.builder.HashCodeBuilder;
5
-import se.citerus.dddsample.domain.model.ValueObject;
6
-
7
-public class ValueObjectSupport<T extends ValueObject> implements ValueObject<T> {
8
-
9
-    private transient int cachedHashCode = 0;
10
-
11
-    @Override
12
-    public final boolean sameValueAs(final T other) {
13
-        return other != null && EqualsBuilder.reflectionEquals(this, other, false);
5
+import se.citerus.dddsample.domain.shared.ValueObject;
6
+
7
+/**
8
+ * Base class for value objects.
9
+ *
10
+ * @param <T>
11
+ */
12
+public abstract class ValueObjectSupport<T extends ValueObject> implements ValueObject<T> {
13
+
14
+  private transient int cachedHashCode = 0;
15
+
16
+  /**
17
+   * @param other The other value object.
18
+   * @return True if all non-transient fields are equal.
19
+   */
20
+  @Override
21
+  public final boolean sameValueAs(final T other) {
22
+    return other != null && EqualsBuilder.reflectionEquals(this, other, false);
23
+  }
24
+
25
+  /**
26
+   * @return Hash code built from all non-transient fields.
27
+   */
28
+  @Override
29
+  public final int hashCode() {
30
+    // Using a local variable to ensure that we only do a single read
31
+    // of the cachedHashCode field, to avoid race conditions.
32
+    // It doesn't matter if several threads compute the hash code and overwrite
33
+    // each other, but it's important that we never return 0, which could happen
34
+    // with multiple reads of the cachedHashCode field.
35
+    //
36
+    // See java.lang.String.hashCode()
37
+    int h = cachedHashCode;
38
+    if (h == 0) {
39
+      // Lazy initialization of hash code.
40
+      // Value objects are immutable, so the hash code never changes.
41
+      h = HashCodeBuilder.reflectionHashCode(this, false);
42
+      cachedHashCode = h;
14 43
     }
15 44
 
16
-    @Override
17
-    public final int hashCode() {
18
-        // Using a local variable to ensure that we only do a single read
19
-        // of the cachedHashCode field, to avoid race conditions.
20
-        // It doesn't matter if several threads compute the hash code and overwrite
21
-        // each other, but it's important that we never return 0, which could happen
22
-        // with multiple field reads.
23
-        int h = cachedHashCode;
24
-        if (h == 0) {
25
-            // Lazy initialization of hash code.
26
-            // Value objects are immutable, so the hash code never changes.
27
-            h = HashCodeBuilder.reflectionHashCode(this, false);
28
-            cachedHashCode = h;
29
-        }
30
-
31
-        return h;
32
-    }
45
+    return h;
46
+  }
33 47
 
34
-    @Override
35
-    public final boolean equals(final Object o) {
36
-        if (this == o) return true;
37
-        if (o == null || getClass() != o.getClass()) return false;
48
+  /**
49
+   * @param o other object
50
+   * @return True if other object has the same value as this value object.
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;
38 56
 
39
-        return sameValueAs((T) o);
40
-    }
57
+    return sameValueAs((T) o);
58
+  }
41 59
 
42
-}
60
+}