Back to index

wims  3.65+svn20090927
Queue.java
Go to the documentation of this file.
00001 /*
00002 $Id: Queue.java,v 1.2 2003/02/18 11:48:48 sander Exp $
00003 */
00004 
00005 
00006 package fr.ove.utils;
00007 
00008 import java.io.Serializable;
00009 import java.util.*;
00010 import fr.ove.utils.Queueable;
00011 import fr.ove.utils.LinkedElement;
00012 
00019 public class Queue implements Queueable, Serializable {
00023     private LinkedElement head;
00024     
00028     private LinkedElement tail;
00029     
00033     private int nbElements;
00034     
00038     public Queue() {
00039         head = tail = null;
00040         nbElements = 0;
00041     }
00042     
00046     public void enqueue(Object element) {
00047         LinkedElement newElement = new LinkedElement(element);
00048         if (nbElements == 0)
00049             head = tail = newElement;
00050         else {
00051             newElement.setNext(tail);
00052             tail.setPrevious(newElement);
00053             tail = newElement;
00054         }
00055         
00056         nbElements++;
00057     }
00058 
00063     public Object dequeue() {
00064         Object element = null;
00065         
00066         if (nbElements != 0) {
00067             LinkedElement dequeued = head;
00068             element = dequeued.getElement();
00069             head = dequeued.getPrevious();
00070             dequeued.remove();
00071             
00072             if (nbElements == 1)
00073                 head = tail = null;
00074                 
00075             nbElements--;
00076         }
00077         
00078         return element;
00079     }
00080 
00085     public Object peek() {
00086         if (nbElements != 0)
00087             return head.getElement();
00088         else 
00089             return null;
00090     }
00091 
00096     public boolean isEmpty() {
00097         return (nbElements == 0);
00098     }
00099     
00103     public void clear() {
00104         head = tail = null;
00105         nbElements = 0;
00106     }
00107         
00111     public Enumeration elements() {
00112         synchronized (this) {
00113             return (Enumeration) new QueueEnumerator(this);
00114         }
00115     }
00116     
00117     
00122     public Object[] getElements() {
00123         Object elements[] = null;
00124         
00125         if (nbElements != 0) {
00126             elements = new Object[nbElements];
00127             
00128             LinkedElement ptr = tail;
00129             int index = 0;
00130             while (ptr != null) {
00131                 elements[index++] = ptr.getElement();
00132                 ptr = ptr.getNext();
00133             }
00134         }
00135         
00136         return elements;
00137     }
00138     
00142     public String toString() {
00143         String string = "";
00144         LinkedElement ptr = tail;
00145         
00146         while (ptr != null) {
00147             string += " " + ptr.toString();
00148             ptr = ptr.getNext();
00149         }
00150         
00151         return string + " ";
00152     }
00153 
00154 
00155 
00156     /*
00157     * An Enumeration representation of a queue.
00158     */
00159     private class QueueEnumerator implements Enumeration, Serializable {
00163         Queue queue;
00164         
00168         LinkedElement next;
00169         
00174         public QueueEnumerator(Queue queue) {
00175             this.queue = queue;
00176             next = queue.head;
00177         }
00178         
00185         public boolean hasMoreElements() {
00186             return (next != null);
00187         }
00188 
00195         public Object nextElement() {
00196               synchronized (queue) {
00197                   if (next != null) {
00198                       Object element = next.getElement();
00199                       next = next.getPrevious();
00200                      return element;
00201               }
00202               }
00203               
00204               throw new NoSuchElementException("QueueEnumerator");
00205         }
00206     }
00207 
00208 }