diff --git a/README.md b/README.md index 15e4c76..c8e627d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ cond := abool.New() // default to false cond.Set() // Sets to true cond.IsSet() // Returns true cond.UnSet() // Sets to false +cond.IsNotSet() // Returns true cond.SetTo(any) // Sets to whatever you want cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded cond.Toggle() // Inverts the boolean then returns the value before inverting diff --git a/bool.go b/bool.go index e894dab..d4977a2 100644 --- a/bool.go +++ b/bool.go @@ -38,6 +38,11 @@ func (ab *AtomicBool) IsSet() bool { return atomic.LoadInt32((*int32)(ab))&1 == 1 } +// IsNotSet returns whether the Boolean is false. +func (ab *AtomicBool) IsNotSet() bool { + return !ab.IsSet() +} + // SetTo sets the boolean with given Boolean. func (ab *AtomicBool) SetTo(yes bool) { if yes { diff --git a/bool_test.go b/bool_test.go index c35aa16..5469f3a 100644 --- a/bool_test.go +++ b/bool_test.go @@ -25,6 +25,10 @@ func TestBool(t *testing.T) { t.Fatal("Empty value of AtomicBool should be false") } + if v.IsSet() == v.IsNotSet() { + t.Fatal("AtomicBool.IsNotSet() should be the opposite of IsSet()") + } + v.Set() if !v.IsSet() { t.Fatal("AtomicBool.Set() failed") @@ -164,12 +168,18 @@ func TestRace(t *testing.T) { } func ExampleAtomicBool() { - cond := New() // default to false - cond.Set() // set to true - cond.IsSet() // returns true - cond.UnSet() // set to false - cond.SetTo(true) // set to whatever you want - cond.Toggle() // toggles the boolean value + cond := New() // default to false + any := true + old := any + new := !any + + cond.Set() // Sets to true + cond.IsSet() // Returns true + cond.UnSet() // Sets to false + cond.IsNotSet() // Returns true + cond.SetTo(any) // Sets to whatever you want + cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded + cond.Toggle() // Inverts the boolean then returns the value before inverting } // Benchmark Read