    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();
    16:    }
    17:
    18:    /**
    19:       Remove message at head.
    20:       @return message that has been removed from the queue
    21:    */
    22:    public Message removeFirst()
    23:    {
    24:       return (Message) 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
    48:    */
    49:    public Message getFirst()
    50:    {
    51:       return (Message) queue.get(0);
    52:    }
    53:
    54:    private ArrayList queue;
    55: }