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