summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/data/MarkListIterator.java
blob: 725b050520af48d4ca12ca997e2c54115c9a11b2 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package bjc.data;

import java.util.*;

/**
 * ListIterator which allows navigation/marking of an iterator.
 * 
 * @author bjcul
 *
 * @param <E> The element type
 */
public class MarkListIterator<E> implements ListIterator<E> {
	private Iterator<E> backing;

	private List<E> cache;
	private Deque<Integer> marks;

	private int currIdx;
	private int maxIdx;

	/**
	 * Create a new marking list iterator.
	 * 
	 * @param backing The iterator which backs us.
	 */
	public MarkListIterator(Iterator<E> backing) {
		this.backing = backing;

		this.currIdx = 0;
		this.maxIdx = 0;

		this.cache = new ArrayList<>();
		this.marks = new ArrayDeque<>();
	}

	/**
	 * Create a new marking list iterator.
	 * 
	 * @param backing The iterable to get the backing iterator from.
	 */
	public MarkListIterator(Iterable<E> backing) {
		this(backing.iterator());
	}

	@Override
	public boolean hasNext() {
		if (currIdx < maxIdx)
			return true;
		return backing.hasNext();
	}

	@Override
	public E next() {
		if (currIdx < maxIdx) {
			return cache.get(currIdx++);
		}
		currIdx++;
		maxIdx++;
		E next = backing.next();
		cache.add(next);
		return next;
	}

	@Override
	public boolean hasPrevious() {
		return maxIdx > 0 && currIdx > 0;
	}

	@Override
	public E previous() {
		currIdx--;
		return cache.get(currIdx);
	}

	@Override
	public int nextIndex() {
		return currIdx + 1;
	}

	@Override
	public int previousIndex() {
		return currIdx - 1;
	}

	/**
	 * Mark the current position in the iterator.
	 */
	public void mark() {
		marks.push(currIdx);
	}

	/**
	 * Reset the iterator to the last position that was marked, leaving the mark in
	 * place.
	 * 
	 * @return Whether or not a rollback actually happened
	 */
	public boolean rollback() {
		return rollback(false);
	}

	/**
	 * Reset the iterator to the last position that was marked.
	 * 
	 * @param clearMark Whether to clear the mark being rolled back to.
	 *
	 * @return Whether or not a rollback actually happened
	 */
	public boolean rollback(boolean clearMark) {
		if (marks.isEmpty()) return false;
		
		if (clearMark) {
			currIdx = marks.pop();
		} else {
			currIdx = marks.peek();
		}
		return true;
	}

	/**
	 * Remove the last position that was marked.
	 * 
	 * @return The marked position
	 */
	public int commit() {
		return marks.pop();
	}

	/**
	 * Resets the cache state of this iterator.
	 * 
	 * Once this has been called, all of the previous elements and marks are
	 * discarded.
	 */
	public void reset() {
		this.cache = new ArrayList<>();
		this.marks = new ArrayDeque<>();

		this.currIdx = 0;
		this.maxIdx = 0;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException("Can't remove items");
	}

	@Override
	public void set(E e) {
		throw new UnsupportedOperationException("Can't set items");
	}

	@Override
	public void add(E e) {
		throw new UnsupportedOperationException("Can't add items");
	}

	/**
	 * Check if this iterator has at least one active mark.
	 * 
	 * @return Whether this iterator has any active marks.
	 */
	public boolean hasMark() {
		return !marks.isEmpty();
	}
}