OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / examples / shipping / cargo / itinerary.go
1 package cargo
2
3 import (
4         "time"
5
6         "github.com/go-kit/kit/examples/shipping/location"
7         "github.com/go-kit/kit/examples/shipping/voyage"
8 )
9
10 // Leg describes the transportation between two locations on a voyage.
11 type Leg struct {
12         VoyageNumber   voyage.Number     `json:"voyage_number"`
13         LoadLocation   location.UNLocode `json:"from"`
14         UnloadLocation location.UNLocode `json:"to"`
15         LoadTime       time.Time         `json:"load_time"`
16         UnloadTime     time.Time         `json:"unload_time"`
17 }
18
19 // NewLeg creates a new itinerary leg.
20 func NewLeg(voyageNumber voyage.Number, loadLocation, unloadLocation location.UNLocode, loadTime, unloadTime time.Time) Leg {
21         return Leg{
22                 VoyageNumber:   voyageNumber,
23                 LoadLocation:   loadLocation,
24                 UnloadLocation: unloadLocation,
25                 LoadTime:       loadTime,
26                 UnloadTime:     unloadTime,
27         }
28 }
29
30 // Itinerary specifies steps required to transport a cargo from its origin to
31 // destination.
32 type Itinerary struct {
33         Legs []Leg `json:"legs"`
34 }
35
36 // InitialDepartureLocation returns the start of the itinerary.
37 func (i Itinerary) InitialDepartureLocation() location.UNLocode {
38         if i.IsEmpty() {
39                 return location.UNLocode("")
40         }
41         return i.Legs[0].LoadLocation
42 }
43
44 // FinalArrivalLocation returns the end of the itinerary.
45 func (i Itinerary) FinalArrivalLocation() location.UNLocode {
46         if i.IsEmpty() {
47                 return location.UNLocode("")
48         }
49         return i.Legs[len(i.Legs)-1].UnloadLocation
50 }
51
52 // FinalArrivalTime returns the expected arrival time at final destination.
53 func (i Itinerary) FinalArrivalTime() time.Time {
54         return i.Legs[len(i.Legs)-1].UnloadTime
55 }
56
57 // IsEmpty checks if the itinerary contains at least one leg.
58 func (i Itinerary) IsEmpty() bool {
59         return i.Legs == nil || len(i.Legs) == 0
60 }
61
62 // IsExpected checks if the given handling event is expected when executing
63 // this itinerary.
64 func (i Itinerary) IsExpected(event HandlingEvent) bool {
65         if i.IsEmpty() {
66                 return true
67         }
68
69         switch event.Activity.Type {
70         case Receive:
71                 return i.InitialDepartureLocation() == event.Activity.Location
72         case Load:
73                 for _, l := range i.Legs {
74                         if l.LoadLocation == event.Activity.Location && l.VoyageNumber == event.Activity.VoyageNumber {
75                                 return true
76                         }
77                 }
78                 return false
79         case Unload:
80                 for _, l := range i.Legs {
81                         if l.UnloadLocation == event.Activity.Location && l.VoyageNumber == event.Activity.VoyageNumber {
82                                 return true
83                         }
84                 }
85                 return false
86         case Claim:
87                 return i.FinalArrivalLocation() == event.Activity.Location
88         }
89
90         return true
91 }