state.go 555 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package neighbors
  2. type (
  3. State interface {
  4. Present() bool
  5. Changed() bool
  6. Set(present bool)
  7. Reset()
  8. }
  9. state struct {
  10. present, was, initial bool
  11. }
  12. )
  13. func NewState() State {
  14. return &state{initial: true}
  15. }
  16. func (s *state) Present() bool {
  17. return s.present
  18. }
  19. func (s *state) Changed() bool {
  20. return s.present != s.was
  21. }
  22. func (s *state) Set(present bool) {
  23. if s.initial {
  24. s.was = !present
  25. s.present = present
  26. s.initial = false
  27. } else {
  28. s.was = s.present
  29. s.present = present
  30. }
  31. }
  32. func (s *state) Reset() {
  33. s.initial = true
  34. }