| 30 | /// |
| 31 | /// 1.6 |
| 32 | public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E> { |
| 33 | |
| 34 | private static final int DEFAULT_SIZE = 16; |
| 35 | |
| 36 | private enum DequeStatus { |
| 37 | Empty, Normal, Full; |
| 38 | } |
| 39 | |
| 40 | private transient DequeStatus status; |
| 41 | |
| 42 | private transient int modCount; |
| 43 | |
| 44 | // the pointer of the head element |
| 45 | private transient int front; |
| 46 | |
| 47 | // the pointer of the "next" position of the tail element |
| 48 | private transient int rear; |
| 49 | |
| 50 | private transient E[] elements; |
| 51 | |
| 52 | @SuppressWarnings("hiding") |
| 53 | private class ArrayDequeIterator<E> implements Iterator<E> { |
| 54 | private int pos; |
| 55 | |
| 56 | private final int expectedModCount; |
| 57 | |
| 58 | private boolean canRemove; |
| 59 | |
| 60 | @SuppressWarnings("synthetic-access") |
| 61 | ArrayDequeIterator() { |
| 62 | super(); |
| 63 | pos = front; |
| 64 | expectedModCount = modCount; |
| 65 | canRemove = false; |
| 66 | } |
| 67 | |
| 68 | @SuppressWarnings("synthetic-access") |
| 69 | public boolean hasNext() { |
| 70 | if (expectedModCount != modCount) { |
| 71 | return false; |
| 72 | } |
| 73 | return hasNextInternal(); |
| 74 | } |
| 75 | |
| 76 | private boolean hasNextInternal() { |
| 77 | // canRemove means "next" method is called, and the Full |
| 78 | // status can ensure that this method is not called just |
| 79 | // after "remove" method is call.(so, canRemove can keep |
| 80 | // true after "next" method called) |
| 81 | return (pos != rear) |
| 82 | || ((status == DequeStatus.Full) && !canRemove); |
| 83 | } |
| 84 | |
| 85 | @SuppressWarnings( { "synthetic-access", "unchecked" }) |
| 86 | public E next() { |
| 87 | if (hasNextInternal()) { |
| 88 | E result = (E) elements[pos]; |
| 89 | if (expectedModCount == modCount && null != result) { |
nothing calls this directly
no outgoing calls
no test coverage detected