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