    01: import java.util.ArrayList;
    02:
    03: /**
    04:    A first-in, first-out collection of messages. This
    05:    implementation is not very efficient. We will consider
    06:    a more efficient implementation in chapter 3.
    07: */
    08: public class MessageQueue
    09: {
    10:    /**
    11:       Constructs an empty message queue.
    12:    */
    13:    public MessageQueue()
    14:    {
    15:       queue = new ArrayList<Message>();
    16:    }
    17:
    18:    /**
    19:       Remove message at head.
    20:       @return message that has been removed from the queue
    21:    */
    22:    public Message remove()
    23:    {
    24:       return queue.remove(0);
    25:    }
    26:
    27:    /**
    28:       Append message at tail.
    29:       @param newMessage the message to be appended
    30:    */
    31:    public void add(Message newMessage)
    32:    {
    33:       queue.add(newMessage);
    34:    }
    35:
    36:    /**
    37:       Get the total number of messages in the queue.
    38:       @return the total number of messages in the queue
    39:    */
    40:    public int size()
    41:    {
    42:       return queue.size();
    43:    }
    44:
    45:    /**
    46:       Get message at head.
    47:       @return message that is at the head of the queue, or null
    48:       if the queue is empty
    49:    */
    50:    public Message peek()
    51:    {
    52:       if (queue.size() == 0) return null;
    53:       else return queue.get(0);
    54:    }
    55:
    56:    private ArrayList<Message> queue;
    57: }