/**
 * 
         -------- Copyright (C) [2012]  [Kristofer SCHWEIGER & Niklas FLORIAN1]---------

    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., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA

 * 
 * For any licensing issues please contact us under following adress: Mosaik.Projekt.2012@gmail.com
 */



package dataBase;

public abstract class TimedAction extends Thread {
	
	private long ms;
	
	/**
	 * Constructor
	 * @param ms - time to wait before starting action (in thread)
	 */
	public TimedAction(long ms){
		this.ms = ms;
		this.start();
	}
	
	/** do not overwrite **/
	public void run(){
		
		if(ms<1){			// do it now!
			doAction();
			return;
		}
		
		try {
			sleep(ms);
		} catch (InterruptedException e) {
			System.err.println("Interrupted TimedAction: "+e);
		}
		
		doAction();
	}
	
	/**
	 * Catches exceptions from action function
	 */
	private void doAction(){
		try {
			action();
		} catch (Exception e) {
			System.err.println("Exception in TimedAction: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * Abstract action function to implement.
	 * @throws Exception
	 */
	public abstract void action() throws Exception;
	
}
