/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 25-Oct-2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package uk.ac.ic.doc.kevlar.core.framework;

import uk.ac.ic.doc.kevlar.api.programs.arguments.IInputNode;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IOutputNode;
import uk.ac.ic.doc.kevlar.publicInterfaces.INodeID;

/**
 * @author Daniel Burke and Steve Zymler
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Pipe implements IPipe 
{
	private long id = 0;
	private boolean broken;
	
	private PipelineProgram fromProgram ;
	private INodeID fromNodeID;
	private PipelineProgram toProgram ;
	private INodeID toNodeID;
	
	public boolean isDestroyed() 
	{
		return broken;
	}
	
	public void setup( long id,
			  PipelineProgram fromProgram,
			  INodeID fromNode,
			  PipelineProgram toProgram,
			  INodeID toNode)
	{
		this.id = id ;
		this.fromProgram = fromProgram ;
		this.fromNodeID = fromNode;
		this.toProgram = toProgram ;
		this.toNodeID = toNode;
		this.broken = false ;
		

		fromProgram.addOutputPipe( fromNode, this );
		toProgram.addInputPipe( toNode, this ); 
	}
	
	public Pipe( long id,
				  PipelineProgram fromProgram,
				  INodeID fromNode,
				  PipelineProgram toProgram,
				  INodeID toNode)
	{
	    setup(id, fromProgram, fromNode, toProgram, toNode);	
	}
	
	public Pipe()
	{
	    broken = true;
	    // intended to be used with setup;
	}

	public IPipelineProgram getFromProgram() 
	{		
		return fromProgram;
	}

	public IPipelineProgram getToProgram() 
	{
		return toProgram;
	}
	
	public INodeID getFromNodeID() {
		return fromNodeID;
	}

	public INodeID getToNodeID() {
		return toNodeID;
	}
	
	public IOutputNode getFromNode()
	{
	    return fromProgram.getIONodes().getOutputNode(fromNodeID);
	}
	
	public IInputNode getToNode()
	{
	    return toProgram.getIONodes().getInputNode(toNodeID);
	}
	
	public void setFromNodeID(INodeID fromNode) {
		this.fromNodeID = fromNode;
	}

	public void setToNodeID(INodeID toNode) {
		this.toNodeID = toNode;
	}
	
	public long getID() 
	{
		return id;
	}
	
	public boolean equals(Object obj) 
	{
		if( obj instanceof Pipe )
		{
			return (id == ((IPipe)obj).getID()) ; 
		}
		return false ;
	}
		
	public int hashCode() 
	{	
		return (int) (id % Integer.MAX_VALUE);
	}

	
	public void destroy() 
	{
		this.broken = true ;
		fromProgram.removeOutputPipe(fromNodeID);
		toProgram.removeInputPipe(toNodeID);
	}

}
