浏览代码

Encapsulated fields in pattern support classes to avoid name clashes (using "_primaryKey" instead of "id"). Fixed hbm files to reflect change.

Removed unused DomanObjectUtils class.
peter_backlund 17 年前
父节点
当前提交
e4a7fe2fbe

+ 1
- 2
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/model/handling/HandlingEvent.java 查看文件

7
 import se.citerus.dddsample.tracking.core.domain.model.location.Location;
7
 import se.citerus.dddsample.tracking.core.domain.model.location.Location;
8
 import se.citerus.dddsample.tracking.core.domain.model.shared.HandlingActivity;
8
 import se.citerus.dddsample.tracking.core.domain.model.shared.HandlingActivity;
9
 import se.citerus.dddsample.tracking.core.domain.model.voyage.Voyage;
9
 import se.citerus.dddsample.tracking.core.domain.model.voyage.Voyage;
10
-import se.citerus.dddsample.tracking.core.domain.patterns.DomainObjectUtils;
11
 import se.citerus.dddsample.tracking.core.domain.patterns.domainevent.DomainEvent;
10
 import se.citerus.dddsample.tracking.core.domain.patterns.domainevent.DomainEvent;
12
 import se.citerus.dddsample.tracking.core.domain.patterns.valueobject.ValueObject;
11
 import se.citerus.dddsample.tracking.core.domain.patterns.valueobject.ValueObject;
13
 
12
 
159
   }
158
   }
160
 
159
 
161
   public Voyage voyage() {
160
   public Voyage voyage() {
162
-    return DomainObjectUtils.nullSafe(activity.voyage(), Voyage.NONE);
161
+    return activity.voyage() != null ? activity.voyage() : Voyage.NONE;
163
   }
162
   }
164
 
163
 
