-
Notifications
You must be signed in to change notification settings - Fork 1
/
value.go
70 lines (54 loc) · 989 Bytes
/
value.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
package async
import (
"sync"
)
func NewValue[T any]() *Value[T] {
return &Value[T]{}
}
type Value[T any] struct {
value T
nonempty bool
lock sync.RWMutex
}
func (r *Value[T]) Empty() bool {
r.lock.RLock()
defer r.lock.RUnlock()
return !r.nonempty
}
func (r *Value[T]) SetValue(val T) {
r.lock.Lock()
defer r.lock.Unlock()
r.value = val
r.nonempty = true
}
func (r *Value[T]) Value() T {
r.lock.RLock()
defer r.lock.RUnlock()
return r.value
}
func NewValues[T any]() *Values[T] {
return &Values[T]{}
}
type Values[T any] struct {
lock sync.Mutex
values []T
}
func (r *Values[T]) AddValue(val T) {
r.lock.Lock()
defer r.lock.Unlock()
r.values = append(r.values, val)
}
func (r *Values[T]) Values() []T {
r.lock.Lock()
defer r.lock.Unlock()
var values []T
for _, v := range r.values {
values = append(values, v)
}
return values
}
func (r *Values[T]) Empty() bool {
r.lock.Lock()
defer r.lock.Unlock()
return len(r.values) == 0
}