gime-lib/timeentry_collection.go

91 lines
2.2 KiB
Go
Raw Normal View History

2017-07-27 15:27:10 +00:00
package gime
import "fmt"
// TimeEntryCollection is a collection of Time Entries for gomobile bindings
type TimeEntryCollection struct {
list []TimeEntry
}
2017-07-28 11:44:08 +00:00
// Load a TimeEntry collection from a database
func (gdb *GimeDB) LoadTimeEntryCollection(tp string) *TimeEntryCollection {
ret := new(TimeEntryCollection)
entries := gdb.dbGetAllTimeEntries(tp)
for i := range entries {
ret.Push(&entries[i])
}
return ret
}
2017-07-27 15:27:10 +00:00
// Length returns how many time entries are in the collection
func (tc *TimeEntryCollection) Length() int {
return len(tc.list)
}
// Get returns the TimeEntry at idx or a nil entry
func (tc *TimeEntryCollection) Get(idx int) *TimeEntry {
if idx <= tc.Length() {
return &tc.list[idx]
}
return nil
}
// Clear empties the list of TimeEntries
func (tc *TimeEntryCollection) Clear() {
tc.list = tc.list[:0]
}
// Index finds the index of the given TimeEntry or -1 if not found
func (tc *TimeEntryCollection) Index(t *TimeEntry) int {
for i, tst := range tc.list {
if tst.Equals(t) {
return i
}
}
return -1
}
// Insert inserts a TimeEntry into the collection at i
func (tc *TimeEntryCollection) Insert(i int, t *TimeEntry) {
if i < 0 || i > tc.Length() {
fmt.Println("gime: Attempted to insert time entry at invalid index")
}
tc.list = append(tc.list, TimeEntry{})
copy(tc.list[i+1:], tc.list[i:])
tc.list[i] = *t
}
// Remove removes the TimeEntry at i from the collection
func (tc *TimeEntryCollection) Remove(i int) {
if i < 0 || i >= tc.Length() {
fmt.Println("gime: Attempted to remove time entry at invalid index")
}
copy(tc.list[i:], tc.list[i+1:])
tc.list[len(tc.list)-1] = TimeEntry{}
tc.list = tc.list[:len(tc.list)-1]
}
// Push adds an element to the end of the collection
func (tc *TimeEntryCollection) Push(t *TimeEntry) {
tc.Insert(tc.Length(), t)
}
// Pop removes the last element from the collection
func (tc *TimeEntryCollection) Pop() *TimeEntry {
ret := tc.list[tc.Length()-1]
tc.Remove(tc.Length() - 1)
return &ret
}
// Unshift adds an element to the front of the collection
func (tc *TimeEntryCollection) Unshift(t *TimeEntry) {
tc.Insert(0, t)
}
// Shift removes an element from the front of the collection
func (tc *TimeEntryCollection) Shift(t *TimeEntry) *TimeEntry {
ret := tc.list[0]
tc.Remove(0)
return &ret
}