package descriptors;

public class LocalDesc extends PulseSignalDesc {
	//for LOCALs
	private String lpath;
	
	public final String pathFormat = "/pulse/local/path";
	public final String getPathFormat(){ return pathFormat; }
	
	/** Descriptor for LOCAL signal */
	public LocalDesc(int pulse, String path){
		this.pulse = pulse;
		this.lpath = path;
	}	
		
	/** Return path to signal file in disk cache. */
	final public String toString(){
		String path;
		
		path = "/" + pulse + "/local/" + lpath;
		return path.toLowerCase();			
	}
	
	/** Set descriptor from path */
	final public void setFromPath(String path){
		try{
			path = path.toLowerCase();
			
			//break path into parts
			String parts[] = path.split("\\/");
			int j=0;			
			while(parts[j].length()<=0) j++; //move to next non-blank entry
			
			//next part is pulse number			
			try{
				pulse = Integer.parseInt(parts[j]);			
			}catch(NumberFormatException err){ pulse = 0; }
			if(pulse <= 0)
				throw new IllegalArgumentException("Invalid pulse number '"+parts[j]+"'. ");
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
					
			if(!parts[j].equalsIgnoreCase("local"))
				throw new IllegalArgumentException("setting LocalDesc object on non local signal '"+path+"'");
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
				
			lpath = "";
			for(;j<parts.length;j++)
				if(parts[j].length() > 0)
					lpath += "/" + parts[j];				
			lpath = lpath.substring(1); //string first /
						
		}catch(NullPointerException err){
			throw new IllegalArgumentException("Invalid path for LocalDesc, expecting " + pathFormat);
		}
	}

	
}
