package com.srbenoit.log;

import java.util.ArrayList;
import java.util.List;

/**
 * A list of messages generated by some process that spans classes. Messages are intended for users,
 * not for system diagnostic.
 */
public class MessageList extends LoggedObject {

    /** the list of messages */
    private final transient List<MessageInt> messages;

    /**
     * Constructs a new <code>MessageList</code>.
     */
    public MessageList() {

        super();

        this.messages = new ArrayList<MessageInt>(20);
    }

    /**
     * Gets the number of messages in the list.
     *
     * @return  the number of messages
     */
    public int size() {

        return this.messages.size();
    }

    /**
     * Gets a particular message.
     *
     * @param   index  the index of the message to get
     * @return  the message
     */
    public MessageInt getMessage(final int index) {

        return this.messages.get(index);
    }

    /**
     * Adds a message to the list and logs it at the <code>FINE</code> log level.
     *
     * @param  message  the message to add
     */
    public void add(final MessageInt message) {

        LOG.fine(message.toString());
        this.messages.add(message);
    }

    /**
     * Generates the string representation of the message list, which contains all of the message
     * strings separated by carriage return / newlines;
     *
     * @return  the string representation
     */
    @Override public String toString() {

        StringBuilder builder;

        builder = new StringBuilder(200);

        for (MessageInt msg : this.messages) {
            builder.append(msg.toString());
            builder.append("\r\n");
        }

        return builder.toString();
    }
}
