This commit is contained in:
crusader 2017-11-27 18:41:29 +09:00
parent ea2b6398a7
commit 2122e939e8

View File

@ -1,20 +1,28 @@
package context package context
import "sync"
type AttributeManager interface { type AttributeManager interface {
Init() Init()
SetAttribute(key string, value interface{}) SetAttribute(key string, value interface{})
GetAttribute(key string) (value interface{}) GetAttribute(key string) (value interface{})
GetAttributeNames() []string
RemoveAttribute(key string) RemoveAttribute(key string)
Contains(key string) (exist bool) Contains(key string) (exist bool)
Destroy() Destroy()
} }
type AttributeManagers struct { type AttributeManagers struct {
Parent AttributeManager
attributes map[string]interface{} attributes map[string]interface{}
mtx sync.RWMutex
} }
func (am *AttributeManagers) Init() { func (am *AttributeManagers) Init() {
if nil != am.Parent {
am.Parent.Init()
}
if nil == am.attributes { if nil == am.attributes {
am.Destroy() am.Destroy()
} }
@ -22,48 +30,76 @@ func (am *AttributeManagers) Init() {
} }
func (am *AttributeManagers) SetAttribute(key string, value interface{}) { func (am *AttributeManagers) SetAttribute(key string, value interface{}) {
if nil == am.attributes { am.checkInitialized()
am.attributes = make(map[string]interface{})
} am.mtx.Lock()
defer am.mtx.Unlock()
am.attributes[key] = value am.attributes[key] = value
} }
func (am *AttributeManagers) GetAttribute(key string) (value interface{}) { func (am *AttributeManagers) GetAttribute(key string) (value interface{}) {
if nil == am.attributes { am.checkInitialized()
return nil
}
return am.attributes[key]
}
func (am *AttributeManagers) GetAttributeNames() []string { am.mtx.RLock()
if nil == am.attributes { defer am.mtx.RUnlock()
return nil
} if _, ok := am.attributes[key]; ok {
keys := []string{} return am.attributes[key]
for k, _ := range am.attributes {
keys = append(keys, k)
} }
return keys if nil == am.Parent {
return nil
}
return am.Parent.GetAttribute(key)
} }
func (am *AttributeManagers) RemoveAttribute(key string) { func (am *AttributeManagers) RemoveAttribute(key string) {
if nil == am.attributes { am.checkInitialized()
am.mtx.Lock()
defer am.mtx.Unlock()
if _, ok := am.attributes[key]; ok {
delete(am.attributes, key)
return return
} }
delete(am.attributes, key)
if nil == am.Parent {
return
}
am.Parent.RemoveAttribute(key)
} }
func (am *AttributeManagers) Contains(key string) (exist bool) { func (am *AttributeManagers) Contains(key string) (exist bool) {
if nil == am.attributes { am.checkInitialized()
am.mtx.RLock()
defer am.mtx.RUnlock()
if _, ok := am.attributes[key]; ok {
return true
}
if nil == am.Parent {
return false return false
} }
_, ok := am.attributes[key] return am.Parent.Contains(key)
return ok
} }
func (am *AttributeManagers) Destroy() { func (am *AttributeManagers) Destroy() {
if nil != am.attributes { if nil != am.attributes {
am.attributes = nil am.attributes = nil
} }
if nil != am.Parent {
am.Parent.Destroy()
}
}
func (am *AttributeManagers) checkInitialized() {
if nil == am.attributes {
panic("Attribute Manager: must be initialized")
}
} }