Move server tracking in program into global memory state
This commit is contained in:
39
server/collection.go
Normal file
39
server/collection.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package server
|
||||
|
||||
type Collection struct {
|
||||
items []*Server
|
||||
}
|
||||
|
||||
// Return all of the items in the collection.
|
||||
func (c *Collection) All() []*Server {
|
||||
return c.items
|
||||
}
|
||||
|
||||
// Adds an item to the collection store.
|
||||
func (c *Collection) Add(s *Server) {
|
||||
c.items = append(c.items, s)
|
||||
}
|
||||
|
||||
// Returns only those items matching the filter criteria.
|
||||
func (c *Collection) Filter(filter func (*Server) bool) []*Server {
|
||||
r := make([]*Server, 0)
|
||||
for _, v := range c.items {
|
||||
if filter(v) {
|
||||
r = append(r, v)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// Returns a single element from the collection matching the filter. If nothing is
|
||||
// found a nil result is returned.
|
||||
func (c *Collection) Find(filter func (*Server) bool) *Server {
|
||||
for _, v := range c.items {
|
||||
if filter(v) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user