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

@ -16,6 +16,7 @@ public abstract class AbstractQueue<T> extends AbstractCollection<T> implements
super();
}
@Override
public boolean add(T element) {
if (offer(element)) {
return true;
@ -23,7 +24,8 @@ public abstract class AbstractQueue<T> extends AbstractCollection<T> implements
throw new IllegalStateException();
}
}
@Override
public boolean addAll(Collection <? extends T> collection) {
if (collection == null) {
throw new NullPointerException();
@ -35,26 +37,31 @@ public abstract class AbstractQueue<T> extends AbstractCollection<T> implements
return true;
}
@Override
public void clear() {
while (size() > 0) {
poll();
}
}
@Override
public T element() {
emptyCheck();
return peek();
}
public T remove() {
emptyCheck();
return poll();
}
private void emptyCheck() {
if (size() == 0) {
T result = peek();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
@Override
public T remove() {
T result = poll();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
}