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