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

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.qmanager;

import gecco.server.automaton.*;

/**
 *  This class encapsulates data about an automaton in the queue of the server.
 *  It consists of data about which automaton it is, that is, the position on
 *  the map, and data about which place in the queue it has, that is, the time
 *  until the automaton should be taken from the queue and processed in some
 *  way.
 *
 * @author     Henrik Bäärnhielm, Karl Hasselström, Henrik Hägerström
 * @created    May 2, 2001
 */
public class QueuedAutomaton implements Comparable {
    /**
     *  Description of the Field
     */
    private int x;
    /**
     *  Description of the Field
     */
    private int y;
    /**
     *  Description of the Field
     */
    private long absoluteTimeToExecute;


    /**
     *  Constructor for the QueuedAutomaton object
     *
     * @param  x                      The x-coordinate of the automaton
     * @param  y                      The x-coordinate of the automaton
     * @param  absoluteTimeToExecute  The time when the automaton should be
     *      processed. This is absolute time, _not_ a relative time from the
     *      current time.
     */
    public QueuedAutomaton(int x, int y, long absoluteTimeToExecute) {
        this.x = x;
        this.y = y;
        this.absoluteTimeToExecute = absoluteTimeToExecute;
    }


    /**
     *  Sets the time when the automaton should be processed.
     *
     * @param  newTimeToExecute  The new TimeToExecute value
     */
    public void setTimeToExecute(long newTimeToExecute) {
        absoluteTimeToExecute = newTimeToExecute;
    }


    /**
     *  Gets the x coordinate of the automaton
     *
     * @return    The x coordinate
     */
    public int getX() {
        return x;
    }


    /**
     *  Gets the y coordinate of the automaton
     *
     * @return    The y coordinate
     */
    public int getY() {
        return y;
    }


    /**
     *  Gets the time when the automaton shoule be processed.
     *
     * @return    The time (absolute)
     */
    public long getTimeToExecute() {
        return absoluteTimeToExecute;
    }


    /**
     *  Tests whether to QueuedAutomatons are equal, which is true iff they have
     *  the same coordinates and the same time for process. This method obeys
     *  the general contract of the Comparable interface.
     *
     * @param  a  Should be a QueuedAutomaton
     * @return    true if a is equal to this automaton, false otherwise
     */
    public boolean equals(Object a) {
        return (compareTo(a) == 0);
    }


    /**
     *  Compares this automaton with a given automaton. An automaton is
     *  considered less than an other automaton if at least one of the following
     *  conditions are satisfied. a) the time for process is less, that is,
     *  earlier b) the x coordinate is less c) the y coordinate is less The
     *  checks are performed in the above order. This method obeys the general
     *  contract of the Comparable interface.
     *
     * @param  a  Should be a QueuedAutomaton
     * @return    -1 if this automaton is less than a, 0 if the automatons are
     *      equal, 1 if a is greater than this automaton
     */
    public int compareTo(Object a) {
        if (!(a instanceof QueuedAutomaton)) {
            return -1;
        }
        QueuedAutomaton b = (QueuedAutomaton) a;
        if (absoluteTimeToExecute != b.getTimeToExecute()) {
            return absoluteTimeToExecute < b.getTimeToExecute() ? -1 : 1;
        }
        if (x != b.getX()) {
            return x < b.getX() ? -1 : 1;
        }
        if (y == b.getY()) {
            return 0;
        }
        return y < b.getY() ? -1 : 1;
    }

}
