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.shared.AbstractSpecification;
8 import se.citerus.dddsample.domain.shared.ValueObject;
9
10 import java.util.Date;
11
12
13
14
15
16
17 public class RouteSpecification extends AbstractSpecification<Itinerary> implements ValueObject<RouteSpecification> {
18
19 private Location origin;
20 private Location destination;
21 private Date arrivalDeadline;
22
23
24
25
26
27
28 public RouteSpecification(final Location origin, final Location destination, final Date arrivalDeadline) {
29 Validate.notNull(origin, "Origin is required");
30 Validate.notNull(destination, "Destination is required");
31 Validate.notNull(arrivalDeadline, "Arrival deadline is required");
32 Validate.isTrue(!origin.sameIdentityAs(destination), "Origin and destination can't be the same: " + origin);
33
34 this.origin = origin;
35 this.destination = destination;
36 this.arrivalDeadline = (Date) arrivalDeadline.clone();
37 }
38
39
40
41
42 public Location origin() {
43 return origin;
44 }
45
46
47
48
49 public Location destination() {
50 return destination;
51 }
52
53
54
55
56 public Date arrivalDeadline() {
57 return new Date(arrivalDeadline.getTime());
58 }
59
60 @Override
61 public boolean isSatisfiedBy(final Itinerary itinerary) {
62 return itinerary != null &&
63 origin().sameIdentityAs(itinerary.initialDepartureLocation()) &&
64 destination().sameIdentityAs(itinerary.finalArrivalLocation()) &&
65 arrivalDeadline().after(itinerary.finalArrivalDate());
66 }
67
68 @Override
69 public boolean sameValueAs(final RouteSpecification other) {
70 return other != null && new EqualsBuilder().
71 append(this.origin, other.origin).
72 append(this.destination, other.destination).
73 append(this.arrivalDeadline, other.arrivalDeadline).
74 isEquals();
75 }
76
77 @Override
78 public boolean equals(final Object o) {
79 if (this == o) return true;
80 if (o == null || getClass() != o.getClass()) return false;
81
82 final RouteSpecification that = (RouteSpecification) o;
83
84 return sameValueAs(that);
85 }
86
87 @Override
88 public int hashCode() {
89 return new HashCodeBuilder().
90 append(this.origin).
91 append(this.destination).
92 append(this.arrivalDeadline).
93 toHashCode();
94 }
95
96 RouteSpecification() {
97
98 }
99
100 }