/
vertex_builder_choice.go
51 lines (41 loc) · 1.17 KB
/
vertex_builder_choice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//nolint:dupl
package hsm
// ChoiceVertexBuilder builder.
type ChoiceVertexBuilder[C any] interface {
WithID(id string) ChoiceVertexBuilder[C]
ParentOf(parent *Vertex[C]) ChoiceVertexBuilder[C]
AddTransitions(transitions ...*Transition[C]) ChoiceVertexBuilder[C]
Build() *Vertex[C]
}
type choiceVertexBuilder[C any] struct {
id string
parent *Vertex[C]
edges *edgesCollection[C]
}
// WithID defines vertex's identity, must be unique within the entire HSM.
func (b *choiceVertexBuilder[C]) WithID(id string) ChoiceVertexBuilder[C] {
b.id = id
return b
}
// ParentOf indicates vertex's parent.
func (b *choiceVertexBuilder[C]) ParentOf(parent *Vertex[C]) ChoiceVertexBuilder[C] {
b.parent = parent
return b
}
// AddTransitions registers the given transitions starting from this vertex.
func (b *choiceVertexBuilder[C]) AddTransitions(transitions ...*Transition[C]) ChoiceVertexBuilder[C] {
for _, t := range transitions {
b.edges.add(t)
}
return b
}
// Build returns a vertex instance.
func (b *choiceVertexBuilder[C]) Build() *Vertex[C] {
vertex := &Vertex[C]{
id: b.id,
kind: vertexKindChoice,
parent: b.parent,
edges: b.edges,
}
return vertex
}