View Javadoc

1   package se.citerus.dddsample.domain.model.location;
2   
3   import org.apache.commons.lang.Validate;
4   import se.citerus.dddsample.domain.shared.Entity;
5   
6   /**
7    * A location is our model is stops on a journey, such as cargo
8    * origin or destination, or carrier movement endpoints.
9    *
10   * It is uniquely identified by a UN Locode.
11   *
12   */
13  public final class Location implements Entity<Location> {
14  
15    private UnLocode unLocode;
16    private String name;
17  
18    /**
19     * Special Location object that marks an unknown location.
20     */
21    public static final Location UNKNOWN = new Location(
22      new UnLocode("XXXXX"), "Unknown location"
23    );
24  
25    /**
26     * Package-level constructor, visible for test only.
27     *
28     * @param unLocode UN Locode
29     * @param name     location name
30     * @throws IllegalArgumentException if the UN Locode or name is null
31     */
32    Location(final UnLocode unLocode, final String name) {
33      Validate.notNull(unLocode);
34      Validate.notNull(name);
35      
36      this.unLocode = unLocode;
37      this.name = name;
38    }
39  
40    /**
41     * @return UN Locode for this location.
42     */
43    public UnLocode unLocode() {
44      return unLocode;
45    }
46  
47    /**
48     * @return Actual name of this location, e.g. "Stockholm".
49     */
50    public String name() {
51      return name;
52    }
53  
54    /**
55     * @param object to compare
56     * @return Since this is an entiy this will be true iff UN locodes are equal.
57     */
58    @Override
59    public boolean equals(final Object object) {
60      if (object == null) {
61        return false;
62      }
63      if (this == object) {
64        return true;
65      }
66      if (!(object instanceof Location)) {
67        return false;
68      }
69      Location other = (Location) object;
70      return sameIdentityAs(other);
71    }
72  
73    @Override
74    public boolean sameIdentityAs(final Location other) {
75      return this.unLocode.sameValueAs(other.unLocode);
76    }
77  
78    /**
79     * @return Hash code of UN locode.
80     */
81    @Override
82    public int hashCode() {
83      return unLocode.hashCode();
84    }
85  
86    @Override
87    public String toString() {
88      return name + " [" + unLocode + "]";
89    }
90  
91    Location() {
92      // Needed by Hibernate
93    }
94  
95    private Long id;
96  
97  }