-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ListIteratorRemoveQueue.java
99 lines (83 loc) · 2.15 KB
/
ListIteratorRemoveQueue.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
89
90
91
92
93
94
95
96
97
98
99
package me.nallar.tickthreading.collections;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.ConcurrentLinkedQueue;
@SuppressWarnings ("ConstantConditions")
class ListIteratorRemoveQueue<T> extends ConcurrentLinkedQueue<T> implements List<T> {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private T next;
@Override
public boolean hasNext() {
next = ListIteratorRemoveQueue.this.peek();
return next != null;
}
@Override
public T next() {
if (next == null) {
throw new IllegalStateException("iterator.next() called without .hasNext()");
}
return next;
}
@Override
public void remove() {
if (ListIteratorRemoveQueue.this.poll() != next) {
throw new IllegalStateException("Already removed");
}
}
};
}
@Override
public boolean addAll(Collection<? extends T> c) {
return c == this || super.addAll(c);
}
@Override
public boolean addAll(int index, Collection c) {
throw new UnsupportedOperationException();
}
@Override
public T get(int index) {
if (index == 0) {
return this.peek();
}
throw new UnsupportedOperationException();
}
@Override
public T set(int index, Object element) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Object element) {
throw new UnsupportedOperationException();
}
@Override
public T remove(int index) {
if (index == 0) {
return this.poll();
}
throw new UnsupportedOperationException();
}
@Override
public int indexOf(Object o) {
throw new UnsupportedOperationException();
}
@Override
public int lastIndexOf(Object o) {
throw new UnsupportedOperationException();
}
@Override
public ListIterator<T> listIterator() {
throw new UnsupportedOperationException();
}
@Override
public ListIterator<T> listIterator(int index) {
throw new UnsupportedOperationException();
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
}
}