View Javadoc

1   package se.citerus.dddsample.domain.model.voyage;
2   
3   import org.apache.commons.lang.Validate;
4   import org.apache.commons.lang.builder.EqualsBuilder;
5   import org.apache.commons.lang.builder.HashCodeBuilder;
6   import se.citerus.dddsample.domain.model.location.Location;
7   import se.citerus.dddsample.domain.shared.ValueObject;
8   
9   import java.util.Date;
10  
11  
12  /**
13   * A carrier movement is a vessel voyage from one location to another.
14   */
15  public final class CarrierMovement implements ValueObject<CarrierMovement> {
16  
17    private Location departureLocation;
18    private Location arrivalLocation;
19    private Date departureTime;
20    private Date arrivalTime;
21  
22    // Null object pattern 
23    public static final CarrierMovement NONE = new CarrierMovement(
24      Location.UNKNOWN, Location.UNKNOWN,
25      new Date(0), new Date(0)
26    );
27  
28    /**
29     * Constructor.
30     *
31     * @param departureLocation location of departure
32     * @param arrivalLocation location of arrival
33     * @param departureTime time of departure
34     * @param arrivalTime time of arrival
35     */
36    // TODO make package local
37    public CarrierMovement(Location departureLocation,
38                           Location arrivalLocation,
39                           Date departureTime,
40                           Date arrivalTime) {
41      Validate.noNullElements(new Object[]{departureLocation, arrivalLocation, departureTime, arrivalTime});
42      this.departureTime = departureTime;
43      this.arrivalTime = arrivalTime;
44      this.departureLocation = departureLocation;
45      this.arrivalLocation = arrivalLocation;
46    }
47  
48    /**
49     * @return Departure location.
50     */
51    public Location departureLocation() {
52      return departureLocation;
53    }
54  
55    /**
56     * @return Arrival location.
57     */
58    public Location arrivalLocation() {
59      return arrivalLocation;
60    }
61  
62    /**
63     * @return Time of departure.
64     */
65    public Date departureTime() {
66      return new Date(departureTime.getTime());
67    }
68  
69    /**
70     * @return Time of arrival.
71     */
72    public Date arrivalTime() {
73      return new Date(arrivalTime.getTime());
74    }
75  
76    @Override
77    public boolean equals(final Object o) {
78      if (this == o) return true;
79      if (o == null || getClass() != o.getClass()) return false;
80  
81      final CarrierMovement that = (CarrierMovement) o;
82  
83      return sameValueAs(that);
84    }
85  
86    @Override
87    public int hashCode() {
88      return new HashCodeBuilder().
89        append(this.departureLocation).
90        append(this.departureTime).
91        append(this.arrivalLocation).
92        append(this.arrivalTime).
93        toHashCode();
94    }
95  
96    @Override
97    public boolean sameValueAs(CarrierMovement other) {
98      return other != null && new EqualsBuilder().
99        append(this.departureLocation, other.departureLocation).
100       append(this.departureTime, other.departureTime).
101       append(this.arrivalLocation, other.arrivalLocation).
102       append(this.arrivalTime, other.arrivalTime).
103       isEquals();
104   }
105 
106   CarrierMovement() {
107     // Needed by Hibernate
108   }
109 
110   // Auto-generated surrogate key
111   private Long id;
112 
113 }