-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelection_builder.go
47 lines (41 loc) · 1.46 KB
/
election_builder.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
package trp
// ElectionBuilder exposes a simple builder-pattern DSL for building up an Election progressively.
type ElectionBuilder struct {
ElectionID string
Ballots []*Ballot
}
func NewElectionBuilder(optionalElectionID ...string) *ElectionBuilder {
var electionID string
if len(optionalElectionID) == 1 {
electionID = optionalElectionID[0]
} else {
electionID = "Election"
}
ballots := []*Ballot{}
return &ElectionBuilder{electionID, ballots}
}
// Ballot creates a new Ballot given a two-dimensional slice of priorities
func (builder *ElectionBuilder) Ballot(voterID string, choices [][]string) *ElectionBuilder {
ballot := &Ballot{
VoterID: voterID,
Priorities: choices,
}
builder.Ballots = append(builder.Ballots, ballot)
return builder
}
// Vote is similar to Ballot but it allows only a single dimension of choices (i.e. no ties)
func (builder *ElectionBuilder) Vote(voterID string, choices ...string) *ElectionBuilder {
priorities := make([][]string, 0, len(choices))
for _, rank := range choices {
priorities = append(priorities, []string{rank})
}
return builder.Ballot(voterID, priorities)
}
// Election returns a new Election with all the ballots from this builder included
func (builder *ElectionBuilder) Election() *Election {
return NewElection(builder.ElectionID, builder.Ballots)
}
// Results is simply a shorthand for Election().Results()
func (builder *ElectionBuilder) Results() *ElectionResults {
return builder.Election().Results()
}