Make Set private.
authorAndrey Petrov <andrey.petrov@shazow.net>
Wed, 21 Jan 2015 00:39:07 +0000 (16:39 -0800)
committerAndrey Petrov <andrey.petrov@shazow.net>
Wed, 21 Jan 2015 00:44:35 +0000 (16:44 -0800)
chat/message/theme_test.go
chat/message/user_test.go
chat/room.go
chat/set.go
chat/set_test.go

index da0cd0a330075d249167595a8c05d02cd5bd8add..2a1509ba82d98e18d95e7e92e0e654c05e612e54 100644 (file)
@@ -54,7 +54,7 @@ func TestTheme(t *testing.T) {
                t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
        }
 
-       u := NewUser(testId("foo"))
+       u := NewUser(SimpleId("foo"))
        u.colorIdx = 4
        actual = colorTheme.ColorName(u)
        expected = "\033[38;05;4mfoo\033[0m"
index 6e5caeb654f7a8e1e105ae1e6d76b5b3dfa13c10..ffc8b28d1dfc9116719e736360db9fe6e2b3684e 100644 (file)
@@ -9,12 +9,12 @@ func TestMakeUser(t *testing.T) {
        var actual, expected []byte
 
        s := &MockScreen{}
-       u := NewUser(testId("foo"))
+       u := NewUser(SimpleId("foo"))
        m := NewAnnounceMsg("hello")
 
        defer u.Close()
        u.Send(m)
-       u.ConsumeOne(s)
+       u.HandleMsg(<-u.ConsumeChan(), s)
 
        s.Read(&actual)
        expected = []byte(m.String() + Newline)
index 11f0bb1f8b92970d151964eba98a50179c5708ef..2d7a9832b8bfe0d0f37818044f018ab79c48ad19 100644 (file)
@@ -30,7 +30,7 @@ type Member struct {
 type Room struct {
        topic     string
        history   *message.History
-       members   *Set
+       members   *idSet
        broadcast chan message.Message
        commands  Commands
        closed    bool
@@ -44,7 +44,7 @@ func NewRoom() *Room {
        return &Room{
                broadcast: broadcast,
                history:   message.NewHistory(historyLen),
-               members:   NewSet(),
+               members:   newIdSet(),
                commands:  *defaultCommands,
        }
 }
@@ -58,7 +58,7 @@ func (r *Room) SetCommands(commands Commands) {
 func (r *Room) Close() {
        r.closeOnce.Do(func() {
                r.closed = true
-               r.members.Each(func(m Item) {
+               r.members.Each(func(m identified) {
                        m.(*Member).Close()
                })
                r.members.Clear()
@@ -92,7 +92,7 @@ func (r *Room) HandleMsg(m message.Message) {
                }
 
                r.history.Add(m)
-               r.members.Each(func(u Item) {
+               r.members.Each(func(u identified) {
                        user := u.(*Member).User
                        if skip && skipUser == user {
                                // Skip
index ed3c09f291b0d389a44385dd0e5bfb8d9f1009c9..b7ec5b3aacfbf5270983733766b7af580e111ac2 100644 (file)
@@ -10,43 +10,43 @@ import (
 var ErrIdTaken = errors.New("id already taken")
 
 // The error returned when a requested item does not exist in the set.
-var ErrItemMissing = errors.New("item does not exist")
+var ErridentifiedMissing = errors.New("item does not exist")
 
 // Interface for an item storeable in the set
-type Item interface {
+type identified interface {
        Id() string
 }
 
 // Set with string lookup.
 // TODO: Add trie for efficient prefix lookup?
-type Set struct {
-       lookup map[string]Item
+type idSet struct {
+       lookup map[string]identified
        sync.RWMutex
 }
 
-// NewSet creates a new set.
-func NewSet() *Set {
-       return &Set{
-               lookup: map[string]Item{},
+// newIdSet creates a new set.
+func newIdSet() *idSet {
+       return &idSet{
+               lookup: map[string]identified{},
        }
 }
 
 // Clear removes all items and returns the number removed.
-func (s *Set) Clear() int {
+func (s *idSet) Clear() int {
        s.Lock()
        n := len(s.lookup)
-       s.lookup = map[string]Item{}
+       s.lookup = map[string]identified{}
        s.Unlock()
        return n
 }
 
 // Len returns the size of the set right now.
-func (s *Set) Len() int {
+func (s *idSet) Len() int {
        return len(s.lookup)
 }
 
 // In checks if an item exists in this set.
-func (s *Set) In(item Item) bool {
+func (s *idSet) In(item identified) bool {
        s.RLock()
        _, ok := s.lookup[item.Id()]
        s.RUnlock()
@@ -54,20 +54,20 @@ func (s *Set) In(item Item) bool {
 }
 
 // Get returns an item with the given Id.
-func (s *Set) Get(id string) (Item, error) {
+func (s *idSet) Get(id string) (identified, error) {
        s.RLock()
        item, ok := s.lookup[id]
        s.RUnlock()
 
        if !ok {
-               return nil, ErrItemMissing
+               return nil, ErridentifiedMissing
        }
 
        return item, nil
 }
 
 // Add item to this set if it does not exist already.
-func (s *Set) Add(item Item) error {
+func (s *idSet) Add(item identified) error {
        s.Lock()
        defer s.Unlock()
 
@@ -81,21 +81,21 @@ func (s *Set) Add(item Item) error {
 }
 
 // Remove item from this set.
-func (s *Set) Remove(item Item) error {
+func (s *idSet) Remove(item identified) error {
        s.Lock()
        defer s.Unlock()
        id := item.Id()
        _, found := s.lookup[id]
        if !found {
-               return ErrItemMissing
+               return ErridentifiedMissing
        }
        delete(s.lookup, id)
        return nil
 }
 
-// Replace item from old id with new Item.
-// Used for moving the same Item to a new Id, such as a rename.
-func (s *Set) Replace(oldId string, item Item) error {
+// Replace item from old id with new identified.
+// Used for moving the same identified to a new Id, such as a rename.
+func (s *idSet) Replace(oldId string, item identified) error {
        s.Lock()
        defer s.Unlock()
 
@@ -108,11 +108,11 @@ func (s *Set) Replace(oldId string, item Item) error {
        // Remove oldId
        _, found = s.lookup[oldId]
        if !found {
-               return ErrItemMissing
+               return ErridentifiedMissing
        }
        delete(s.lookup, oldId)
 
-       // Add new Item
+       // Add new identified
        s.lookup[item.Id()] = item
 
        return nil
@@ -120,7 +120,7 @@ func (s *Set) Replace(oldId string, item Item) error {
 
 // Each loops over every item while holding a read lock and applies fn to each
 // element.
-func (s *Set) Each(fn func(item Item)) {
+func (s *idSet) Each(fn func(item identified)) {
        s.RLock()
        for _, item := range s.lookup {
                fn(item)
@@ -129,8 +129,8 @@ func (s *Set) Each(fn func(item Item)) {
 }
 
 // ListPrefix returns a list of items with a prefix, case insensitive.
-func (s *Set) ListPrefix(prefix string) []Item {
-       r := []Item{}
+func (s *idSet) ListPrefix(prefix string) []identified {
+       r := []identified{}
        prefix = strings.ToLower(prefix)
 
        s.RLock()
index c70ef2e298dbba2bf0fc3e7a62802fdd9fc4c7a6..6db2e9b2f2ec69f0563fe72e0e8e408fb5743f9a 100644 (file)
@@ -8,7 +8,7 @@ import (
 
 func TestSet(t *testing.T) {
        var err error
-       s := NewSet()
+       s := newIdSet()
        u := message.NewUser(message.SimpleId("foo"))
 
        if s.In(u) {