001package horstmann.ch05_mailgui;
002import java.util.ArrayList;
003
004/**
005   A first-in, first-out bounded collection of messages.
006 */
007public class MessageQueue
008{
009        /**
010      Constructs an empty message queue.
011         */
012        public MessageQueue()
013        {
014                queue = new ArrayList<Message>();
015        }
016
017        /**
018      Remove message at head.
019      @return message that has been removed from the queue
020         */
021        public Message remove()
022        {
023                if (!queue.isEmpty())
024                        return queue.remove(0);
025                else
026                        throw new IllegalStateException("Queue is empty");
027        }
028
029        /**
030      Append message at tail.
031      @param newMessage the message to be appended
032         */
033        public void add(Message newMessage)
034        {
035                queue.add(newMessage);
036        }
037
038        /**
039      Get the total number of messages in the queue.
040      @return the total number of messages in the queue
041         */
042        public int getLength()
043        {
044                return queue.size();
045        }
046
047        /**
048      Get message at head.
049      @return message that is at the head of the queue
050         */
051        public Message getHead()
052        {
053                if (!queue.isEmpty())
054                        return queue.get(0);
055                else
056                        throw new IllegalStateException("Queue is empty");
057        }
058
059        private ArrayList<Message> queue;
060}