// 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:   Connector.java

package drm.signal;


// Referenced classes of package drm.signal:
//			Simulation, AbstractBox, Signal

public class Connector
	implements Simulation
{

	protected AbstractBox in;
	protected AbstractBox out;
	protected Signal s;
	protected boolean changed;

	public Connector()
	{
		in = null;
		out = null;
		s = null;
		changed = false;
	}

	public Connector(AbstractBox in, AbstractBox out)
	{
		this.in = null;
		this.out = null;
		s = null;
		changed = false;
		connect(in, out);
	}

	public void connect(AbstractBox in, AbstractBox out)
	{
		disconnect();
		this.in = in;
		this.out = out;
		if(in != null)
			in.addOutput(this);
		if(out != null)
			out.addInput(this);
	}

	public void disconnect()
	{
		if(in != null)
			in.remInput(this);
		if(out != null)
			out.remOutput(this);
		in = null;
		out = null;
	}

	public void setSignal(Signal s)
	{
		if(!changed)
			if(this.s != null)
				changed = !this.s.equals(s);
			else
				changed = s != null;
		if(s != null)
			this.s = s.copy();
		else
			this.s = null;
	}

	public Signal getSignal()
	{
		return s;
	}

	public Object getSignalValue()
	{
		return s.getValue();
	}

	public boolean isChanged()
	{
		return changed;
	}

	public void setChanged(boolean state)
	{
		changed = state;
	}

	public Simulation[] simulate()
	{
		if(changed && out != null)
		{
			Simulation sim[] = new Simulation[1];
			sim[0] = out;
			changed = false;
			return sim;
		} else
		{
			changed = false;
			return new Simulation[0];
		}
	}

	public long getStep()
	{
		if(s == null)
			return 0L;
		else
			return s.getStep();
	}

	public String toString()
	{
		String out;
		if(s == null)
			out = "{null";
		else
			out = "{" + s.toString();
		out = out + "," + String.valueOf(changed) + "}";
		return out;
	}
}
