package com.srbenoit.log;

/**
 * A message that indicates an error in parsing. It includes the range in the source data where the
 * parse error occurred.
 */
public class ParseError implements MessageInt {

    /** the error message */
    private final transient String errorMsg;

    /** the start position */
    private final transient int start;

    /** the end position */
    private final transient int end;

    /**
     * Constructs a new <code>ParseError</code>.
     *
     * @param  msg  the message
     * @param  pos  the error position
     */
    public ParseError(final String msg, final int pos) {

        this.errorMsg = msg;
        this.start = pos;
        this.end = pos;
    }

    /**
     * Constructs a new <code>ParseError</code>.
     *
     * @param  msg       the message
     * @param  startPos  the start position
     * @param  endPos    the end position
     */
    public ParseError(final String msg, final int startPos, final int endPos) {

        this.errorMsg = msg;
        this.start = startPos;
        this.end = endPos;
    }

    /**
     * Gets the error message.
     *
     * @return  the error message
     */
    public String getMessage() {

        return this.errorMsg;
    }

    /**
     * Gets the start position of the parse error.
     *
     * @return  the start position
     */
    public int getStart() {

        return this.start;
    }

    /**
     * Gets the end position of the parse error.
     *
     * @return  the end position
     */
    public int getEnd() {

        return this.end;
    }

    /**
     * Generates the string representation of the message.
     *
     * @return  the string representation
     */
    @Override public String toString() {

        StringBuilder builder;

        builder = new StringBuilder(50);

        builder.append(this.errorMsg);
        builder.append('(');
        builder.append(this.start);

        if (this.end != this.start) {
            builder.append('-');
            builder.append(this.end);
        }

        builder.append(')');

        return builder.toString();
    }
}
