Cargo.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package se.citerus.dddsample.domain;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import org.apache.commons.lang.builder.EqualsBuilder;
  8. import org.apache.commons.lang.builder.HashCodeBuilder;
  9. import org.apache.commons.lang.builder.ReflectionToStringBuilder;
  10. import org.apache.commons.lang.builder.ToStringStyle;
  11. import javax.persistence.EmbeddedId;
  12. import javax.persistence.Entity;
  13. import javax.persistence.ManyToOne;
  14. import javax.persistence.OneToMany;
  15. /**
  16. * A Cargo an entity identifed by TrackingId and is capable of getting its DeliveryHistory plus a number
  17. * of convenience operation for finding current destination etc.
  18. */
  19. @Entity
  20. public class Cargo {
  21. @EmbeddedId
  22. private TrackingId trackingId;
  23. @ManyToOne
  24. private Location origin;
  25. @ManyToOne
  26. private Location finalDestination;
  27. @OneToMany
  28. private final Set<HandlingEvent> events = new HashSet<HandlingEvent>();
  29. public Cargo(TrackingId trackingId, Location origin, Location finalDestination) {
  30. this.trackingId = trackingId;
  31. this.origin = origin;
  32. this.finalDestination = finalDestination;
  33. }
  34. //
  35. // public DeliveryHistory deliveryHistory() {
  36. // return history;
  37. // }
  38. public void handle(HandlingEvent event) {
  39. events.add(event);
  40. }
  41. /**
  42. * @return An <b>unmodifiable</b> list of handling events, ordered by the time the events occured.
  43. */
  44. public List<HandlingEvent> eventsOrderedByTime() {
  45. List<HandlingEvent> eventList = new ArrayList<HandlingEvent>(events);
  46. Collections.sort(eventList, HandlingEvent.BY_TIMESTAMP_COMPARATOR);
  47. return Collections.unmodifiableList(eventList);
  48. }
  49. /**
  50. *
  51. * @return The last handled event
  52. */
  53. public HandlingEvent lastEvent() {
  54. if (events.isEmpty()) {
  55. return null;
  56. } else {
  57. List<HandlingEvent> orderedEvents = eventsOrderedByTime();
  58. return orderedEvents.get(orderedEvents.size() - 1);
  59. }
  60. }
  61. /**
  62. * Checks if the Cargo's last event was reported at the same Location as the final destination.
  63. *
  64. * Note that this doesn't nessecary mean that the Cargo has been delivered. Possibly there are more handling to be done before the Cargo can be claimed at the final destination
  65. *
  66. * @return true if Cargos is at final destination otherwise false.
  67. */
  68. public boolean atFinalDestiation() {
  69. return currentLocation().equals(finalDestination);
  70. }
  71. /**
  72. * Returns the last known Location or Location.UNKOWN if no HandlingEvent history can be found for this Cargo
  73. *
  74. * TODO: Rename this to lastKnownLocation.
  75. *
  76. * @return The last known location
  77. */
  78. public Location currentLocation() {
  79. HandlingEvent lastEvent = lastEvent();
  80. // If we have no last event, we have not even received the package. Return unknown location
  81. if (lastEvent == null) {
  82. return Location.UNKNOWN;
  83. }
  84. Location location = lastEvent.location();
  85. return location;
  86. }
  87. public TrackingId trackingId() {
  88. return trackingId;
  89. }
  90. public Location origin() {
  91. return origin;
  92. }
  93. public Location finalDestination() {
  94. return finalDestination;
  95. }
  96. @Override
  97. public String toString() {
  98. return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
  99. }
  100. @Override
  101. public boolean equals(Object obj) {
  102. if (!(obj instanceof Cargo)) {
  103. return false;
  104. }
  105. Cargo rhs = (Cargo) obj;
  106. return new EqualsBuilder()
  107. .append(trackingId, rhs.trackingId)
  108. .append(origin, rhs.origin)
  109. .append(finalDestination, rhs.finalDestination)
  110. .isEquals();
  111. }
  112. @Override
  113. public int hashCode() {
  114. return new HashCodeBuilder(7, 39)
  115. .append(trackingId)
  116. .append(origin)
  117. .append(finalDestination)
  118. .toHashCode();
  119. }
  120. // Needed by Hibernate
  121. Cargo() {}
  122. }