Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 1 | package stats |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // NewString creates a new String StatsObject with the given name and |
| 9 | // returns a pointer to it. |
| 10 | func NewString(name string) *String { |
| 11 | lock.Lock() |
| 12 | defer lock.Unlock() |
| 13 | |
| 14 | node := findNodeLocked(name, true) |
| 15 | s := String{value: ""} |
| 16 | node.object = &s |
| 17 | return &s |
| 18 | } |
| 19 | |
| 20 | // String implements the StatsObject interface. |
| 21 | type String struct { |
| 22 | mu sync.RWMutex |
| 23 | lastUpdate time.Time |
| 24 | value string |
| 25 | } |
| 26 | |
| 27 | // Set sets the value of the object. |
| 28 | func (s *String) Set(value string) { |
| 29 | s.mu.Lock() |
| 30 | defer s.mu.Unlock() |
| 31 | s.lastUpdate = time.Now() |
| 32 | s.value = value |
| 33 | } |
| 34 | |
| 35 | // LastUpdate returns the time at which the object was last updated. |
Robin Thellend | e6dcc5d | 2014-09-17 15:48:00 -0700 | [diff] [blame] | 36 | func (s *String) LastUpdate() time.Time { |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 37 | s.mu.RLock() |
| 38 | defer s.mu.RUnlock() |
| 39 | return s.lastUpdate |
| 40 | } |
| 41 | |
| 42 | // Value returns the current value of the object. |
Robin Thellend | e6dcc5d | 2014-09-17 15:48:00 -0700 | [diff] [blame] | 43 | func (s *String) Value() interface{} { |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 44 | s.mu.RLock() |
| 45 | defer s.mu.RUnlock() |
| 46 | return s.value |
| 47 | } |