-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyclic.go
68 lines (61 loc) · 1.73 KB
/
cyclic.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
package counter
import (
"fmt"
"sync"
)
const (
// MaxInt - holds maximum int value for your platform
MaxInt = int(^uint(0) >> 1)
)
// CyclicIncrementor - step-by-step counter with limitation of its maximum value.
// After maximum is reached counter will reset into zero.
// You should use NewCyclicIncrementor() to create counter, but also can create counter like this:
// c := &counter.CyclicIncrementor{}
// But in that case, counter is not operational until its maximum value will be set:
// err := c.SetMaxValue(max)
// Also note, if counter is only declared as pointer:
// var c *CyclicIncrementor
// it is not really initialized and it cannot be used at this point.
type CyclicIncrementor struct {
mx sync.RWMutex // for value and max
value int
max int
}
// GetValue - return counter value
func (c *CyclicIncrementor) GetValue() int {
c.mx.RLock()
defer c.mx.RUnlock()
return c.value
}
// Inc - increment by 1 current value of counter. When value is reached max, counter will reset into zero.
func (c *CyclicIncrementor) Inc() {
c.mx.Lock()
if c.value < c.max {
c.value++
} else {
c.value = 0
}
c.mx.Unlock()
}
// SetMaxValue - change max allowed value for counter.
// Only positive integers allowed to set max value.
func (c *CyclicIncrementor) SetMaxValue(max int) error {
if max < 0 {
return fmt.Errorf("counter.CyclicIncrementor: invalid max value (%d)", max)
}
c.mx.Lock()
if c.value > max {
c.value = 0
}
c.max = max
c.mx.Unlock()
return nil
}
// NewCyclicIncrementor - return new cyclic counter with preassigned maximum value equals to MaxInt.
func NewCyclicIncrementor() (*CyclicIncrementor, error) {
c := &CyclicIncrementor{}
if err := c.SetMaxValue(MaxInt); err != nil {
return nil, err
}
return c, nil
}