refactor: add interface to map

This commit is contained in:
Derrick Hammer 2024-01-07 03:57:12 -05:00
parent ca1e2dcf72
commit b340cda442
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 28 additions and 17 deletions

View File

@ -1,30 +1,41 @@
package structs package structs
import ( import (
"github.com/emirpasic/gods/maps"
"github.com/emirpasic/gods/maps/hashmap" "github.com/emirpasic/gods/maps/hashmap"
"log" "log"
"sync" "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 *hashmap.Map
mutex *sync.RWMutex mutex *sync.RWMutex
} }
func NewMap() *Map { func NewMap() Map {
return &Map{ return &MapImpl{
Map: hashmap.New(), Map: hashmap.New(),
mutex: &sync.RWMutex{}, 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() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
return m.Map.Get(key) 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) val, found := m.Get(key)
if !found { if !found {
@ -40,7 +51,7 @@ func (m *Map) GetInt(key interface{}) (value *int) {
return value return value
} }
func (m *Map) GetString(key interface{}) (value *string) { func (m *MapImpl) GetString(key interface{}) (value *string) {
val, found := m.Get(key) val, found := m.Get(key)
if !found { if !found {
@ -56,65 +67,65 @@ func (m *Map) GetString(key interface{}) (value *string) {
return return
} }
func (m *Map) Put(key interface{}, value interface{}) { func (m *MapImpl) Put(key interface{}, value interface{}) {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
m.Map.Put(key, value) 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) m.Put(key, value)
} }
func (m *Map) Remove(key interface{}) { func (m *MapImpl) Remove(key interface{}) {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
m.Map.Remove(key) m.Map.Remove(key)
} }
func (m *Map) Keys() []interface{} { func (m *MapImpl) Keys() []interface{} {
m.mutex.RLock() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
return m.Map.Keys() return m.Map.Keys()
} }
func (m *Map) Values() []interface{} { func (m *MapImpl) Values() []interface{} {
m.mutex.RLock() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
return m.Map.Values() return m.Map.Values()
} }
func (m *Map) Size() int { func (m *MapImpl) Size() int {
m.mutex.RLock() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
return m.Map.Size() return m.Map.Size()
} }
func (m *Map) Empty() bool { func (m *MapImpl) Empty() bool {
m.mutex.RLock() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
return m.Map.Empty() return m.Map.Empty()
} }
func (m *Map) Clear() { func (m *MapImpl) Clear() {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
m.Map.Clear() m.Map.Clear()
} }
func (m *Map) String() string { func (m *MapImpl) String() string {
m.mutex.RLock() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
return m.Map.String() 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() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
return m.Map.Get(value) return m.Map.Get(value)
} }
func (m *Map) Contains(value interface{}) bool { func (m *MapImpl) Contains(value interface{}) bool {
m.mutex.RLock() m.mutex.RLock()
defer m.mutex.RUnlock() defer m.mutex.RUnlock()
_, has := m.Map.Get(value) _, has := m.Map.Get(value)