Added an implemention of ArrayDeque, as well as unit tests

I also used this opportunity to reduce code duplication around other queue/deque implementations.
This commit is contained in:
Mike Jensen
2014-03-18 19:45:00 -06:00
parent e9e365d698
commit b5d388a718
10 changed files with 899 additions and 251 deletions

View File

@ -1,13 +1,12 @@
package java.util.concurrent;
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Queue;
import avian.Atomic;
public class ConcurrentLinkedQueue<T> implements Queue<T> {
public class ConcurrentLinkedQueue<T> extends AbstractQueue<T> {
private static final long QueueHead;
private static final long QueueTail;
private static final long NodeNext;
@ -67,31 +66,11 @@ public class ConcurrentLinkedQueue<T> implements Queue<T> {
return poll(false);
}
@Override
public T element() {
T result = peek();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
@Override
public T poll() {
return poll(true);
}
@Override
public T remove() {
T result = poll();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
private T poll(boolean remove) {
while (true) {
Node<T> h = head;
@ -150,12 +129,6 @@ public class ConcurrentLinkedQueue<T> implements Queue<T> {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends T> collection) {
// TODO - implement
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object element) {
// TODO - implement

View File

@ -1,11 +1,22 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
public class LinkedBlockingQueue<T> extends AbstractQueue<T>
implements BlockingQueue<T> {
private final Object collectionLock;
private final LinkedList<T> storage;
private final int capacity;
@ -84,15 +95,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public boolean add(T element) {
if (! offer(element)) {
throw new IllegalStateException("At capacity");
}
return true;
}
@Override
public boolean offer(T element) {
synchronized (collectionLock) {
@ -158,17 +160,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public T element() {
T result = peek();
if (result == null) {
throw new NoSuchElementException();
}
return result;
}
// should be synchronized on collectionLock before calling
private T removeFirst() {
T result = storage.removeFirst();
@ -209,17 +200,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public T remove() {
T result = poll();
if (result == null) {
throw new NoSuchElementException();
}
return result;
}
@Override
public int drainTo(Collection<? super T> c) {
return drainTo(c, Integer.MAX_VALUE);
@ -256,11 +236,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean contains(Object element) {
synchronized (collectionLock) {