-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bounds.go
69 lines (52 loc) · 1.54 KB
/
Bounds.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
// Copyright (c) 2019 Australian Rivers Institute.
package variable
import (
"fmt"
strings2 "strings"
"github.com/LindsayBradford/crem/pkg/strings"
)
type Bounded interface {
WithinBounds(value float64) bool
BoundErrorAsText(value float64) string
}
var converter = strings.NewConverter().Localised().WithFloatingPointPrecision(6).PaddingZeros()
var _ Bounded = new(Bounds)
type Bounds struct {
// hasMinimum bool
// minimum float64
hasMaximum bool
maximum float64
}
// func (vb *Bounds) SetMinimum(minimum float64) {
// vb.hasMinimum = true
// vb.minimum = minimum
// }
func (vb *Bounds) SetMaximum(maximum float64) {
vb.hasMaximum = true
vb.maximum = maximum
}
func (vb *Bounds) WithinBounds(value float64) bool {
// if vb.hasMinimum && value < vb.minimum {
// return false
// }
if vb.hasMaximum && value > vb.maximum {
return false
}
return true
}
func (vb *Bounds) BoundErrorAsText(value float64) string {
boundMessages := make([]string, 0)
// if vb.hasMinimum && value < vb.minimum {
// lowerBoundAsString := converter.Convert(vb.minimum)
// boundMessages = append(boundMessages, fmt.Sprintf("< lower bound %s", lowerBoundAsString))
// }
if vb.hasMaximum && value > vb.maximum {
upperBoundAsString := converter.Convert(vb.maximum)
boundMessages = append(boundMessages, fmt.Sprintf("> upper bound %s", upperBoundAsString))
}
if len(boundMessages) > 0 {
boundMessagesAsString := strings2.Join(boundMessages, ", ")
return fmt.Sprintf("%s %s", converter.Convert(value), boundMessagesAsString)
}
return ""
}