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