Skip to content

Commit 5928530

Browse files
author
aliwoto
committed
Add SafeSet method to NumIdGenerator type.
Signed-off-by: aliwoto <[email protected]>
1 parent c66407d commit 5928530

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

ssg/methods.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,19 @@ func (n *NumIdGenerator[T]) Set(current T) {
15131513
n.current = current
15141514
}
15151515

1516+
// SafeSet method will set the current value to the given value
1517+
// if and only if the passed value is more than current value of
1518+
// the generator.
1519+
// please do notice that with calling n.Next() method, you will get
1520+
// the next id and not the current value.
1521+
func (n *NumIdGenerator[T]) SafeSet(current T) {
1522+
n.mut.Lock()
1523+
defer n.mut.Unlock()
1524+
if current > n.current {
1525+
n.current = current
1526+
}
1527+
}
1528+
15161529
// Reset method will set the current value to 0.
15171530
func (n *NumIdGenerator[T]) Reset() {
15181531
n.mut.Lock()

0 commit comments

Comments
 (0)