/*Stub.java*/
/* A Java class for
 * LineImpedance.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */ 

public class Stub extends Object /*implements Cloneable */{

	private double Zchar;
	private double length;
	private double position;
	private static final double beta=2.0*Math.PI;
	private boolean isParallel;
	private boolean isOpen;
	private boolean isEnable;
	private Complex Ystub, YAsIfOn;
	
	public Stub(){
		this(0.25,0.25,50.0,true,false,false);
	}
	

	public Stub(double length, double position, double Zchar, boolean isParallel, boolean isOpen, boolean isEnable){
		this.length=length;
		this.position=position;
		this.Zchar=Zchar;
		this.isParallel=isParallel;
		this.isOpen=isOpen;
		this.isEnable=isEnable;
		Compute();
	}
	
	/*
	public Object clone(){
		try{
			Stub ZZ = new Stub();
			ZZ.length = this.length;
			ZZ.position = this.position;
			ZZ.Zchar = this.Zchar;
			ZZ.isParallel=this.isParallel;
			ZZ.isOpen=this.isOpen;
			ZZ.isEnable=this.isEnable;
			ZZ.Ystub = this.Ystub;
			return ZZ;
		}
		catch (Exception e){
			return null;
		}
	}
	*/
	public final String toString(){
		String tmp;
		tmp="Stub: ";
		if(this.isOpen){tmp=tmp+"Open, ";}
		else{tmp=tmp+"Short, ";}
		if(this.isParallel){tmp=tmp+"Parallel, ";}
		else{tmp=tmp+"Series, ";}
		tmp=tmp+"Zchar="+this.Zchar+", ";
		tmp=tmp+"Length="+this.length+", ";
		tmp=tmp+"Position="+this.position+", ";
		tmp=tmp+"Admittance="+Ystub;
		if(isEnable){tmp=tmp+"Stub Enable";}
		else{tmp=tmp+"Stub Disable";} 
		return tmp;
	}

	public final synchronized void Open(){
		isOpen=true;
		Compute();
	}
	
	public final synchronized void Short(){                                                 
		isOpen=false; 
		Compute();
	}

	public final synchronized void Parallel(){
		isParallel=true;
	}
	
	public final synchronized void Series(){
		isParallel=false;
	}

	protected final void Compute(){
	    if(isEnable){
		try{
		  if(isOpen){
		   Ystub=new Complex(0.0,Math.tan(beta*length)/Zchar);
		   YAsIfOn = (Complex)Ystub.clone();
		  }
		  else{
		   Ystub=new Complex(0.0,-1.0/(Math.tan(beta*length)*Zchar));
		   YAsIfOn = (Complex)Ystub.clone();
		  }
		}
		catch(Exception e){}
	    }
	    else{
		Ystub = new Complex(0.0,0.0);
		if(isOpen){YAsIfOn = new Complex(0.0,Math.tan(beta*length)/Zchar);}
		else{YAsIfOn = new Complex(0.0,-1.0/(Math.tan(beta*length)*Zchar));}
		
	    } 
	}
	
	

	public final Complex getYstub(){
		return Ystub;
	}

	public final Complex getYAsIfOn(){
		return YAsIfOn;
	}

	public final double getLength(){
		return this.length;
	}

	public final synchronized void setLength(double length){
		this.length=length;
		Compute();
	}

	public final double getPosition(){	
		return this.position;
	}

	public final synchronized void setPosition(double position){
		this.position=position;
		Compute();
	}

	public final synchronized void setZchar(double Zchar){
		this.Zchar = Zchar;
		Compute();
	}
	
	public final double getZchar(){
		return Zchar;
	}

	public final synchronized void isEnable(boolean isEnable){
		this.isEnable=isEnable;
		Compute();
	}

	

	public final boolean isEnable(){
		return isEnable;
	}

	public final boolean isParallel(){
		return isParallel;
	}

	public final boolean isOpen(){
		return isOpen;
	}
}
