blob: 3162ce0879b80665947bc8e36021315f3920ba90 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// 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 Thellendac59e972014-08-19 18:26:11 -07005package stats
6
7import (
8 "sync"
9 "time"
10)
11
12// NewString creates a new String StatsObject with the given name and
13// returns a pointer to it.
14func 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.
25type String struct {
26 mu sync.RWMutex
27 lastUpdate time.Time
28 value string
29}
30
31// Set sets the value of the object.
32func (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 Thellende6dcc5d2014-09-17 15:48:00 -070040func (s *String) LastUpdate() time.Time {
Robin Thellendac59e972014-08-19 18:26:11 -070041 s.mu.RLock()
42 defer s.mu.RUnlock()
43 return s.lastUpdate
44}
45
46// Value returns the current value of the object.
Robin Thellende6dcc5d2014-09-17 15:48:00 -070047func (s *String) Value() interface{} {
Robin Thellendac59e972014-08-19 18:26:11 -070048 s.mu.RLock()
49 defer s.mu.RUnlock()
50 return s.value
51}