/*Stub.java*/

public class Stub extends Object /*implements Cloneable */{

	private double Zchar;
	private double length, length_meters;
	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,true);
	//}
	

	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 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(length < 0.0){length = 0.0;}
	    if(position < 0.0){position = 0.0;}
	    if(isEnable){
		try{
		  if(isOpen){
		   Ystub=new Complex(0.0,Math.tan(beta*length)/Zchar);
		   YAsIfOn = (Complex)Ystub.clone();
		  }
		  else{
		   if(length==0.0){
			Ystub=new Complex(0.0,-1.0/(Math.tan(beta*(length+0.5))*Zchar));
		   }
		   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 synchronized void setLength_meters(double length){
		this.length_meters = 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;
	}
}
