@Test public void testIterator() throws InterruptedException { CircularBlockingDeque<String> deque = new CircularBlockingDeque<String>(2); deque.addLast("unused"); String expectedString1 = "Hello, world!"; String expectedString2 = "Goodbye, world!"; deque.addLast(expectedString1); deque.addLast(expectedString2); Iterator<String> iterator = deque.iterator(); assertEquals(expectedString1, iterator.next()); assertEquals(expectedString2, iterator.next()); assertFalse(iterator.hasNext()); try { iterator.next(); fail(); } catch (NoSuchElementException e) { } deque.takeFirst(); iterator = deque.iterator(); assertEquals(expectedString2, iterator.next()); assertFalse(iterator.hasNext()); }
@Override public Iterator<T> iterator() { return new Iterator<T>() { int offset = 0; @Override public boolean hasNext() { return offset < length; } @Override public T next() { if (offset == length) { throw new NoSuchElementException(); } T entry = deque[(start + offset) % limit]; offset++; return entry; } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
CircularBlockingDeque implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { int offset = 0; @Override public boolean hasNext() { return offset < length; } @Override public T next() { if (offset == length) { throw new NoSuchElementException(); } T entry = deque[(start + offset) % limit]; offset++; return entry; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } }
CircularBlockingDeque implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { int offset = 0; @Override public boolean hasNext() { return offset < length; } @Override public T next() { if (offset == length) { throw new NoSuchElementException(); } T entry = deque[(start + offset) % limit]; offset++; return entry; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } @SuppressWarnings("unchecked")  CircularBlockingDeque(int capacity); }
CircularBlockingDeque implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { int offset = 0; @Override public boolean hasNext() { return offset < length; } @Override public T next() { if (offset == length) { throw new NoSuchElementException(); } T entry = deque[(start + offset) % limit]; offset++; return entry; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } @SuppressWarnings("unchecked")  CircularBlockingDeque(int capacity); boolean addLast(T entry); boolean addFirst(T entry); T takeFirst(); T peekFirst(); T takeLast(); T peekLast(); boolean isEmpty(); @Override Iterator<T> iterator(); }
CircularBlockingDeque implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { int offset = 0; @Override public boolean hasNext() { return offset < length; } @Override public T next() { if (offset == length) { throw new NoSuchElementException(); } T entry = deque[(start + offset) % limit]; offset++; return entry; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } @SuppressWarnings("unchecked")  CircularBlockingDeque(int capacity); boolean addLast(T entry); boolean addFirst(T entry); T takeFirst(); T peekFirst(); T takeLast(); T peekLast(); boolean isEmpty(); @Override Iterator<T> iterator();  }