/**
 * Copyright (c) 2005 by Kevin Bierhoff
 */
package edu.cmu.cs.l3.trace;

import java.io.Serializable;
import java.io.IOException;


/**
 * @author Kevin
 *
 */
public class BackEdge implements Serializable {
	
	private PathGraph graph;
	private PathGraphVertex origin;
	private PathGraphVertex target;
	private PathGraphEdge dummyIn;
	private PathGraphEdge dummyOut;

	/**
	 * 
	 */
	public BackEdge(PathGraph graph, PathGraphVertex origin, PathGraphVertex target, PathGraphEdge dummyIn, PathGraphEdge dummyOut) {
		super();
		this.graph = graph;
		this.origin = origin;
		this.target = target;
		this.dummyIn = dummyIn;
		this.dummyOut = dummyOut;
		if(dummyIn != null) dummyIn.setBackEdge(this);
		if(dummyOut != null) dummyOut.setBackEdge(this);
	}

	/**
	 * @return Returns the dummyIn.
	 */
	public PathGraphEdge getDummyIn() {
		return dummyIn;
	}

	/**
	 * @return Returns the dummyOut.
	 */
	public PathGraphEdge getDummyOut() {
		return dummyOut;
	}

	/**
	 * @param dummyOut The dummyOut to set.
	 */
	protected void setDummyOut(PathGraphEdge dummyOut) {
		if(this.dummyOut != null && this.dummyOut != dummyOut) {
			// shouldn't happen but this recovery is easy
			this.dummyOut.setBackEdge(null);
		}
		this.dummyOut = dummyOut;
		if(dummyOut != null) 
			dummyOut.setBackEdge(this);
	}

	/**
	 * @return Returns the graph.
	 */
	public PathGraph getGraph() {
		return graph;
	}

	/**
	 * @return Returns the origin.
	 */
	public PathGraphVertex getOrigin() {
		return origin;
	}

	/**
	 * @return Returns the target.
	 */
	public PathGraphVertex getTarget() {
		return target;
	}

    /** Serialization tools **/
    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
	out.writeObject(new Integer(graph.getIndex(origin)));
	out.writeObject(new Integer(graph.getIndex(target)));
	out.writeObject(new Integer(graph.getIndex(dummyIn)));
	out.writeObject(new Integer(graph.getIndex(dummyOut)));
    }

    int origin_ind, target_ind, dummyIn_ind, dummyOut_ind;
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
	origin_ind = ((Integer) in.readObject()).intValue();
	target_ind = ((Integer) in.readObject()).intValue();
	dummyIn_ind = ((Integer) in.readObject()).intValue();
	dummyOut_ind = ((Integer) in.readObject()).intValue();
    }

    protected void setPathGraph(PathGraph pg) {
	graph = pg;
	origin = graph.getVertex(origin_ind);
	target = graph.getVertex(target_ind);
	dummyIn = graph.getEdge(dummyIn_ind);
	dummyIn.setBackEdge(this);
	if(dummyOut_ind >= 0) {
		dummyOut = graph.getEdge(dummyOut_ind);
		dummyOut.setBackEdge(this); 
	}
	else
		dummyOut = null;
    }
}
