-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ComodificationDebugArrayList.java
88 lines (75 loc) · 1.99 KB
/
ComodificationDebugArrayList.java
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
package me.nallar.tickthreading.collections;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ComodificationDebugArrayList<T> extends ArrayList<T> {
Throwable lastModification = null;
@Override
public void ensureCapacity(int minCapacity) {
super.ensureCapacity(minCapacity);
lastModification = new Throwable();
}
@Override
public T remove(int index) {
lastModification = new Throwable();
return super.remove(index);
}
@Override
public boolean remove(Object o) {
lastModification = new Throwable();
return super.remove(o);
}
@Override
public Iterator<T> iterator() {
return new DebugListIterator();
}
public class DebugListIterator implements Iterator<T> {
int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
@Override
public boolean hasNext() {
return cursor != size();
}
@Override
public T next() {
checkForComodification();
try {
T next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
@Override
public void remove() {
if (lastRet == -1) {
throw new IllegalStateException();
}
checkForComodification();
try {
ComodificationDebugArrayList.this.remove(lastRet);
if (lastRet < cursor) {
cursor--;
}
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
private void checkForComodification() {
if (modCount != expectedModCount) {
throw new ComodificationException(ComodificationDebugArrayList.this.lastModification);
}
}
}
public static class ComodificationException extends RuntimeException {
public ComodificationException(Throwable cause) {
super(cause);
}
}
}