-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerated_string_map.go
142 lines (110 loc) · 3.3 KB
/
generated_string_map.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Code generated by go-codegen(https://github.com/nchern/go-codegen).
// You COULD edit this code it you really need it and know what are you doing
// Package hashmap provides a built-in implementation of a generic map
package main
import "sync"
// T0 is a generic type variable placeholder of a key type. It will not appear in the generated code
// T1 is a generic type variable placeholder of a value type. It will not appear in the generated code
// StringStringMapVisitor is a visitor function to visit map pairs
type StringStringMapVisitor func(string, string) bool
// StringStringMap exposes the contract of String to String map
type StringStringMap interface {
// Each visits each element in the map. It stops iterations if visitor func returns false
Each(visitor StringStringMapVisitor)
// Get returns the value of a given key. If the key was not found the second return value will be false
Get(key string) (v string, found bool)
// Set sets the value of a given key
Set(key string, val string)
// Update updates the current map from a given map
Update(src map[string]string) StringStringMap
// Remove removes a given key from this map
Remove(key string) bool
// Clone creates a copy of this map
Clone() StringStringMap
}
type baseStringStringMap struct {
_map map[string]string
}
// NewStringStringMap creates a basic instance of the StringStringMap. It is _unsafe_ for concurrent access.
func NewStringStringMap() StringStringMap {
res := &baseStringStringMap{
_map: map[string]string{},
}
return res
}
// NewStringStringMapSyncronized creates a concurrent safe instance of the StringStringMap
func NewStringStringMapSyncronized() StringStringMap {
return &syncStringStringMap{
inner: NewStringStringMap(),
}
}
func (m *baseStringStringMap) Get(key string) (v string, found bool) {
v, found = m._map[key]
return
}
func (m *baseStringStringMap) Each(visitor StringStringMapVisitor) {
for k, v := range m._map {
if !visitor(k, v) {
return
}
}
}
func (m *baseStringStringMap) Set(key string, val string) {
m._map[key] = val
}
func (m *baseStringStringMap) Update(src map[string]string) StringStringMap {
for k, v := range src {
m._map[k] = v
}
return m
}
func (m *baseStringStringMap) Remove(key string) bool {
_, found := m._map[key]
delete(m._map, key)
return found
}
func (m *baseStringStringMap) Clone() StringStringMap {
res := NewStringStringMap()
for k, v := range m._map {
res.Set(k, v)
}
return res
}
type syncStringStringMap struct {
inner StringStringMap
mutex sync.RWMutex
}
func (m *syncStringStringMap) Each(visitor StringStringMapVisitor) {
m.mutex.RLock()
m.inner.Each(visitor)
m.mutex.RUnlock()
}
func (m *syncStringStringMap) Get(key string) (v string, found bool) {
m.mutex.RLock()
v, found = m.inner.Get(key)
m.mutex.RUnlock()
return
}
func (m *syncStringStringMap) Set(key string, val string) {
m.mutex.Lock()
m.inner.Set(key, val)
m.mutex.Unlock()
}
func (m *syncStringStringMap) Update(src map[string]string) StringStringMap {
m.mutex.Lock()
m.inner.Update(src)
m.mutex.Unlock()
return m
}
func (m *syncStringStringMap) Remove(key string) bool {
m.mutex.Lock()
found := m.inner.Remove(key)
m.mutex.Unlock()
return found
}
func (m *syncStringStringMap) Clone() StringStringMap {
m.mutex.RLock()
r := m.inner.Clone()
m.mutex.RUnlock()
return r
}