Month: November 2011

  • Pass Enum values by reference in Java

    Title might be little misleading since in java we pass always by value, but we can mimic passing by reference.

    /**
     * This class allows us to pass values by reference ranter than value.
     * Not directly as java just passes reference of the object by value but indirecly 
     * @author Greg
     *
     * @param 
     */
    public class IndirectReference {
    	public E ref;
    
    	public IndirectReference(E ref) {
    		this.ref = ref;
    	}
    
    	public void set(E ref) {
    		this.ref = ref;
    	}
    }
    

    Example usage

    Just a snippet copied from my interpreter

    	IndirectReference signal = new IndirectReference(ControlSignal.NOOP);
    	if(signal.ref == ControlSignal.BREAK){
    	    break;
    	}	
    
  • Regex to remove DOCTYPE prolog

    While using HTML Tidy I needed to remove the DOCTYPE prolog to prevent
    ‘org.xml.sax.SAXParseException: Already seen doctype.’ exception.

    Regex is quite simple, only catch is that we need to make sure we include the \n\r in our selecton and make it not greedy.

     convertedData = convertedData.replaceAll("", "");	
    

    This will consume multiline as well as single declarations

    /*		
    	
    */