/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.server.actionprocessor;

import gecco.server.core.*;

/**
 *  This class is a wrapper object that is stored in the priority queue in the
 *  ActionProcessor.
 *
 * @author     Henrik Hägerström
 * @created    April 23, 2001
 */
public class QueuedAction implements Comparable {
    /**
     *  Description of the Field
     */
    private Action action;
    /**
     *  Description of the Field
     */
    private String fromRole;
    /**
     *  Description of the Field
     */
    private long absoluteTimeToExecute;


    /**
     *  Instanciates a QueuedAction object.
     *
     * @param  _fromRole              The commanding role
     * @param  _action                The action to be executed
     * @param  absoluteTimeToExecute  The time to execute next checkpoint for
     *      the action
     */
    public QueuedAction(String _fromRole, Action _action, long absoluteTimeToExecute) {
        fromRole = _fromRole;
        action = _action;
        this.absoluteTimeToExecute = absoluteTimeToExecute;
    }


    /**
     *  Sets the TimeToExecute attribute of the QueuedAction object
     *
     * @param  newTimeToExecute  The new TimeToExecute value
     */
    public void setTimeToExecute(long newTimeToExecute) {
        absoluteTimeToExecute = newTimeToExecute;
    }


    /**
     *  Gets the Action attribute of the QueuedAction object
     *
     * @return    The Action value
     */
    public Action getAction() {
        return action;
    }


    /**
     *  Gets the FromRole attribute of the QueuedAction object
     *
     * @return    The FromRole value
     */
    public String getFromRole() {
        return fromRole;
    }


    /**
     *  Gets the TimeToExecute attribute of the QueuedAction object
     *
     * @return    The TimeToExecute value
     */
    public long getTimeToExecute() {
        return absoluteTimeToExecute;
    }


    /**
     *  Description of the Method
     *
     * @param  a  Description of Parameter
     * @return    Description of the Returned Value
     */
    public boolean equals(Object a) {
        return (compareTo(a) == 0);
    }


    /**
     *  Description of the Method
     *
     * @param  a  Description of Parameter
     * @return    Description of the Returned Value
     */
    public int compareTo(Object a) {
        // Another object always comes before this one in a TreeSet
        if (!(a instanceof QueuedAction)) {
            return 1;
        }
        QueuedAction b = (QueuedAction) a;
        if (absoluteTimeToExecute != b.getTimeToExecute()) {
            return absoluteTimeToExecute < b.getTimeToExecute() ? -1 : 1;
        }
        if (action.getActionHandle() != b.getAction().getActionHandle()) {
            return action.getActionHandle() < b.getAction().getActionHandle() ? -1 : 1;
        }
        return 0;
    }

}
