Quellcode durchsuchen

Implemented sameValueAs, equals and hashCode for DeliveryHistory as a value object.

peter_backlund vor 18 Jahren
Ursprung
Commit
8122211be7

+ 41
- 28
dddsample/src/main/java/se/citerus/dddsample/domain/DeliveryHistory.java Datei anzeigen

@@ -1,14 +1,13 @@
1 1
 package se.citerus.dddsample.domain;
2 2
 
3
-import org.apache.commons.lang.builder.ReflectionToStringBuilder;
4
-import org.apache.commons.lang.builder.ToStringStyle;
3
+import static se.citerus.dddsample.domain.StatusCode.*;
5 4
 
6 5
 import java.util.*;
7 6
 
8 7
 /**
9 8
  * The delivery history of a cargo.
10
- * <p/>
11
- * Is this an entiy or a value object?
9
+ *
10
+ * This is a value object.
12 11
  */
13 12
 public final class DeliveryHistory {
14 13
 
@@ -53,38 +52,31 @@ public final class DeliveryHistory {
53 52
     }
54 53
   }
55 54
 
56
-  @Override
57
-  public String toString() {
58
-    return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
59
-  }
60
-
61
-  // Needed by Hibernate
62
-  DeliveryHistory() {
63
-  }
64
-
65 55
   public StatusCode status() {
66 56
     if (lastEvent() == null)
67
-      return StatusCode.NOT_RECEIVED;
57
+      return NOT_RECEIVED;
68 58
 
69 59
     final HandlingEvent.Type type = lastEvent().type();
70
-    if (type == HandlingEvent.Type.LOAD)
71
-      return StatusCode.ONBOARD_CARRIER;
72
-
73
-    if (type == HandlingEvent.Type.UNLOAD)
74
-      return StatusCode.IN_PORT;
60
+    
61
+    switch (type) {
62
+      case LOAD:
63
+        return ONBOARD_CARRIER;
75 64
 
76
-    if (type == HandlingEvent.Type.RECEIVE)
77
-      return StatusCode.IN_PORT;
65
+      case UNLOAD:
66
+      case RECEIVE:
67
+      case CUSTOMS:
68
+        return IN_PORT;
78 69
 
79
-    if (type == HandlingEvent.Type.CLAIM)
80
-      return StatusCode.CLAIMED;
70
+      case CLAIM:
71
+        return CLAIMED;
81 72
 
82
-    //TODO: What about Type.CUSTOMS?
83
-    return null;
73
+      default:
74
+        return null;
75
+    }
84 76
   }
85 77
 
86 78
   public Location currentLocation() {
87
-    if (status().equals(StatusCode.IN_PORT)) {
79
+    if (status().equals(IN_PORT)) {
88 80
       return lastEvent().location();
89 81
     } else {
90 82
       return null;
@@ -92,13 +84,34 @@ public final class DeliveryHistory {
92 84
   }
93 85
 
94 86
   public CarrierMovement currentCarrierMovement() {
95
-    if (status().equals(StatusCode.ONBOARD_CARRIER)) {
87
+    if (status().equals(ONBOARD_CARRIER)) {
96 88
       return lastEvent().carrierMovement();
97 89
     } else {
98 90
       return null;
99 91
     }
100 92
   }
101 93
 
102
-  // TODO: what about equals, hashCode, toString?
94
+  public boolean sameValueAs(DeliveryHistory other) {
95
+    return other != null && events.equals(other.events);
96
+  }
97
+
98
+  @Override
99
+  public boolean equals(Object o) {
100
+    if (this == o) return true;
101
+    if (o == null || getClass() != o.getClass()) return false;
102
+
103
+    final DeliveryHistory other = (DeliveryHistory) o;
104
+
105
+    return sameValueAs(other);
106
+  }
107
+
108
+  @Override
109
+  public int hashCode() {
110
+    return events.hashCode();
111
+  }
112
+
113
+  DeliveryHistory() {
114
+    // Needed by Hibernate
115
+  }
103 116
 
104 117
 }