package com.jscape.inet.terminal.events;

import com.jscape.inet.terminal.TerminalListener;

/**
 * Represents <code>Terminal</code> command event. This event is generated
 * when new control command comes. Holds information about incoming command.
 */
public class CommandEvent
        implements TerminalEvent {

    private final String canonicalForm;
    private final String value;

    //----------------------------------------------------------------------------

    /**
     * Creates <code>CommandEvent</code> object giving "canonical" form and value
     * of incoming control command.
     *
     * @param canonicalForm "canonical" form.
     * @param value         string representation of control command.
     */
    public CommandEvent(String canonicalForm, String value) {
        if (null == canonicalForm || value == null) {
            throw new NullPointerException("CommandEvent.constructor(): null passed");
        }

        this.canonicalForm = canonicalForm;
        this.value = value;
    }

    //----------------------------------------------------------------------------

    /**
     * Returns "canonical" form of incoming command.
     *
     * @return "canonical" form of incoming command.
     */
    public String getCanonicalForm() {
        return this.canonicalForm;
    }

    //----------------------------------------------------------------------------

    /**
     * Returns string representation of control command
     *
     * @return string representation of control command
     */
    public String getValue() {
        return this.value;
    }

    //----------------------------------------------------------------------------

    /**
     * Calls correct callback method of <code>TerminalListener</code> object.
     *
     * @param listener <code>TerminalListener</code> object.
     */
    public void accept(TerminalListener listener) {
        listener.onCommandEvent(this);
    }

}