View Javadoc

1   package se.citerus.dddsample.domain.model.cargo;
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.model.voyage.Voyage;
8   import se.citerus.dddsample.domain.shared.ValueObject;
9   
10  import java.util.Date;
11  
12  /**
13   * An itinerary consists of one or more legs.
14   */
15  public class Leg implements ValueObject<Leg> {
16  
17    private Voyage voyage;
18    private Location loadLocation;
19    private Location unloadLocation;
20    private Date loadTime;
21    private Date unloadTime;
22  
23    public Leg(Voyage voyage, Location loadLocation, Location unloadLocation, Date loadTime, Date unloadTime) {
24      Validate.noNullElements(new Object[] {voyage, loadLocation, unloadLocation, loadTime, unloadTime});
25      
26      this.voyage = voyage;
27      this.loadLocation = loadLocation;
28      this.unloadLocation = unloadLocation;
29      this.loadTime = loadTime;
30      this.unloadTime = unloadTime;
31    }
32  
33    public Voyage voyage() {
34      return voyage;
35    }
36  
37    public Location loadLocation() {
38      return loadLocation;
39    }
40  
41    public Location unloadLocation() {
42      return unloadLocation;
43    }
44  
45    public Date loadTime() {
46      return new Date(loadTime.getTime());
47    }
48  
49    public Date unloadTime() {
50      return new Date(unloadTime.getTime());
51    }
52  
53    @Override
54    public boolean sameValueAs(final Leg other) {
55      return other != null && new EqualsBuilder().
56        append(this.voyage, other.voyage).
57        append(this.loadLocation, other.loadLocation).
58        append(this.unloadLocation, other.unloadLocation).
59        append(this.loadTime, other.loadTime).
60        append(this.unloadTime, other.unloadTime).
61        isEquals();
62    }
63  
64    @Override
65    public boolean equals(final Object o) {
66      if (this == o) return true;
67      if (o == null || getClass() != o.getClass()) return false;
68  
69      Leg leg = (Leg) o;
70  
71      return sameValueAs(leg);
72    }
73  
74    @Override
75    public int hashCode() {
76      return new HashCodeBuilder().
77        append(voyage).
78        append(loadLocation).
79        append(unloadLocation).
80        append(loadTime).
81        append(unloadTime).
82        toHashCode();
83    }
84  
85    Leg() {
86      // Needed by Hibernate
87    }
88  
89    // Auto-generated surrogate key
90    private Long id;
91  
92  }