Tuesday, January 31, 2012

Tuples in Java

I came across the concept of tuples in C# which is quite interesting and useful. A tuple is a data structure that has a specific number and sequence of elements.  C# introduced this feature since .NET 3.5. It supports upto 8 generic objects. Unfortunately Java haven't got one yet.


In my current project there was a need for a similar Data-structure, where I had to pass back a large amount of data of a particular object. But I was interested only in just two properties of that object. I could have created a proxy class, but that would have ben a sort of over engineering (Which I don't want to do). At that time I thought of a simple attempt to write a Tuple. I wrote one and it's really reusable and clean. You can find the  the code snippet below.

public abstract class Tuple<T1, T2, T3, T4>{


    protected T1 t1;
    protected T2 t2;
    protected T3 t3;
    protected T4 t4;
    
    public static    < T1, T2> Tuple <T1, T2, ?, ?> create(T1 aT1, T2 aT2){
        return new MultipleTuple(aT1, aT2);
    }
    
    public  T1 getFirstItem(){
        return t1;
    }
    
    public T2 getSecondItem(){
        return t2;
    }
    
    public T3 getThirdItem(){
        return t3;
    }
    
    public T4 getFourthItem(){
        return t4;
    }

}

A MultiTuple


final class MultipleTuple<T1, T2> extends Tuple {
    
     MultipleTuple(T1 aT1, T2 aT2) {
        t1 = aT1;
        t2 = aT2;
    }
}


Example


String employee = "Diego Maradona";
double renumeration = 56.5;
        
Tuple  tuple = Tuple.create(employee, renumeration);


System.out.println(tuple.getFirstItem());
System.out.println(tuple.getSecondItem());




You can extend as many classes you want to create. You might need to restrict the visiblilty and make them final so that it's secure. Or you could even extend the MultipleTuple(Hoping it's no longer final) class for the next tuples. Make sure the create method is overloaded with the right parameters.




Happy Hacking.

Wednesday, January 25, 2012

IE Caches Jquery Ajax Request

Do you know what's the time now, while I write this post? It's 2:00 am in the morning. I was asked to fix a Jquery issue happening only in Internet explorer.

There was a piece of code which makes an ajax request to the server periodically. But this code only works  once.  This problem is seen only in Internet explorer. After scratching my head for a while I finally realized that IE caches ajax requests.

The solution is to use the following code:


$.ajaxSetup ({
   cache: false
});

Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL. So if you have  long query string as the ajax request this might be a problem. I am  not sure, I but I guess so.

Happy Hacking!


Thursday, January 12, 2012

Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() using Spring JdbcTemplate

My colleague was getting this exception for a Spring JdbcTemplate based code which I had written for some simple Mysql dao functionalities. The funny point was that we were both using the same version of Mysql drivers, but I was using 32bit Mysql 5.5 but my colleague was using the 64bit version.

This quick fix solves the problem:


KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
 public PreparedStatement createPreparedStatement(Connection aConnection) throws SQLException {
                    PreparedStatement preparedStatement = aConnection.prepareStatement(SAVE_USER,
PreparedStatement.RETURN_GENERATED_KEYS);
                    preparedStatement.setString(....);
                    ....
                         preparedStatement.setString(....);
                    return preparedStatement;
                }
            }, keyHolder);
 user.setId(keyHolder.getKey().intValue());

A few other fellow bloggers had given suggestions like switching the Mysql dirvers, but it's didn't help me at all. I had tried all the last 10 Mysql driver releases. I am not sure if its a programming error or a Mysql bug, but the above code does fix the problem!

Happy Hacking :)

Adobe Flash Builder 4.5 Hangs

I recently bought Adobe Flash builder 4.6 Premium. It's pretty good but hangs quite a lot. As it was build on top of eclipse, this simple trick helped me to get rid of the problem.

  • Got Flash Builder Home/eclipse 
  • Open eclipse.ini
  • Change the vmargs( Xms & -Xmx) to  -Xms512m-Xmx1024m ( by default 256 & 512)
  • Save and restart Flash Builder
Note: You could change the Xms and Xmx value to the value suitable to your system. These values are just samples.

Happy hacking :)