-
Notifications
You must be signed in to change notification settings - Fork 0
/
tofrom_numbers.go
94 lines (82 loc) · 2.2 KB
/
tofrom_numbers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gophonenumbers
import (
"strconv"
"github.com/pkg/errors"
)
type Direction int
const (
To Direction = iota
From
)
type ToFromNumbersSets struct {
All ToFromNumbersSet
AreaCodesMap map[string]ToFromNumbersSet
}
type ToFromNumbersSetsStats struct {
ToAreaCodeCount uint
ToNumberCount uint
FromAreaCodeCount uint
FromNumberCount uint
}
func NewToFromNumbersSets() ToFromNumbersSets {
return ToFromNumbersSets{
AreaCodesMap: map[string]ToFromNumbersSet{}}
}
type ToFromNumbersSet struct {
To NumbersSet
From NumbersSet
}
func NewToFromNumbersSet() ToFromNumbersSet {
return ToFromNumbersSet{
To: NewNumbersSet(),
From: NewNumbersSet()}
}
func (tfns *ToFromNumbersSet) AddNumber(ni NumberInfo, direction Direction) error {
if direction == To {
return tfns.To.AddNumber(ni)
}
return tfns.From.AddNumber(ni)
}
func (tfSets *ToFromNumbersSets) AddNumber(num NumberInfo, direction Direction, addAreaCode bool) error {
if direction == To {
err := tfSets.All.To.AddNumber(num)
if err != nil {
return errors.Wrap(err, "AreaCodeNumbersSets.AddNumber")
}
if addAreaCode {
num.InflateComponents()
areaCode := strconv.Itoa(int(num.Components.NANPAreaCode))
tfSetAC, ok := tfSets.AreaCodesMap[areaCode]
if !ok {
tfSetAC = NewToFromNumbersSet()
}
tfSetAC.AddNumber(num, direction)
tfSets.AreaCodesMap[areaCode] = tfSetAC
}
return nil
}
err := tfSets.All.From.AddNumber(num)
if err != nil {
return errors.Wrap(err, "AreaCodeNumbersSets.AddNumber")
}
if addAreaCode {
num.InflateComponents()
areaCode := strconv.Itoa(int(num.Components.NANPAreaCode))
tfSetAC, ok := tfSets.AreaCodesMap[areaCode]
if !ok {
tfSetAC = NewToFromNumbersSet()
}
tfSetAC.AddNumber(num, direction)
tfSets.AreaCodesMap[areaCode] = tfSetAC
}
return nil
}
func (tfSets *ToFromNumbersSets) Stats() ToFromNumbersSetsStats {
toAreaCodes := tfSets.All.To.AreaCodes()
fromAreaCodes := tfSets.All.From.AreaCodes()
return ToFromNumbersSetsStats{
ToAreaCodeCount: uint(len(toAreaCodes.Bins)),
FromAreaCodeCount: uint(len(fromAreaCodes.Bins)),
ToNumberCount: uint(len(tfSets.All.To.NumbersMap)),
FromNumberCount: uint(len(tfSets.All.From.NumbersMap))}
}