This commit is contained in:
crusader 2017-11-27 19:51:03 +09:00
parent cd79287e2d
commit 0eefc4bd27
2 changed files with 90 additions and 104 deletions

View File

@ -1,104 +0,0 @@
package context
import "sync"
type AttributeManager interface {
Attributes() Attributes
}
type Attributes interface {
initialize()
Set(key string, value interface{})
Get(key string) (value interface{})
Remove(key string)
Contains(key string) (exist bool)
}
func NewAttributes(parent Attributes) Attributes {
am := &attributes{
Parent: parent,
}
am.initialize()
return am
}
type attributes struct {
Parent Attributes
values map[string]interface{}
mtx sync.RWMutex
}
func (am *attributes) initialize() {
if nil != am.Parent {
am.Parent.initialize()
}
am.values = make(map[string]interface{})
}
func (am *attributes) Set(key string, value interface{}) {
am.checkInitialized()
am.mtx.Lock()
defer am.mtx.Unlock()
am.values[key] = value
}
func (am *attributes) Get(key string) (value interface{}) {
am.checkInitialized()
am.mtx.RLock()
defer am.mtx.RUnlock()
if _, ok := am.values[key]; ok {
return am.values[key]
}
if nil == am.Parent {
return nil
}
return am.Parent.Get(key)
}
func (am *attributes) Remove(key string) {
am.checkInitialized()
am.mtx.Lock()
defer am.mtx.Unlock()
if _, ok := am.values[key]; ok {
delete(am.values, key)
return
}
if nil == am.Parent {
return
}
am.Parent.Remove(key)
}
func (am *attributes) Contains(key string) (exist bool) {
am.checkInitialized()
am.mtx.RLock()
defer am.mtx.RUnlock()
if _, ok := am.values[key]; ok {
return true
}
if nil == am.Parent {
return false
}
return am.Parent.Contains(key)
}
func (am *attributes) checkInitialized() {
if nil == am.values {
panic("Attribute Manager: must be initialized")
}
}

View File

@ -1,7 +1,97 @@
package context
import "sync"
type ContextKey string
func (c ContextKey) String() string {
return string(c)
}
func NewContext(parent Context) Context {
c := &defaultContext{
parent: parent,
}
c.attributes = make(map[string]interface{})
return c
}
type Context interface {
SetAttribute(key string, value interface{})
GetAttribute(key string) (value interface{})
RemoveAttribute(key string)
ContainsAttribute(key string) (exist bool)
}
type defaultContext struct {
parent Context
attributes map[string]interface{}
mtx sync.RWMutex
}
func (dc *defaultContext) SetAttribute(key string, value interface{}) {
dc.checkInitialized()
dc.mtx.Lock()
defer dc.mtx.Unlock()
dc.attributes[key] = value
}
func (dc *defaultContext) GetAttribute(key string) (value interface{}) {
dc.checkInitialized()
dc.mtx.RLock()
defer dc.mtx.RUnlock()
if _, ok := dc.attributes[key]; ok {
return dc.attributes[key]
}
if nil == dc.parent {
return nil
}
return dc.parent.GetAttribute(key)
}
func (dc *defaultContext) RemoveAttribute(key string) {
dc.checkInitialized()
dc.mtx.Lock()
defer dc.mtx.Unlock()
if _, ok := dc.attributes[key]; ok {
delete(dc.attributes, key)
return
}
if nil == dc.parent {
return
}
dc.parent.RemoveAttribute(key)
}
func (dc *defaultContext) ContainsAttribute(key string) (exist bool) {
dc.checkInitialized()
dc.mtx.RLock()
defer dc.mtx.RUnlock()
if _, ok := dc.attributes[key]; ok {
return true
}
if nil == dc.parent {
return false
}
return dc.parent.ContainsAttribute(key)
}
func (dc *defaultContext) checkInitialized() {
if nil == dc.attributes {
panic("Attribute Manager: must be initialized")
}
}