Explorar el Código

Fixed incorrect indentation

Decker108 hace 11 años
padre
commit
82badfbee1

+ 49
- 49
src/main/java/se/citerus/dddsample/domain/model/handling/HandlingHistory.java Ver fichero

@@ -4,67 +4,67 @@ import org.apache.commons.lang.Validate;
4 4
 import se.citerus.dddsample.domain.shared.ValueObject;
5 5
 
6 6
 import java.util.*;
7
+
7 8
 import static java.util.Collections.sort;
8 9
 
9 10
 /**
10 11
  * The handling history of a cargo.
11
- *
12 12
  */
13 13
 public class HandlingHistory implements ValueObject<HandlingHistory> {
14 14
 
15
-  private final List<HandlingEvent> handlingEvents;
16
-
17
-  public static final HandlingHistory EMPTY = new HandlingHistory(Collections.<HandlingEvent>emptyList());
18
-
19
-  public HandlingHistory(Collection<HandlingEvent> handlingEvents) {
20
-    Validate.notNull(handlingEvents, "Handling events are required");
21
-
22
-    this.handlingEvents = new ArrayList<>(handlingEvents);
23
-  }
24
-
25
-  /**
26
-   * @return A distinct list (no duplicate registrations) of handling events, ordered by completion time.
27
-   */
28
-  public List<HandlingEvent> distinctEventsByCompletionTime() {
29
-    final List<HandlingEvent> ordered = new ArrayList<>(
30
-            new HashSet<>(handlingEvents)
31
-    );
32
-    sort(ordered, BY_COMPLETION_TIME_COMPARATOR);
33
-    return Collections.unmodifiableList(ordered);
34
-  }
35
-
36
-  /**
37
-   * @return Most recently completed event, or null if the delivery history is empty.
38
-   */
39
-  public HandlingEvent mostRecentlyCompletedEvent() {
40
-    final List<HandlingEvent> distinctEvents = distinctEventsByCompletionTime();
41
-    if (distinctEvents.isEmpty()) {
42
-      return null;
43
-    } else {
44
-      return distinctEvents.get(distinctEvents.size() - 1);
15
+    private final List<HandlingEvent> handlingEvents;
16
+
17
+    public static final HandlingHistory EMPTY = new HandlingHistory(Collections.<HandlingEvent>emptyList());
18
+
19
+    public HandlingHistory(Collection<HandlingEvent> handlingEvents) {
20
+        Validate.notNull(handlingEvents, "Handling events are required");
21
+
22
+        this.handlingEvents = new ArrayList<>(handlingEvents);
45 23
     }
46
-  }
47 24
 
48
-  @Override
49
-  public boolean sameValueAs(HandlingHistory other) {
50
-    return other != null && this.handlingEvents.equals(other.handlingEvents);
51
-  }
25
+    /**
26
+     * @return A distinct list (no duplicate registrations) of handling events, ordered by completion time.
27
+     */
28
+    public List<HandlingEvent> distinctEventsByCompletionTime() {
29
+        final List<HandlingEvent> ordered = new ArrayList<>(
30
+                new HashSet<>(handlingEvents)
31
+        );
32
+        sort(ordered, BY_COMPLETION_TIME_COMPARATOR);
33
+        return Collections.unmodifiableList(ordered);
34
+    }
52 35
 
53
-  @Override
54
-  public boolean equals(Object o) {
55
-    if (this == o) return true;
56
-    if (o == null || getClass() != o.getClass()) return false;
36
+    /**
37
+     * @return Most recently completed event, or null if the delivery history is empty.
38
+     */
39
+    public HandlingEvent mostRecentlyCompletedEvent() {
40
+        final List<HandlingEvent> distinctEvents = distinctEventsByCompletionTime();
41
+        if (distinctEvents.isEmpty()) {
42
+            return null;
43
+        } else {
44
+            return distinctEvents.get(distinctEvents.size() - 1);
45
+        }
46
+    }
57 47
 
58
-    final HandlingHistory other = (HandlingHistory) o;
59
-    return sameValueAs(other);
60
-  }
48
+    @Override
49
+    public boolean sameValueAs(HandlingHistory other) {
50
+        return other != null && this.handlingEvents.equals(other.handlingEvents);
51
+    }
52
+
53
+    @Override
54
+    public boolean equals(Object o) {
55
+        if (this == o) return true;
56
+        if (o == null || getClass() != o.getClass()) return false;
61 57
 
62
-  @Override
63
-  public int hashCode() {
64
-    return handlingEvents.hashCode();
65
-  }
58
+        final HandlingHistory other = (HandlingHistory) o;
59
+        return sameValueAs(other);
60
+    }
61
+
62
+    @Override
63
+    public int hashCode() {
64
+        return handlingEvents.hashCode();
65
+    }
66 66
 
67
-  private static final Comparator<HandlingEvent> BY_COMPLETION_TIME_COMPARATOR =
68
-          (he1, he2) -> he1.completionTime().compareTo(he2.completionTime());
67
+    private static final Comparator<HandlingEvent> BY_COMPLETION_TIME_COMPARATOR =
68
+            (he1, he2) -> he1.completionTime().compareTo(he2.completionTime());
69 69
 
70 70
 }

+ 116
- 115
src/main/java/se/citerus/dddsample/domain/model/voyage/SampleVoyages.java Ver fichero

@@ -1,7 +1,9 @@
1 1
 package se.citerus.dddsample.domain.model.voyage;
2 2
 
3 3
 import static se.citerus.dddsample.application.util.DateTestUtil.toDate;
4
+
4 5
 import se.citerus.dddsample.domain.model.location.Location;
6
+
5 7
 import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
6 8
 
7 9
 import java.lang.reflect.Field;
@@ -9,126 +11,125 @@ import java.util.*;
9 11
 
10 12
 /**
11 13
  * Sample carrier movements, for test purposes.
12
- *
13 14
  */
14 15
 public class SampleVoyages {
15 16
 
16
-  public static final Voyage CM001 = createVoyage("CM001", STOCKHOLM, HAMBURG);
17
-  public static final Voyage CM002 = createVoyage("CM002", HAMBURG, HONGKONG);
18
-  public static final Voyage CM003 = createVoyage("CM003", HONGKONG, NEWYORK);
19
-  public static final Voyage CM004 = createVoyage("CM004", NEWYORK, CHICAGO);
20
-  public static final Voyage CM005 = createVoyage("CM005", CHICAGO, HAMBURG);
21
-  public static final Voyage CM006 = createVoyage("CM006", HAMBURG, HANGZOU);
22
-  private static Voyage createVoyage(String id, Location from, Location to) {
23
-    return new Voyage(new VoyageNumber(id), new Schedule(Collections.singletonList(
24
-            new CarrierMovement(from, to, new Date(), new Date())
25
-    )));
26
-  }
27
-
28
-  // TODO CM00[1-6] and createVoyage are deprecated. Remove and refactor tests.
29
-
30
-  public final static Voyage v100 = new Voyage.Builder(new VoyageNumber("V100"), HONGKONG).
31
-    addMovement(TOKYO, toDate("2009-03-03"), toDate("2009-03-05")).
32
-    addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-09")).
33
-    build();
34
-  public final static Voyage v200 = new Voyage.Builder(new VoyageNumber("V200"), TOKYO).
35
-      addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-08")).
36
-      addMovement(CHICAGO, toDate("2009-03-10"), toDate("2009-03-14")).
37
-      addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-16")).
38
-      build();
39
-  public final static Voyage v300 = new Voyage.Builder(new VoyageNumber("V300"), TOKYO).
40
-        addMovement(ROTTERDAM, toDate("2009-03-08"), toDate("2009-03-11")).
41
-        addMovement(HAMBURG, toDate("2009-03-11"), toDate("2009-03-12")).
42
-        addMovement(MELBOURNE, toDate("2009-03-14"), toDate("2009-03-18")).
43
-        addMovement(TOKYO, toDate("2009-03-19"), toDate("2009-03-21")).
44
-        build();
45
-  public final static Voyage v400 = new Voyage.Builder(new VoyageNumber("V400"), HAMBURG).
46
-          addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-15")).
47
-          addMovement(HELSINKI, toDate("2009-03-15"), toDate("2009-03-16")).
48
-          addMovement(HAMBURG, toDate("2009-03-20"), toDate("2009-03-22")).
49
-          build();
50
-
51
-  /**
52
-   * Voyage number 0100S (by ship)
53
-   *
54
-   * Hongkong - Hangzou - Tokyo - Melbourne - New York
55
-   */
56
-  public static final Voyage HONGKONG_TO_NEW_YORK =
57
-    new Voyage.Builder(new VoyageNumber("0100S"), HONGKONG).
58
-      addMovement(HANGZOU, toDate("2008-10-01", "12:00"), toDate("2008-10-03", "14:30")).
59
-      addMovement(TOKYO, toDate("2008-10-03", "21:00"), toDate("2008-10-06", "06:15")).
60
-      addMovement(MELBOURNE, toDate("2008-10-06", "11:00"), toDate("2008-10-12", "11:30")).
61
-      addMovement(NEWYORK, toDate("2008-10-14", "12:00"), toDate("2008-10-23", "23:10")).
62
-      build();
63
-
64
-
65
-  /**
66
-   * Voyage number 0200T (by train)
67
-   *
68
-   * New York - Chicago - Dallas
69
-   */
70
-  public static final Voyage NEW_YORK_TO_DALLAS =
71
-    new Voyage.Builder(new VoyageNumber("0200T"), NEWYORK).
72
-      addMovement(CHICAGO, toDate("2008-10-24", "07:00"), toDate("2008-10-24", "17:45")).
73
-      addMovement(DALLAS, toDate("2008-10-24", "21:25"), toDate("2008-10-25", "19:30")).
74
-      build();
75
-
76
-  /**
77
-   * Voyage number 0300A (by airplane)
78
-   *
79
-   * Dallas - Hamburg - Stockholm - Helsinki
80
-   */
81
-  public static final Voyage DALLAS_TO_HELSINKI =
82
-    new Voyage.Builder(new VoyageNumber("0300A"), DALLAS).
83
-      addMovement(HAMBURG, toDate("2008-10-29", "03:30"), toDate("2008-10-31", "14:00")).
84
-      addMovement(STOCKHOLM, toDate("2008-11-01", "15:20"), toDate("2008-11-01", "18:40")).
85
-      addMovement(HELSINKI, toDate("2008-11-02", "09:00"), toDate("2008-11-02", "11:15")).
86
-      build();
87
-
88
-  /**
89
-   * Voyage number 0301S (by ship)
90
-   *
91
-   * Dallas - Hamburg - Stockholm - Helsinki, alternate route
92
-   */
93
-  public static final Voyage DALLAS_TO_HELSINKI_ALT =
94
-    new Voyage.Builder(new VoyageNumber("0301S"), DALLAS).
95
-      addMovement(HELSINKI, toDate("2008-10-29", "03:30"), toDate("2008-11-05", "15:45")).
96
-      build();
97
-
98
-  /**
99
-   * Voyage number 0400S (by ship)
100
-   *
101
-   * Helsinki - Rotterdam - Shanghai - Hongkong
102
-   *
103
-   */
104
-  public static final Voyage HELSINKI_TO_HONGKONG =
105
-    new Voyage.Builder(new VoyageNumber("0400S"), HELSINKI).
106
-      addMovement(ROTTERDAM, toDate("2008-11-04", "05:50"), toDate("2008-11-06", "14:10")).
107
-      addMovement(SHANGHAI, toDate("2008-11-10", "21:45"), toDate("2008-11-22", "16:40")).
108
-      addMovement(HONGKONG, toDate("2008-11-24", "07:00"), toDate("2008-11-28", "13:37")).
109
-      build();
110
-
111
-  public static final Map<VoyageNumber, Voyage> ALL = new HashMap<>();
112
-
113
-  static {
114
-    for (Field field : SampleVoyages.class.getDeclaredFields()) {
115
-      if (field.getType().equals(Voyage.class)) {
116
-        try {
117
-          Voyage voyage = (Voyage) field.get(null);
118
-          ALL.put(voyage.voyageNumber(), voyage);
119
-        } catch (IllegalAccessException e) {
120
-          throw new RuntimeException(e);
17
+    public static final Voyage CM001 = createVoyage("CM001", STOCKHOLM, HAMBURG);
18
+    public static final Voyage CM002 = createVoyage("CM002", HAMBURG, HONGKONG);
19
+    public static final Voyage CM003 = createVoyage("CM003", HONGKONG, NEWYORK);
20
+    public static final Voyage CM004 = createVoyage("CM004", NEWYORK, CHICAGO);
21
+    public static final Voyage CM005 = createVoyage("CM005", CHICAGO, HAMBURG);
22
+    public static final Voyage CM006 = createVoyage("CM006", HAMBURG, HANGZOU);
23
+
24
+    private static Voyage createVoyage(String id, Location from, Location to) {
25
+        return new Voyage(new VoyageNumber(id), new Schedule(Collections.singletonList(
26
+                new CarrierMovement(from, to, new Date(), new Date())
27
+        )));
28
+    }
29
+
30
+    // TODO CM00[1-6] and createVoyage are deprecated. Remove and refactor tests.
31
+
32
+    public final static Voyage v100 = new Voyage.Builder(new VoyageNumber("V100"), HONGKONG).
33
+            addMovement(TOKYO, toDate("2009-03-03"), toDate("2009-03-05")).
34
+            addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-09")).
35
+            build();
36
+    public final static Voyage v200 = new Voyage.Builder(new VoyageNumber("V200"), TOKYO).
37
+            addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-08")).
38
+            addMovement(CHICAGO, toDate("2009-03-10"), toDate("2009-03-14")).
39
+            addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-16")).
40
+            build();
41
+    public final static Voyage v300 = new Voyage.Builder(new VoyageNumber("V300"), TOKYO).
42
+            addMovement(ROTTERDAM, toDate("2009-03-08"), toDate("2009-03-11")).
43
+            addMovement(HAMBURG, toDate("2009-03-11"), toDate("2009-03-12")).
44
+            addMovement(MELBOURNE, toDate("2009-03-14"), toDate("2009-03-18")).
45
+            addMovement(TOKYO, toDate("2009-03-19"), toDate("2009-03-21")).
46
+            build();
47
+    public final static Voyage v400 = new Voyage.Builder(new VoyageNumber("V400"), HAMBURG).
48
+            addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-15")).
49
+            addMovement(HELSINKI, toDate("2009-03-15"), toDate("2009-03-16")).
50
+            addMovement(HAMBURG, toDate("2009-03-20"), toDate("2009-03-22")).
51
+            build();
52
+
53
+    /**
54
+     * Voyage number 0100S (by ship)
55
+     * <p>
56
+     * Hongkong - Hangzou - Tokyo - Melbourne - New York
57
+     */
58
+    public static final Voyage HONGKONG_TO_NEW_YORK =
59
+            new Voyage.Builder(new VoyageNumber("0100S"), HONGKONG).
60
+                    addMovement(HANGZOU, toDate("2008-10-01", "12:00"), toDate("2008-10-03", "14:30")).
61
+                    addMovement(TOKYO, toDate("2008-10-03", "21:00"), toDate("2008-10-06", "06:15")).
62
+                    addMovement(MELBOURNE, toDate("2008-10-06", "11:00"), toDate("2008-10-12", "11:30")).
63
+                    addMovement(NEWYORK, toDate("2008-10-14", "12:00"), toDate("2008-10-23", "23:10")).
64
+                    build();
65
+
66
+
67
+    /**
68
+     * Voyage number 0200T (by train)
69
+     * <p>
70
+     * New York - Chicago - Dallas
71
+     */
72
+    public static final Voyage NEW_YORK_TO_DALLAS =
73
+            new Voyage.Builder(new VoyageNumber("0200T"), NEWYORK).
74
+                    addMovement(CHICAGO, toDate("2008-10-24", "07:00"), toDate("2008-10-24", "17:45")).
75
+                    addMovement(DALLAS, toDate("2008-10-24", "21:25"), toDate("2008-10-25", "19:30")).
76
+                    build();
77
+
78
+    /**
79
+     * Voyage number 0300A (by airplane)
80
+     * <p>
81
+     * Dallas - Hamburg - Stockholm - Helsinki
82
+     */
83
+    public static final Voyage DALLAS_TO_HELSINKI =
84
+            new Voyage.Builder(new VoyageNumber("0300A"), DALLAS).
85
+                    addMovement(HAMBURG, toDate("2008-10-29", "03:30"), toDate("2008-10-31", "14:00")).
86
+                    addMovement(STOCKHOLM, toDate("2008-11-01", "15:20"), toDate("2008-11-01", "18:40")).
87
+                    addMovement(HELSINKI, toDate("2008-11-02", "09:00"), toDate("2008-11-02", "11:15")).
88
+                    build();
89
+
90
+    /**
91
+     * Voyage number 0301S (by ship)
92
+     * <p>
93
+     * Dallas - Hamburg - Stockholm - Helsinki, alternate route
94
+     */
95
+    public static final Voyage DALLAS_TO_HELSINKI_ALT =
96
+            new Voyage.Builder(new VoyageNumber("0301S"), DALLAS).
97
+                    addMovement(HELSINKI, toDate("2008-10-29", "03:30"), toDate("2008-11-05", "15:45")).
98
+                    build();
99
+
100
+    /**
101
+     * Voyage number 0400S (by ship)
102
+     * <p>
103
+     * Helsinki - Rotterdam - Shanghai - Hongkong
104
+     */
105
+    public static final Voyage HELSINKI_TO_HONGKONG =
106
+            new Voyage.Builder(new VoyageNumber("0400S"), HELSINKI).
107
+                    addMovement(ROTTERDAM, toDate("2008-11-04", "05:50"), toDate("2008-11-06", "14:10")).
108
+                    addMovement(SHANGHAI, toDate("2008-11-10", "21:45"), toDate("2008-11-22", "16:40")).
109
+                    addMovement(HONGKONG, toDate("2008-11-24", "07:00"), toDate("2008-11-28", "13:37")).
110
+                    build();
111
+
112
+    public static final Map<VoyageNumber, Voyage> ALL = new HashMap<>();
113
+
114
+    static {
115
+        for (Field field : SampleVoyages.class.getDeclaredFields()) {
116
+            if (field.getType().equals(Voyage.class)) {
117
+                try {
118
+                    Voyage voyage = (Voyage) field.get(null);
119
+                    ALL.put(voyage.voyageNumber(), voyage);
120
+                } catch (IllegalAccessException e) {
121
+                    throw new RuntimeException(e);
122
+                }
123
+            }
121 124
         }
122
-      }
123 125
     }
124
-  }
125 126
 
126
-  public static List<Voyage> getAll() {
127
-    return new ArrayList<>(ALL.values());
128
-  }
127
+    public static List<Voyage> getAll() {
128
+        return new ArrayList<>(ALL.values());
129
+    }
130
+
131
+    public static Voyage lookup(VoyageNumber voyageNumber) {
132
+        return ALL.get(voyageNumber);
133
+    }
129 134
 
130
-  public static Voyage lookup(VoyageNumber voyageNumber) {
131
-    return ALL.get(voyageNumber);
132
-  }
133
-  
134 135
 }

+ 35
- 36
src/main/java/se/citerus/dddsample/interfaces/tracking/CargoTrackingController.java Ver fichero

@@ -22,59 +22,58 @@ import java.util.Map;
22 22
  * Controller for tracking cargo. This interface sits immediately on top of the
23 23
  * domain layer, unlike the booking interface which has a a remote facade and supporting
24 24
  * DTOs in between.
25
- * <p/>
25
+ * <p>
26 26
  * An adapter class, designed for the tracking use case, is used to wrap the domain model
27 27
  * to make it easier to work with in a web page rendering context. We do not want to apply
28 28
  * view rendering constraints to the design of our domain model, and the adapter
29
- * helps us shield the domain model classes. 
30
- * <p/>
29
+ * helps us shield the domain model classes.
30
+ * <p>
31 31
  *
32 32
  * @eee se.citerus.dddsample.application.web.CargoTrackingViewAdapter
33 33
  * @see se.citerus.dddsample.interfaces.booking.web.CargoAdminController
34
- *
35 34
  */
36 35
 @Controller
37 36
 public final class CargoTrackingController {
38 37
 
39
-  private CargoRepository cargoRepository;
40
-  private HandlingEventRepository handlingEventRepository;
41
-  private MessageSource messageSource;
38
+    private CargoRepository cargoRepository;
39
+    private HandlingEventRepository handlingEventRepository;
40
+    private MessageSource messageSource;
42 41
 
43
-  @RequestMapping(method = RequestMethod.GET)
44
-  public Map<String, CargoTrackingViewAdapter> get(final TrackCommand trackCommand) {
45
-      return new HashMap<>();
46
-  }
42
+    @RequestMapping(method = RequestMethod.GET)
43
+    public Map<String, CargoTrackingViewAdapter> get(final TrackCommand trackCommand) {
44
+        return new HashMap<>();
45
+    }
47 46
 
48
-  @RequestMapping(method = RequestMethod.POST)
49
-  protected Map<String, CargoTrackingViewAdapter> onSubmit(final HttpServletRequest request,
50
-                                                           final TrackCommand command,
51
-                                                           final BindingResult bindingResult) {
52
-    new TrackCommandValidator().validate(command, bindingResult);
47
+    @RequestMapping(method = RequestMethod.POST)
48
+    protected Map<String, CargoTrackingViewAdapter> onSubmit(final HttpServletRequest request,
49
+                                                             final TrackCommand command,
50
+                                                             final BindingResult bindingResult) {
51
+        new TrackCommandValidator().validate(command, bindingResult);
53 52
 
54
-    final TrackingId trackingId = new TrackingId(command.getTrackingId());
55
-    final Cargo cargo = cargoRepository.find(trackingId);
53
+        final TrackingId trackingId = new TrackingId(command.getTrackingId());
54
+        final Cargo cargo = cargoRepository.find(trackingId);
56 55
 
57
-    final Map<String, CargoTrackingViewAdapter> model = new HashMap<String, CargoTrackingViewAdapter>();
58
-    if (cargo != null) {
59
-      final Locale locale = RequestContextUtils.getLocale(request);
60
-      final List<HandlingEvent> handlingEvents = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
61
-      model.put("cargo", new CargoTrackingViewAdapter(cargo, messageSource, locale, handlingEvents));
62
-    } else {
63
-      bindingResult.rejectValue("trackingId", "cargo.unknown_id", new Object[]{command.getTrackingId()}, "Unknown tracking id");
56
+        final Map<String, CargoTrackingViewAdapter> model = new HashMap<String, CargoTrackingViewAdapter>();
57
+        if (cargo != null) {
58
+            final Locale locale = RequestContextUtils.getLocale(request);
59
+            final List<HandlingEvent> handlingEvents = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
60
+            model.put("cargo", new CargoTrackingViewAdapter(cargo, messageSource, locale, handlingEvents));
61
+        } else {
62
+            bindingResult.rejectValue("trackingId", "cargo.unknown_id", new Object[]{command.getTrackingId()}, "Unknown tracking id");
63
+        }
64
+        return model;
64 65
     }
65
-    return model;
66
-  }
67 66
 
68
-  public void setCargoRepository(CargoRepository cargoRepository) {
69
-    this.cargoRepository = cargoRepository;
70
-  }
67
+    public void setCargoRepository(CargoRepository cargoRepository) {
68
+        this.cargoRepository = cargoRepository;
69
+    }
71 70
 
72
-  public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
73
-    this.handlingEventRepository = handlingEventRepository;
74
-  }
71
+    public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
72
+        this.handlingEventRepository = handlingEventRepository;
73
+    }
75 74
 
76
-  public void setMessageSource(MessageSource messageSource) {
77
-      this.messageSource = messageSource;
78
-  }
75
+    public void setMessageSource(MessageSource messageSource) {
76
+        this.messageSource = messageSource;
77
+    }
79 78
 
80 79
 }

+ 69
- 68
src/test/java/se/citerus/dddsample/infrastructure/persistence/inmemory/CargoRepositoryInMem.java Ver fichero

@@ -7,6 +7,7 @@ import se.citerus.dddsample.domain.model.cargo.TrackingId;
7 7
 import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
8 8
 import se.citerus.dddsample.domain.model.handling.HandlingHistory;
9 9
 import se.citerus.dddsample.domain.model.location.Location;
10
+
10 11
 import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
11 12
 
12 13
 import java.util.*;
@@ -14,77 +15,77 @@ import java.util.*;
14 15
 /**
15 16
  * CargoRepositoryInMem implement the CargoRepository interface but is a test
16 17
  * class not intended for usage in real application.
17
- * <p/>
18
+ * <p>
18 19
  * It setup a simple local hash with a number of Cargo's with TrackingId as key
19 20
  * defined at compile time.
20
- * <p/>
21
+ * <p>
21 22
  */
22 23
 public class CargoRepositoryInMem implements CargoRepository {
23 24
 
24
-  private Map<String, Cargo> cargoDb;
25
-  private HandlingEventRepository handlingEventRepository;
26
-
27
-  /**
28
-   * Constructor.
29
-   */
30
-  public CargoRepositoryInMem() {
31
-    cargoDb = new HashMap<>();
32
-  }
33
-
34
-  public Cargo find(final TrackingId trackingId) {
35
-    return cargoDb.get(trackingId.idString());
36
-  }
37
-
38
-  public void store(final Cargo cargo) {
39
-    cargoDb.put(cargo.trackingId().idString(), cargo);
40
-  }
41
-
42
-  public TrackingId nextTrackingId() {
43
-    String random = UUID.randomUUID().toString().toUpperCase();
44
-    return new TrackingId(
45
-      random.substring(0, random.indexOf("-"))
46
-    );
47
-  }
48
-
49
-  public List<Cargo> findAll() {
50
-    return new ArrayList<>(cargoDb.values());
51
-  }
52
-
53
-  public void init() throws Exception {
54
-    final TrackingId xyz = new TrackingId("XYZ");
55
-    final Cargo cargoXYZ = createCargoWithDeliveryHistory(
56
-      xyz, STOCKHOLM, MELBOURNE, handlingEventRepository.lookupHandlingHistoryOfCargo(xyz));
57
-    cargoDb.put(xyz.idString(), cargoXYZ);
58
-
59
-    final TrackingId zyx = new TrackingId("ZYX");
60
-    final Cargo cargoZYX = createCargoWithDeliveryHistory(
61
-      zyx, MELBOURNE, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(zyx));
62
-    cargoDb.put(zyx.idString(), cargoZYX);
63
-
64
-    final TrackingId abc = new TrackingId("ABC");
65
-    final Cargo cargoABC = createCargoWithDeliveryHistory(
66
-      abc, STOCKHOLM, HELSINKI, handlingEventRepository.lookupHandlingHistoryOfCargo(abc));
67
-    cargoDb.put(abc.idString(), cargoABC);
68
-
69
-    final TrackingId cba = new TrackingId("CBA");
70
-    final Cargo cargoCBA = createCargoWithDeliveryHistory(
71
-      cba, HELSINKI, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(cba));
72
-    cargoDb.put(cba.idString(), cargoCBA);
73
-  }
74
-
75
-  public void setHandlingEventRepository(final HandlingEventRepository handlingEventRepository) {
76
-    this.handlingEventRepository = handlingEventRepository;
77
-  }
78
-
79
-  public static Cargo createCargoWithDeliveryHistory(TrackingId trackingId,
80
-                                                     Location origin,
81
-                                                     Location destination,
82
-                                                     HandlingHistory handlingHistory) {
83
-
84
-    final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, new Date());
85
-    final Cargo cargo = new Cargo(trackingId, routeSpecification);
86
-    cargo.deriveDeliveryProgress(handlingHistory);
87
-
88
-    return cargo;
89
-  }
25
+    private Map<String, Cargo> cargoDb;
26
+    private HandlingEventRepository handlingEventRepository;
27
+
28
+    /**
29
+     * Constructor.
30
+     */
31
+    public CargoRepositoryInMem() {
32
+        cargoDb = new HashMap<>();
33
+    }
34
+
35
+    public Cargo find(final TrackingId trackingId) {
36
+        return cargoDb.get(trackingId.idString());
37
+    }
38
+
39
+    public void store(final Cargo cargo) {
40
+        cargoDb.put(cargo.trackingId().idString(), cargo);
41
+    }
42
+
43
+    public TrackingId nextTrackingId() {
44
+        String random = UUID.randomUUID().toString().toUpperCase();
45
+        return new TrackingId(
46
+                random.substring(0, random.indexOf("-"))
47
+        );
48
+    }
49
+
50
+    public List<Cargo> findAll() {
51
+        return new ArrayList<>(cargoDb.values());
52
+    }
53
+
54
+    public void init() throws Exception {
55
+        final TrackingId xyz = new TrackingId("XYZ");
56
+        final Cargo cargoXYZ = createCargoWithDeliveryHistory(
57
+                xyz, STOCKHOLM, MELBOURNE, handlingEventRepository.lookupHandlingHistoryOfCargo(xyz));
58
+        cargoDb.put(xyz.idString(), cargoXYZ);
59
+
60
+        final TrackingId zyx = new TrackingId("ZYX");
61
+        final Cargo cargoZYX = createCargoWithDeliveryHistory(
62
+                zyx, MELBOURNE, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(zyx));
63
+        cargoDb.put(zyx.idString(), cargoZYX);
64
+
65
+        final TrackingId abc = new TrackingId("ABC");
66
+        final Cargo cargoABC = createCargoWithDeliveryHistory(
67
+                abc, STOCKHOLM, HELSINKI, handlingEventRepository.lookupHandlingHistoryOfCargo(abc));
68
+        cargoDb.put(abc.idString(), cargoABC);
69
+
70
+        final TrackingId cba = new TrackingId("CBA");
71
+        final Cargo cargoCBA = createCargoWithDeliveryHistory(
72
+                cba, HELSINKI, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(cba));
73
+        cargoDb.put(cba.idString(), cargoCBA);
74
+    }
75
+
76
+    public void setHandlingEventRepository(final HandlingEventRepository handlingEventRepository) {
77
+        this.handlingEventRepository = handlingEventRepository;
78
+    }
79
+
80
+    public static Cargo createCargoWithDeliveryHistory(TrackingId trackingId,
81
+                                                       Location origin,
82
+                                                       Location destination,
83
+                                                       HandlingHistory handlingHistory) {
84
+
85
+        final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, new Date());
86
+        final Cargo cargo = new Cargo(trackingId, routeSpecification);
87
+        cargo.deriveDeliveryProgress(handlingHistory);
88
+
89
+        return cargo;
90
+    }
90 91
 }