Skip to content

Commit 599dcab

Browse files
committed
refactor: added more documentation
1 parent b9a966f commit 599dcab

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

safe_number.go

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,119 @@ import (
44
"sync"
55
)
66

7-
// SafeNumber is a concurrent-safe number
8-
type SafeNumber struct {
9-
mu sync.Mutex
10-
initialValue int
11-
value int
12-
}
7+
type (
8+
// SafeNumber is a concurrent-safe number
9+
SafeNumber struct {
10+
mu sync.Mutex
11+
initialValue int
12+
value int
13+
}
14+
)
1315

1416
// NewSafeNumber creates a new SafeNumber
17+
//
18+
// Parameters:
19+
//
20+
// - initialValue: the initial value of the number
21+
//
22+
// Returns:
23+
//
24+
// - *SafeNumber: the created SafeNumber
1525
func NewSafeNumber(initialValue int) *SafeNumber {
1626
return &SafeNumber{value: initialValue, initialValue: initialValue}
1727
}
1828

1929
// Increment increments the number by 1
2030
func (sn *SafeNumber) Increment() {
31+
if sn == nil {
32+
return
33+
}
2134
sn.mu.Lock()
2235
defer sn.mu.Unlock()
2336
sn.value++
2437
}
2538

2639
// Decrement decrements the number by 1
2740
func (sn *SafeNumber) Decrement() {
41+
if sn == nil {
42+
return
43+
}
2844
sn.mu.Lock()
2945
defer sn.mu.Unlock()
3046
sn.value--
3147
}
3248

3349
// GetValue returns the current value of the number
50+
//
51+
// Returns:
52+
//
53+
// - int: the current value of the number
3454
func (sn *SafeNumber) GetValue() int {
55+
if sn == nil {
56+
return 0
57+
}
3558
sn.mu.Lock()
3659
defer sn.mu.Unlock()
3760
return sn.value
3861
}
3962

4063
// IncrementAndGetValue increments the number by 1 and returns the new value
64+
//
65+
// Returns:
66+
//
67+
// - int: the new value of the number
4168
func (sn *SafeNumber) IncrementAndGetValue() int {
69+
if sn == nil {
70+
return 0
71+
}
4272
sn.mu.Lock()
4373
defer sn.mu.Unlock()
4474
sn.value++
4575
return sn.value
4676
}
4777

4878
// DecrementAndGetValue decrements the number by 1 and returns the new value
79+
//
80+
// Returns:
81+
//
82+
// - int: the new value of the number
4983
func (sn *SafeNumber) DecrementAndGetValue() int {
84+
if sn == nil {
85+
return 0
86+
}
5087
sn.mu.Lock()
5188
defer sn.mu.Unlock()
5289
sn.value--
5390
return sn.value
5491
}
5592

5693
// OperateValue performs an operation on the value of the number
94+
//
95+
// Parameters:
96+
//
97+
// - operation: a function that takes the current value and returns the new value
5798
func (sn *SafeNumber) OperateValue(operation func(int) int) {
99+
if sn == nil {
100+
return
101+
}
58102
sn.mu.Lock()
59103
defer sn.mu.Unlock()
60104
sn.value = operation(sn.value)
61105
}
62106

63107
// OperateAndGetValue performs an operation on the value of the number and returns the result
108+
//
109+
// Parameters:
110+
//
111+
// - operation: a function that takes the current value and returns the new value
112+
//
113+
// Returns:
114+
//
115+
// - int: the new value of the number
64116
func (sn *SafeNumber) OperateAndGetValue(operation func(int) int) int {
117+
if sn == nil {
118+
return 0
119+
}
65120
sn.mu.Lock()
66121
defer sn.mu.Unlock()
67122
sn.value = operation(sn.value)
@@ -70,6 +125,9 @@ func (sn *SafeNumber) OperateAndGetValue(operation func(int) int) int {
70125

71126
// ResetValue resets the value of the number to the initial value
72127
func (sn *SafeNumber) ResetValue() {
128+
if sn == nil {
129+
return
130+
}
73131
sn.mu.Lock()
74132
defer sn.mu.Unlock()
75133
sn.value = sn.initialValue

0 commit comments

Comments
 (0)