001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.util;
007
008import java.util.*;
009//Obsolete with the intro of jdk 1.5 java.util.Q
010
011/**
012A thin wrapper around a java collection that provides queue semantics (FIFO).
013**/
014public class Queue extends LinkedList
015{
016public Queue() {
017  super();
018  }
019  
020public Queue(Collection c) {
021  super(c);
022  }
023  
024/** 
025Enqueue's the specified object. Behaves as per {@link java.util.Collection#add(Object)} 
026**/
027public boolean enque(Object obj) {
028  return add(obj);
029  }
030
031/** 
032Dequeues an object from the beginning of the queue 
033
034@throws NoSuchElementException  if this list is empty.
035**/
036public Object deque() {
037  return removeFirst();
038  }
039
040/** Returns <tt>true</tt> is this queue is empty, <tt>false</tt> otherwise**/
041public boolean empty() {
042  return size() == 0;
043  }
044
045public static void main(String[] args) 
046  {
047  Queue q = new Queue();
048  q.enque("hello");
049  q.enque("world");
050  System.out.println("q="+ q);
051  //DO NOT PUT "n < q.size()" in the loop, because with every deque
052  //the SIZE CHANGES (and the loop won't work properly).
053  
054  //method 1: take a snapshot
055  /*
056  int size = q.size();    //<--the snapshot 
057  for(int n = 0; n < size; n++) {
058    System.out.println("deque["+n+"]="+q.deque());
059    }
060  */
061  
062  //method 2: use empty()
063  while (! q.empty())
064    System.out.println(q.deque());
065
066  System.out.println("This should throw an Exception");
067  q.deque();
068  }
069
070}          //~class Queue