Pārlūkot izejas kodu

Made UnLocode implement ValueObject

peter_backlund 18 gadus atpakaļ
vecāks
revīzija
2f021ac340

+ 18
- 7
dddsample/src/main/java/se/citerus/dddsample/domain/model/location/UnLocode.java Parādīt failu

@@ -1,8 +1,8 @@
1 1
 package se.citerus.dddsample.domain.model.location;
2 2
 
3 3
 import org.apache.commons.lang.Validate;
4
-import org.apache.commons.lang.builder.EqualsBuilder;
5 4
 import org.apache.commons.lang.builder.HashCodeBuilder;
5
+import se.citerus.dddsample.domain.model.ValueObject;
6 6
 
7 7
 import java.util.regex.Pattern;
8 8
 
@@ -12,7 +12,7 @@ import java.util.regex.Pattern;
12 12
  * http://www.unece.org/cefact/locode/
13 13
  * http://www.unece.org/cefact/locode/DocColumnDescription.htm#LOCODE
14 14
  */
15
-public final class UnLocode {
15
+public final class UnLocode implements ValueObject<UnLocode> {
16 16
 
17 17
   private String unlocode;
18 18
 
@@ -26,22 +26,32 @@ public final class UnLocode {
26 26
    * @param countryAndLocation Location string.
27 27
    */
28 28
   public UnLocode(final String countryAndLocation) {
29
-    Validate.notNull(countryAndLocation);
30
-    Validate.isTrue(VALID_PATTERN.matcher(countryAndLocation).matches());
29
+    Validate.notNull(countryAndLocation, "Country and location may not be null");
30
+    Validate.isTrue(VALID_PATTERN.matcher(countryAndLocation).matches(),
31
+      countryAndLocation + " is not a valid UN/LOCODE (does not match pattern)");
31 32
 
32 33
     this.unlocode = countryAndLocation.toUpperCase();
33 34
   }
34 35
 
35 36
   /**
36
-   * @return country code and location code concatenated.
37
+   * @return country code and location code concatenated, always upper case.
37 38
    */
38 39
   public String idString() {
39 40
     return unlocode;
40 41
   }
41 42
 
42 43
   @Override
43
-  public boolean equals(final Object obj) {
44
-    return EqualsBuilder.reflectionEquals(this, obj);
44
+  public boolean equals(final Object o) {
45
+    if (this == o) return true;
46
+    if (o == null || getClass() != o.getClass()) return false;
47
+
48
+    UnLocode other = (UnLocode) o;
49
+
50
+    return sameValueAs(other);
51
+  }
52
+
53
+  public boolean sameValueAs(UnLocode other) {
54
+    return other != null && this.idString().equals(other.idString());
45 55
   }
46 56
 
47 57
   @Override
@@ -57,4 +67,5 @@ public final class UnLocode {
57 67
   UnLocode() {
58 68
     // Needed by Hibernate
59 69
   }
70
+
60 71
 }