Return | Jackpot | Comms | HostInterface | Config | Timer | JackSystem

/*
 * Timer.java
 */


/**
 * Timer object allows to generate timeouts. There can be up to five timers running
 * at the same time. Timers can be created, destroyed, started and checked if the
 * specified time has elapsed.
 */
class Timer
{
    /**
     * Contains an ID assosiated with this timer
     * Zero value indicates that timer is not vailable  to this
     * Timer object.
     */
    private int TimerId = 0;

    /**
     * Creates and initialises a new timer object. Jackpot application allows for
     * maximum of five timers to be used simultaneously.
     */
    Timer()
    {
        Create();
    }

    /**
     * Creates a new timer.
     *
     * @return  false if there are no more timers available or there is already
     *          timer with this ID.
     *          true is returned if the timer was successfully created.
     */
    boolean Create()
    {
        TimerId = Add();

        return TimerId > 0 ? true : false;
    }


    /**
     * Kills this timer and gives the timer resource to other tasks. In order
     * to use it again Create method should be called to
     * re-initialise the timer.
     */
    void Destroy()
    {
        if (TimerId > 0)
        {
            Remove(TimerId);
            TimerId = 0;
        }
    }

    /**
     * Sets this timer to the specified number of milliseconds and starts the timer
     * running. Once started Expired method can be used to see whether
     * specified time has expired.
     *
     * @param Time timeout value in milliseconds
     */
    void Start(int Time)
    {
        if (TimerId > 0)
            Set(TimerId, Time);
    }

    /**
     * Stops the timer running.
     */
    void Stop()
    {
        if (TimerId > 0)
            Set(TimerId, 0);
    }

    /**
     * Checks if the time specified in Start method has expired.
     *
     * @return  true if previously set time has expired, otherwise false
     *          is returned.
     */
    boolean Expired()
    {
        if (TimerId > 0)
            return CheckExpired(TimerId);

        return false;
    }

    /**
     * Tries to allocate timer resource for this timer object.
     *
     * @return Id value which should be assosiated with this timer object if
     *         successful, otherwise zero is returned.
     */
    private native int Add();

    /**
     * Removes resource from the list for this timer object.
     *
     * @param Id value identifying this timer object
     */
    private native void Remove(int Id);

    /**
     * Checks if the time for this timer object has expired.
     *
     * @param   Id value identifying this timer object
     * @return  true if previously set time value has expired, otherwise
     *          false is returned. False is also returned if timer Id is
     *          incorrect.
     */
    private native boolean CheckExpired(int Id);

    /**
     * Sets the timer value and starts counting down to zero.
     *
     * @param   Id value identifying this timer object
     * @param   Time time in milliseconds
     */
    private native void Set(int Id, int Time);
}

Return | Jackpot | Comms | HostInterface | Config | Timer | JackSystem