// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   AbstractBox.java

package drm.signal;

import java.util.Iterator;
import java.util.Vector;

// Referenced classes of package drm.signal:
//			Connector, Simulation, BoxConnector, BoxSignal, 
//			Signal

public abstract class AbstractBox
	implements BoxConnector, BoxSignal, Simulation
{

	public Vector in;
	public Vector out;
	protected long step;

	public AbstractBox()
	{
		in = new Vector();
		out = new Vector();
		step = 0L;
	}

	public void setInputs(Connector con[])
	{
		in.clear();
		for(int i = 0; i < con.length; i++)
			in.add(con[i]);

	}

	public void setOutputs(Connector con[])
	{
		out.clear();
		for(int i = 0; i < con.length; i++)
			out.add(con[i]);

	}

	public void setInSignal(int nr, Signal s)
		throws ArrayIndexOutOfBoundsException
	{
		Connector bc = (Connector)in.get(nr);
		bc.setSignal(s);
	}

	public void setInput(int nr, Connector con)
		throws ArrayIndexOutOfBoundsException
	{
		in.set(nr, con);
	}

	public Connector getInput(int nr)
		throws ArrayIndexOutOfBoundsException
	{
		return (Connector)in.get(nr);
	}

	public Connector[] getInputs()
	{
		return (Connector[])in.toArray();
	}

	public Connector[] getOutputs()
	{
		return (Connector[])out.toArray();
	}

	public int addInput(Connector con)
	{
		in.add(con);
		return in.size() - 1;
	}

	public void setOutput(int nr, Connector con)
		throws ArrayIndexOutOfBoundsException
	{
		out.set(nr, con);
	}

	public Connector getOutput(int nr)
		throws ArrayIndexOutOfBoundsException
	{
		return (Connector)out.get(nr);
	}

	public int addOutput(Connector con)
	{
		out.add(con);
		return out.size() - 1;
	}

	public Connector remInput(int nr)
		throws ArrayIndexOutOfBoundsException
	{
		return (Connector)in.remove(nr);
	}

	public boolean remInput(Connector con)
	{
		return in.remove(con);
	}

	public Connector remOutput(int nr)
		throws ArrayIndexOutOfBoundsException
	{
		return (Connector)out.remove(nr);
	}

	public boolean remOutput(Connector con)
	{
		return out.remove(con);
	}

	public void setOutSignal(Signal s)
	{
		for(Iterator it = out.iterator(); it.hasNext(); ((Connector)it.next()).setSignal(s));
	}

	public Signal getInSignal(int nr)
		throws ArrayIndexOutOfBoundsException
	{
		return ((Connector)in.get(nr)).getSignal();
	}

	public Object getInValue(int nr)
		throws ArrayIndexOutOfBoundsException
	{
		return ((Connector)in.get(nr)).getSignal().getValue();
	}

	public Signal[] getInSignals()
	{
		return (Signal[])in.toArray();
	}

	protected abstract void update();

	protected void setCurrentStep()
	{
		for(Iterator it = in.iterator(); it.hasNext();)
		{
			Signal ts = ((Connector)it.next()).getSignal();
			long t = 0L;
			if(ts != null)
				ts.getStep();
			if(t > step)
				step = t;
		}

	}

	public Simulation[] simulate()
	{
		setCurrentStep();
		update();
		Iterator it = out.iterator();
		Vector t = new Vector();
		while(it.hasNext()) 
		{
			Connector con = (Connector)it.next();
			if(con.isChanged())
				t.add(con);
		}
		Simulation ts[] = new Simulation[t.size()];
		for(int i = 0; i < ts.length; i++)
			ts[i] = (Simulation)t.get(i);

		return ts;
	}

	public long getStep()
	{
		return step;
	}

	public void setStep(long step)
	{
		this.step = step;
	}

	public void clearSignals()
	{
		for(Iterator it = out.iterator(); it.hasNext(); ((Connector)it.next()).setSignal(null));
	}
}
