Thursday, 16 September 2010

Extending Hibernate/HQL with Database Proprietary Stuff

http://www.roseindia.net/hibernate/firstexample.shtml http://stackoverflow.com/questions/740293/is-it-possible-to-do-standard-deviation-in-nhibernate

Volatile - A Definition (Warning:Swear words)

I seem to have stumbled upon a definition of volatile at bash.

the compiler goes "oh look, it's checking the same memory location over and over. let's stick it in a hardware register and just use THAT value so it's faster", but volatile says "no way bitch. shit happens to this memory location regardless of what's going on in the program itself. keep checking the actual location or so help me god I will break your face"

Friday, 10 September 2010

Strings are immutable

Sometimes I come across something like this:
String myString = "Jack";

// Concatenates the specified string to the end of this string.
// (taken directly from JavaDoc)
myString.concat(" and the Beanstalk");

And people are very surprised when they see that the String myString has not been changed.

It is very confusing, the javadoc helps if you've noticed that a new String is returned by the concat method. Strings cannot be changed! Therefore, either use StringBuffer or be prepared to create a huge pile of new String objects.

Thursday, 2 September 2010

Calling JSP directly

Calling a JSP directly, (from for example within another jsp) in order to redirect its input and output to, for example, a PDF writer, can be done using the following:

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * Response wrapper, that creates its own readable buffer.
 * @author Turbo
 */

public class WebResponseWrapper extends HttpServletResponseWrapper 
{
    
    private StringWriter output;

    public WebResponseWrapper (HttpServletResponse response){
    super(response);
    output = new StringWriter();
    }

    public String getBufferContents()
    {
        return output.toString();
    }

    @Override
    public void flushBuffer() throws IOException {
    }
    
    @Override
    public PrintWriter getWriter() throws java.io.IOException
    {
        return new PrintWriter(output, true);
    }

    @Override
    public void reset() 
    {
    }

    @Override
    public void resetBuffer()
    {
    }

}
/**
 * Class that makes it possible to add some parameters to the existing
 * http request, useful for redirecting.
 */
 
class MyRequestHelper extends HttpServletRequestWrapper
{
    Map<String,String> theMap = null;
    
    public void set(String param, String value)
    {
        theMap.put(param, value);
    }
    
    public MyRequestHelper(HttpServletRequest request)                
    {
        super(request);
        theMap = new HashMap<String,String>();
    }
    
    @Override
    public String getParameter(String param)
    {
        if (theMap.containsKey(param))
        {
            return theMap.get(param);
        }
        return super.getParameter(param);
    }
}

Using these two classes, redirecting a jsp page becomes as simple as:

/* setup wrappers */
WebResponseWrapper resWrapper = new WebResponseWrapper(response);
RequestDispatcher rd = context.getRequestDispatcher("/some/jsp/thing/myJsp.jsp");
MyRequestHelper mrh = new MyRequestHelper(request);

/* change/add some parameters */
mrh.set("forEmail""true");
mrh.set("key", correctKey);
mrh.set("user", userName);

/* issue include to the requestdispatcher */
rd.include(mrh, resWrapper);

/* retrieve output */
String email = resWrapper.getBufferContents();

NullPointerException - Most Common Java Bug In Existence

Unless you are what is called "the Perfect Java Programmer", it is likely that you've perpetrated the NullPointerException at least once (and probably a lot more) in your life.

A lot of the time, I see code like the following:
if (propertyValue.equals("filename.txt"))
{
    // do stuff
}
When it should really be more like this:
if ("filename.txt".equals(propertyValue))
{
    // do stuff
}

Now, as you can see when you try this (with propertyValue = null;), a NullPointerException does not take place.

The reason is in the equals method. Contract states that x.equals(null) == false.

If you're new at Java, you might be rather surprised that something like "somestring".equals() works. Remember that Strings are immutable objects, and therefore there's really no reason not to try something like this.

With that said, let's try and find a common implementation for the equals method. Eclipse seems to do a pretty good job of it:

public class MyObject
{

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            // it is the same object
            return true;
        }
        if (obj == null)
        {
            // oh dear
            return false;
        }
        if (getClass() != obj.getClass())
        {
            // not the same class
            return false;
        }
        MyObject other = (myObject) obj;
        if (!this.field().equals(obj.field())
        {
            // (one of) the fields is not the same!
            return false;
        }
        // couldn't find any differences!
        return true;
    }

    @Override
    public int hashCode() 
    {
        return field();
    }

}
The link to http://community.jboss.org/wiki/EqualsandHashCode might be a good one.