2023-07-05 22:02:16 +02:00

61 lines
1.2 KiB
Go

package ocean
import "sync"
type Collection[T any] interface {
All() <-chan T
Delete(key string)
Exists(key string) bool
Get(key string) (value T, ok bool)
Set(key string, value T)
}
// collection is a hash map of homogeneous data.
type collection[T any] struct {
data sync.Map
name string
}
// NewCollection creates a new collection with the given name.
func NewCollection[T any](name string) *collection[T] {
return &collection[T]{name: name}
}
// Get returns the value for the given key.
func (c *collection[T]) Get(key string) (T, bool) {
value, exists := c.data.Load(key)
return value.(T), exists
}
// Set sets the value for the given key.
func (c *collection[T]) Set(key string, value T) {
c.data.Store(key, value)
}
// Delete deletes a key from the collection.
func (c *collection[T]) Delete(key string) {
c.data.Delete(key)
}
// Exists returns whether or not the key exists.
func (c *collection[T]) Exists(key string) bool {
_, exists := c.data.Load(key)
return exists
}
// All returns a channel of all objects in the collection.
func (c *collection[T]) All() <-chan T {
channel := make(chan T)
go func() {
c.data.Range(func(key, value any) bool {
channel <- value.(T)
return true
})
close(channel)
}()
return channel
}