operation_iterator.go

 1package bug
 2
 3type OperationIterator struct {
 4	bug       *Bug
 5	packIndex int
 6	opIndex   int
 7}
 8
 9func NewOperationIterator(bug Interface) *OperationIterator {
10	return &OperationIterator{
11		bug:       bugFromInterface(bug),
12		packIndex: 0,
13		opIndex:   -1,
14	}
15}
16
17func (it *OperationIterator) Next() bool {
18	// Special case of the staging area
19	if it.packIndex == len(it.bug.packs) {
20		pack := it.bug.staging
21		it.opIndex++
22		return it.opIndex < len(pack.Operations)
23	}
24
25	if it.packIndex >= len(it.bug.packs) {
26		return false
27	}
28
29	pack := it.bug.packs[it.packIndex]
30
31	it.opIndex++
32
33	if it.opIndex < len(pack.Operations) {
34		return true
35	}
36
37	// Note: this iterator doesn't handle the empty pack case
38	it.opIndex = 0
39	it.packIndex++
40
41	// Special case of the non-empty staging area
42	if it.packIndex == len(it.bug.packs) && len(it.bug.staging.Operations) > 0 {
43		return true
44	}
45
46	return it.packIndex < len(it.bug.packs)
47}
48
49func (it *OperationIterator) Value() Operation {
50	// Special case of the staging area
51	if it.packIndex == len(it.bug.packs) {
52		pack := it.bug.staging
53
54		if it.opIndex >= len(pack.Operations) {
55			panic("Iterator is not valid anymore")
56		}
57
58		return pack.Operations[it.opIndex]
59	}
60
61	if it.packIndex >= len(it.bug.packs) {
62		panic("Iterator is not valid anymore")
63	}
64
65	pack := it.bug.packs[it.packIndex]
66
67	if it.opIndex >= len(pack.Operations) {
68		panic("Iterator is not valid anymore")
69	}
70
71	return pack.Operations[it.opIndex]
72}