-
Notifications
You must be signed in to change notification settings - Fork 1
/
float.go
61 lines (50 loc) · 1023 Bytes
/
float.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
package jaws
import (
"strconv"
"sync"
)
var _ FloatSetter = &Float{}
// Float wraps a mutex and a float64, and implements jaws.FloatSetter.
type Float struct {
mu sync.Mutex
Value float64
}
func (s *Float) Set(val float64) {
s.mu.Lock()
s.Value = val
s.mu.Unlock()
}
func (s *Float) Get() (val float64) {
s.mu.Lock()
val = s.Value
s.mu.Unlock()
return
}
func (s *Float) Swap(val float64) (old float64) {
s.mu.Lock()
old, s.Value = s.Value, val
s.mu.Unlock()
return
}
func (s *Float) String() string {
return strconv.FormatFloat(s.Get(), 'f', -1, 64)
}
func (s *Float) JawsGetFloat(*Element) float64 {
return s.Get()
}
func (s *Float) JawsSetFloat(e *Element, val float64) error {
if s.Swap(val) == val {
return ErrValueUnchanged
}
return nil
}
func (s *Float) MarshalJSON() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *Float) UnmarshalJSON(b []byte) (err error) {
var val float64
if val, err = strconv.ParseFloat(string(b), 64); err == nil {
s.Set(val)
}
return
}