+++ /dev/null
-// Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
-//
-// You may modify, copy, and redistribute this code under the terms of
-// the GNU Library Public License version 2.1, with the exception of
-// the portion of clause 6a after the semicolon (aka the "obnoxious
-// relink clause")
-
-package org.ibex.util;
-
-/** A simple synchronized queue, implemented as an array */
-public class Queue {
-
- public Queue(int initiallength) { vec = new Object[initiallength]; }
-
- /** The store */
- private Object[] vec;
-
- /** The index of the first node in the queue */
- private int first = 0;
-
- /** The number of elements in the queue; INVARAINT: size <= vec.length */
- private int size = 0;
-
- /** Grow the queue, if needed */
- private void grow(int newlength) {
- Object[] newvec = new Object[newlength];
- if (first + size > vec.length) {
- System.arraycopy(vec, first, newvec, 0, vec.length - first);
- System.arraycopy(vec, 0, newvec, vec.length - first, size - (vec.length - first));
- } else {
- System.arraycopy(vec, first, newvec, 0, size);
- }
- first = 0;
- vec = newvec;
- }
-
- /** The number of elements in the queue */
- public int size() { return size; }
-
- /** Empties the queue */
- public synchronized void flush() {
- first = 0;
- size = 0;
- for(int i=0; i<vec.length; i++) vec[i] = null;
- }
-
- /** Add an element to the front of the queue */
- public synchronized void prepend(Object o) {
- if (size == vec.length) grow(vec.length * 2);
- first--;
- if (first < 0) first += vec.length;
- vec[first] = o;
- size++;
- if (size == 1) notify();
- }
-
- /** Add an element to the back of the queue */
- public synchronized void append(Object o) {
- if (size == vec.length) grow(vec.length * 2);
- if (first + size >= vec.length) vec[first + size - vec.length] = o;
- else vec[first + size] = o;
- size++;
- if (size == 1) notify();
- }
-
- /** Remove and return and element from the queue, blocking if empty. */
- public Object remove() { return remove(true); }
-
- /** Remove and return an element from the queue, blocking if
- <tt>block</tt> is true and the queue is empty. */
- public synchronized Object remove(boolean block) {
-
- while (size == 0 && block) {
- try { wait(); } catch (InterruptedException e) { }
- }
-
- if (!block && size == 0) return null;
- Object ret = vec[first];
- first++;
- size--;
- if (first >= vec.length) first = 0;
- return ret;
- }
-
- /** Returns the top element in the queue without removing it */
- public synchronized Object peek() {
- if (size == 0) return null;
- return vec[first];
- }
-
-}