From 97ffc5b15eeba27e0b80c1ef21031259f10ad364 Mon Sep 17 00:00:00 2001 From: Mike Keesey Date: Tue, 22 May 2012 15:23:28 -0600 Subject: [PATCH] Adding java.util.Queue interface and java.util.AbstractQueue. --- classpath/java/util/AbstractQueue.java | 60 ++++++++++++++++++++++++++ classpath/java/util/Queue.java | 20 +++++++++ 2 files changed, 80 insertions(+) create mode 100644 classpath/java/util/AbstractQueue.java create mode 100644 classpath/java/util/Queue.java diff --git a/classpath/java/util/AbstractQueue.java b/classpath/java/util/AbstractQueue.java new file mode 100644 index 0000000000..19fa46aaa1 --- /dev/null +++ b/classpath/java/util/AbstractQueue.java @@ -0,0 +1,60 @@ +/* Copyright (c) 2012, 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; + +public abstract class AbstractQueue extends AbstractCollection implements Queue { + + protected AbstractQueue() { + super(); + } + + public boolean add(T element) { + if (offer(element)) { + return true; + } else { + throw new IllegalStateException(); + } + } + + public boolean addAll(Collection collection) { + if (collection == null) { + throw new NullPointerException(); + } + + for (T element : collection) { + add(element); + } + + return true; + } + + public void clear() { + while (size() > 0) { + poll(); + } + } + + public T element() { + emptyCheck(); + return peek(); + } + + public T remove() { + emptyCheck(); + return poll(); + } + + private void emptyCheck() { + if (size() == 0) { + throw new NoSuchElementException(); + } + } +} diff --git a/classpath/java/util/Queue.java b/classpath/java/util/Queue.java new file mode 100644 index 0000000000..27855a7f61 --- /dev/null +++ b/classpath/java/util/Queue.java @@ -0,0 +1,20 @@ +/* Copyright (c) 2012, 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; + +public interface Queue extends Collection, Iterable { + public boolean add(T element); + public T element(); + public boolean offer(T element); + public T peek(); + public T poll(); + public T remove(); +}