blob: ad7fa356fbb79b2fcf9521f7b112ec1921ecfff4 [file] [log] [blame]
Robin Thellendac59e972014-08-19 18:26:11 -07001package stats
2
3import (
4 "sync"
5 "time"
6)
7
8// NewString creates a new String StatsObject with the given name and
9// returns a pointer to it.
10func 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.
21type String struct {
22 mu sync.RWMutex
23 lastUpdate time.Time
24 value string
25}
26
27// Set sets the value of the object.
28func (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 Thellende6dcc5d2014-09-17 15:48:00 -070036func (s *String) LastUpdate() time.Time {
Robin Thellendac59e972014-08-19 18:26:11 -070037 s.mu.RLock()
38 defer s.mu.RUnlock()
39 return s.lastUpdate
40}
41
42// Value returns the current value of the object.
Robin Thellende6dcc5d2014-09-17 15:48:00 -070043func (s *String) Value() interface{} {
Robin Thellendac59e972014-08-19 18:26:11 -070044 s.mu.RLock()
45 defer s.mu.RUnlock()
46 return s.value
47}