package entity;

import java.util.*;

/* SUDTimer is a wrapper for the Java Timer, and is also a TimerTask
 * It implements the Subject interface that we defined
 */
public class SUDTimer extends TimerTask implements Subject {
	private static final long NOW = 0; // 0 milleseconds from now
	private long myInterval;
	private ArrayList<control.Observer> myObservers;
	private Timer t;
	private long DEFAULT_INTERVAL = 1800000; // thirty minutes
	
	public SUDTimer() {
		myInterval = DEFAULT_INTERVAL;
		t = new Timer();
		myObservers = new ArrayList<control.Observer>();
	}
	
	public void setInterval(long interval) {
		myInterval = interval;
	}
	
	public void startTimer() {
		t.schedule(this, NOW, myInterval);
	}
	
	public void registerObserver(control.Observer toRegister){
		myObservers.add(toRegister);
	}
		
	
	public void removeObserver(control.Observer toRemove) {
		myObservers.remove(toRemove);
	}
	
	public void notifyObservers() {
		for (int i = 0; i < myObservers.size(); i++) {
			myObservers.get(i).update();
		}
	}
	
	public void run() {
		notifyObservers();
	}
	
	/* test code
	public static void main(String[] args) {
		SUDTimer sudt = new SUDTimer();
		sudt.startTimer();
	}
	*/
}