165
   public Date completionTime() {
164
   public Date completionTime() {

+ 1
- 3
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/model/voyage/Voyage.java 查看文件

17
   private Schedule schedule;
17
   private Schedule schedule;
18
 
18
 
19
   // Null object pattern
19
   // Null object pattern
20
-  public static final Voyage NONE = new Voyage(
21
-    new VoyageNumber(""), Schedule.EMPTY
22
-  );
20
+  public static final Voyage NONE = new Voyage(new VoyageNumber(""), Schedule.EMPTY);
23
 
21
 
24
   public Voyage(final VoyageNumber voyageNumber, final Schedule schedule) {
22
   public Voyage(final VoyageNumber voyageNumber, final Schedule schedule) {
25
     Validate.notNull(voyageNumber, "Voyage number is required");
23
     Validate.notNull(voyageNumber, "Voyage number is required");

+ 0
- 29
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/patterns/DomainObjectUtils.java 查看文件

1
-package se.citerus.dddsample.tracking.core.domain.patterns;
2
-
3
-/**
4
- * Utility code for domain classes.
5
- */
6
-public class DomainObjectUtils {
7
-
8
-  /**
9
-   * @param actual actual value
10
-   * @param safe   a null-safe value
11
-   * @param <T>    type
12
-   * @return actual value, if it's not null, or safe value if the actual value is null.
13
-   */
14
-  public static <T> T nullSafe(T actual, T safe) {
15
-    return actual == null ? safe : actual;
16
-  }
17
-
18
-  // TODO wrappers for some of the commons-lang code:
19
-  //
20
-  // EqualsBuilder that uses sameIdentity/sameValue,
21
-  // better validation (varargs etc) 
22
-
23
-  /**
24
-   * Prevent instantiation.
25
-   */
26
-  private DomainObjectUtils() {
27
-  }
28
-
29
-}

+ 3
- 4
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/patterns/entity/EntitySupport.java 查看文件

10
  */
10
  */
11
 public abstract class EntitySupport<T extends Entity, ID> implements Entity<T, ID> {
11
 public abstract class EntitySupport<T extends Entity, ID> implements Entity<T, ID> {
12
 
12
 
13
+  @SuppressWarnings("UnusedDeclaration")
14
+  private final Long _primaryKey = null;
15
+  
13
   @Override
16
   @Override
14
   public final boolean sameAs(final T other) {
17
   public final boolean sameAs(final T other) {
15
     return other != null && this.identity().equals(other.identity());
18
     return other != null && this.identity().equals(other.identity());
31
     return sameAs((T) o);
34
     return sameAs((T) o);
32
   }
35
   }
33
 
36
 
34
-  @SuppressWarnings("UnusedDeclaration")
35
-  // Surrogate primary key
36
-  private Long id;
37
-
38
 }
37
 }

+ 7
- 7
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/patterns/valueobject/ValueObjectSupport.java 查看文件

19
 public abstract class ValueObjectSupport<T extends ValueObject> implements ValueObject<T> {
19
 public abstract class ValueObjectSupport<T extends ValueObject> implements ValueObject<T> {
20
 
20
 
21
   @SuppressWarnings("UnusedDeclaration")
21
   @SuppressWarnings("UnusedDeclaration")
22
-  private final Long id = null;
23
-  private transient int cachedHashCode = 0;
24
-  private final static String[] EXCLUDED_FIELDS = {"id", "cachedHashCode"};
22
+  private final Long _primaryKey = null;
23
+  private transient int _cachedHashCode = 0;
24
+  private final static String[] EXCLUDED_FIELDS = {"_primaryKey", "_cachedHashCode"};
25
 
25
 
26
   /**
26
   /**
27
    * @param other The other value object.
27
    * @param other The other value object.
38
   @Override
38
   @Override
39
   public final int hashCode() {
39
   public final int hashCode() {
40
     // Using a local variable to ensure that we only do a single read
40
     // Using a local variable to ensure that we only do a single read
41
-    // of the cachedHashCode field, to avoid race conditions.
41
+    // of the _cachedHashCode field, to avoid race conditions.
42
     // It doesn't matter if several threads compute the hash code and overwrite
42
     // It doesn't matter if several threads compute the hash code and overwrite
43
     // each other, but it's important that we never return 0, which could happen
43
     // each other, but it's important that we never return 0, which could happen
44
-    // with multiple reads of the cachedHashCode field.
44
+    // with multiple reads of the _cachedHashCode field.
45
     //
45
     //
46
     // See java.lang.String.hashCode()
46
     // See java.lang.String.hashCode()
47
-    int h = cachedHashCode;
47
+    int h = _cachedHashCode;
48
     if (h == 0) {
48
     if (h == 0) {
49
       // Lazy initialization of hash code.
49
       // Lazy initialization of hash code.
50
       // Value objects are immutable, so the hash code never changes.
50
       // Value objects are immutable, so the hash code never changes.
51
       h = HashCodeBuilder.reflectionHashCode(this, false);
51
       h = HashCodeBuilder.reflectionHashCode(this, false);
52
-      cachedHashCode = h;
52
+      _cachedHashCode = h;
53
     }
53
     }
54
 
54
 
55
     return h;
55
     return h;

+ 1
- 1
dddsample/tracking/core/src/main/resources/se/citerus/dddsample/tracking/core/infrastructure/persistence/hibernate/Cargo.hbm.xml 查看文件

7
 <hibernate-mapping default-access="field">
7
 <hibernate-mapping default-access="field">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.cargo.Cargo" table="Cargo">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.cargo.Cargo" table="Cargo">
9
 
9
 
10
-    <id name="id" column="id">
10
+    <id name="_primaryKey" column="id">
11
       <generator class="org.hibernate.id.IdentityGenerator"/>
11
       <generator class="org.hibernate.id.IdentityGenerator"/>
12
     </id>
12
     </id>
13
 
13
 

+ 1
- 1
dddsample/tracking/core/src/main/resources/se/citerus/dddsample/tracking/core/infrastructure/persistence/hibernate/CarrierMovement.hbm.xml 查看文件

7
 <hibernate-mapping default-access="field">
7
 <hibernate-mapping default-access="field">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.voyage.CarrierMovement" table="CarrierMovement" mutable="false">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.voyage.CarrierMovement" table="CarrierMovement" mutable="false">
9
 
9
 
10
-    <id name="id" column="id">
10
+    <id name="_primaryKey" column="id">
11
       <generator class="org.hibernate.id.IdentityGenerator"/>
11
       <generator class="org.hibernate.id.IdentityGenerator"/>
12
     </id>
12
     </id>
13
 
13
 

+ 1
- 1
dddsample/tracking/core/src/main/resources/se/citerus/dddsample/tracking/core/infrastructure/persistence/hibernate/Leg.hbm.xml 查看文件

6
 
6
 
7
 <hibernate-mapping default-access="field">
7
 <hibernate-mapping default-access="field">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.cargo.Leg" table="Leg" mutable="false">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.cargo.Leg" table="Leg" mutable="false">
9
-    <id name="id" column="id">
9
+    <id name="_primaryKey" column="id">
10
       <generator class="org.hibernate.id.IdentityGenerator"/>
10
       <generator class="org.hibernate.id.IdentityGenerator"/>
11
     </id>
11
     </id>
12
 
12
 

+ 1
- 1
dddsample/tracking/core/src/main/resources/se/citerus/dddsample/tracking/core/infrastructure/persistence/hibernate/Location.hbm.xml 查看文件

6
 
6
 
7
 <hibernate-mapping default-access="field">
7
 <hibernate-mapping default-access="field">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.location.Location" table="Location">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.location.Location" table="Location">
9
-    <id name="id" column="id">
9
+    <id name="_primaryKey" column="id">
10
       <generator class="org.hibernate.id.IdentityGenerator"/>
10
       <generator class="org.hibernate.id.IdentityGenerator"/>
11
     </id>
11
     </id>
12
     <component name="unLocode" unique="true" update="false">
12
     <component name="unLocode" unique="true" update="false">

+ 1
- 1
dddsample/tracking/core/src/main/resources/se/citerus/dddsample/tracking/core/infrastructure/persistence/hibernate/Voyage.hbm.xml 查看文件

7
 <hibernate-mapping default-access="field">
7
 <hibernate-mapping default-access="field">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.voyage.Voyage" table="Voyage">
8
   <class name="se.citerus.dddsample.tracking.core.domain.model.voyage.Voyage" table="Voyage">
9
 
9
 
10
-    <id name="id" column="id">
10
+    <id name="_primaryKey" column="id">
11
       <generator class="org.hibernate.id.IdentityGenerator"/>
11
       <generator class="org.hibernate.id.IdentityGenerator"/>
12
     </id>
12
     </id>
13
 
13