/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas_timer;

public class TimerEvent {

    private TimerEventListener listener = null;
    private Object arg = null;
    private long reminding = 0;
    private long startvalue;
    private boolean permanent = false;
    private boolean stopped = false;

    /**
     * Constructor
     */
    public TimerEvent(TimerEventListener l, long timeout, Object a, boolean p) {
	if (TraceTimer.isDebug())
	    TraceTimer.debug("TimerEvent.new("+l+","+timeout+","+a+","+p+")");
	listener = l;
	reminding = timeout;
	startvalue = timeout;
	arg = a;
	permanent = p;
    }


    /**
     * Update timer every second. Used by clock.
     * - this must be called with the timerList monitor.
     */
    public long update() {
	return --reminding;
    }

    /**
     * Restart timer to its initial value
     */
    public void restart() {
	if (TraceTimer.isDebug())
	    TraceTimer.debug("TimerEvent("+listener+").restart");
	stopped=false;
	reminding = startvalue;
    }

    /**
     * Process the Timer
     */
    public void process() {
	if (listener != null) {
	    if (TraceTimer.isDebug())
		TraceTimer.debug("TimerEvent("+listener+").process");
	    listener.timeoutExpired(arg);
	}
    }

    public void change(long timeout, Object a) {
	if (TraceTimer.isDebug())
	    TraceTimer.debug("TimerEvent("+listener+").change("+timeout+","+a+")");
	stopped=false;
	startvalue = timeout;
	reminding = startvalue;
	arg = a;
    }

    /**
     * Unvalidate the timer. It will be removed by the timer manager.
     */
    public void unset() {
	if (TraceTimer.isDebug())
	    TraceTimer.debug("TimerEvent("+listener+").unset");
	// the timerlist is not locked.
	reminding = 1;
	arg = null;
	listener = null;
	permanent = false;
	stopped=false;
    }

    /**
     * stop the timer, but keep it for further reuse (See change())
     */
    public void stop() {
	if (TraceTimer.isDebug())
	    TraceTimer.debug("TimerEvent("+listener+").stop");
	// the timerlist is not locked.
	reminding = 1000000;
	stopped=true;
    }

    /**
     * Is this timer valid ?
     */
    public boolean valid() {
	return (listener != null);
    }

    /**
     * Is this timer permanent ?
     */
    public boolean ispermanent() {
	return permanent;
    }

    public boolean isStopped() {
        return stopped;
    }
}

