aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation_iterator.go
blob: a576ddd656c4708d0061bd6e0b587946df8a54fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bug

type OperationIterator struct {
	bug       *Bug
	packIndex int
	opIndex   int
}

func NewOperationIterator(bug *Bug) *OperationIterator {
	return &OperationIterator{
		bug:       bug,
		packIndex: 0,
		opIndex:   -1,
	}
}

func (it *OperationIterator) Next() bool {
	// Special case of the staging area
	if it.packIndex == len(it.bug.Packs)+1 {
		pack := it.bug.Staging
		it.opIndex++
		return it.opIndex < len(pack.Operations)
	}

	if it.packIndex >= len(it.bug.Packs) {
		return false
	}

	pack := it.bug.Packs[it.packIndex]

	it.opIndex++

	if it.opIndex < len(pack.Operations) {
		return true
	}

	// Note: this iterator doesn't handle the empty pack case
	it.opIndex = 0
	it.packIndex++

	// Special case of the non-empty staging area
	if it.packIndex == len(it.bug.Packs)+1 && len(it.bug.Staging.Operations) > 0 {
		return true
	}

	return it.packIndex < len(it.bug.Packs)
}

func (it *OperationIterator) Value() Operation {
	// Special case of the staging area
	if it.packIndex == len(it.bug.Packs)+1 {
		pack := it.bug.Staging

		if it.opIndex >= len(pack.Operations) {
			panic("Iterator is not valid anymore")
		}

		return pack.Operations[it.opIndex]
	}

	if it.packIndex >= len(it.bug.Packs) {
		panic("Iterator is not valid anymore")
	}

	pack := it.bug.Packs[it.packIndex]

	if it.opIndex >= len(pack.Operations) {
		panic("Iterator is not valid anymore")
	}

	return pack.Operations[it.opIndex]
}