diff --git a/structs/map.go b/structs/map.go index 0e6b125..a360650 100644 --- a/structs/map.go +++ b/structs/map.go @@ -1,30 +1,41 @@ package structs import ( + "github.com/emirpasic/gods/maps" "github.com/emirpasic/gods/maps/hashmap" "log" "sync" ) -type Map struct { +var _ maps.Map = (*MapImpl)(nil) + +type Map interface { + GetInt(key interface{}) (value *int) + GetString(key interface{}) (value *string) + PutInt(key interface{}, value int) + Contains(value interface{}) bool + maps.Map +} + +type MapImpl struct { *hashmap.Map mutex *sync.RWMutex } -func NewMap() *Map { - return &Map{ +func NewMap() Map { + return &MapImpl{ Map: hashmap.New(), mutex: &sync.RWMutex{}, } } -func (m *Map) Get(key interface{}) (value interface{}, found bool) { +func (m *MapImpl) Get(key interface{}) (value interface{}, found bool) { m.mutex.RLock() defer m.mutex.RUnlock() return m.Map.Get(key) } -func (m *Map) GetInt(key interface{}) (value *int) { +func (m *MapImpl) GetInt(key interface{}) (value *int) { val, found := m.Get(key) if !found { @@ -40,7 +51,7 @@ func (m *Map) GetInt(key interface{}) (value *int) { return value } -func (m *Map) GetString(key interface{}) (value *string) { +func (m *MapImpl) GetString(key interface{}) (value *string) { val, found := m.Get(key) if !found { @@ -56,65 +67,65 @@ func (m *Map) GetString(key interface{}) (value *string) { return } -func (m *Map) Put(key interface{}, value interface{}) { +func (m *MapImpl) Put(key interface{}, value interface{}) { m.mutex.Lock() defer m.mutex.Unlock() m.Map.Put(key, value) } -func (m *Map) PutInt(key interface{}, value int) { +func (m *MapImpl) PutInt(key interface{}, value int) { m.Put(key, value) } -func (m *Map) Remove(key interface{}) { +func (m *MapImpl) Remove(key interface{}) { m.mutex.Lock() defer m.mutex.Unlock() m.Map.Remove(key) } -func (m *Map) Keys() []interface{} { +func (m *MapImpl) Keys() []interface{} { m.mutex.RLock() defer m.mutex.RUnlock() return m.Map.Keys() } -func (m *Map) Values() []interface{} { +func (m *MapImpl) Values() []interface{} { m.mutex.RLock() defer m.mutex.RUnlock() return m.Map.Values() } -func (m *Map) Size() int { +func (m *MapImpl) Size() int { m.mutex.RLock() defer m.mutex.RUnlock() return m.Map.Size() } -func (m *Map) Empty() bool { +func (m *MapImpl) Empty() bool { m.mutex.RLock() defer m.mutex.RUnlock() return m.Map.Empty() } -func (m *Map) Clear() { +func (m *MapImpl) Clear() { m.mutex.Lock() defer m.mutex.Unlock() m.Map.Clear() } -func (m *Map) String() string { +func (m *MapImpl) String() string { m.mutex.RLock() defer m.mutex.RUnlock() return m.Map.String() } -func (m *Map) GetKey(value interface{}) (key interface{}, found bool) { +func (m *MapImpl) GetKey(value interface{}) (key interface{}, found bool) { m.mutex.RLock() defer m.mutex.RUnlock() return m.Map.Get(value) } -func (m *Map) Contains(value interface{}) bool { +func (m *MapImpl) Contains(value interface{}) bool { m.mutex.RLock() defer m.mutex.RUnlock() _, has := m.Map.Get(value)