package cc.monitors;

import java.util.Enumeration;

import cc.events.*;

public class CompositeMonitor extends AbstractMonitor {

	protected java.util.Vector listOfMonitors = new java.util.Vector();

	public void attachMonitor(AbstractMonitor monitor) {
		monitor.setFather(this);
		listOfMonitors.add(monitor);
	}

	public void detachMonitor(AbstractMonitor monitor) {
		monitor.setFather(null);
		monitor.terminate();
		listOfMonitors.remove(monitor);
	}

	public void detachAll() {
		while (!listOfMonitors.isEmpty()) {
			detachMonitor((AbstractMonitor) listOfMonitors.firstElement());
		}
	}

	public void update(Event event) {
		Enumeration iterator = listOfMonitors.elements();
		while (iterator.hasMoreElements())
			 ((AbstractMonitor) iterator.nextElement()).update(event);
	}

	public void react(AbstractMonitor monitor) {
	}

	public void evaluate() {
		/*for(Enumeration i = listOfMonitors.elements(); 
		       i.hasMoreElements();
		       item = i.nextElement()){
		}
		*/
		Enumeration iterator = listOfMonitors.elements();
		while (iterator.hasMoreElements()) {
			if (((AbstractMonitor) iterator.nextElement())
				.getEvaluationResult()
				== false) {
				this.setEvaluationResult(false);
				return;
			}
		}
		this.setEvaluationResult(true);

	}

	public void terminate() {
		detachAll();
	}
}
