Wednesday, February 11, 2009

Maintaining and Managing variables stored in application's session

In one of our project, we were storing frequently used application related data into the session, but now as the project is growing incrementally, storing new data / modifying existing data into session has became very confusing and difficult to manage.

I was just thinking about the workaround for coming out of this havoc, and voila, I got one :). This workaround I've implemented in our project. I would like to share the idea which brought us out of this mess.

Idea (Workaround): We can use one singleton java class, which has one HashMap variable. There will be custom methods for getting, setting and removing (operations same as that of session operations) values from HashMap. Now we can use this implementation for storing data (which was supposed to be stored into the session) into the HashMap present in this class, and then we can put the object of this java class (we'll be having one object for each session) into the session. So, at the session level, there will be only one object present in the session, and now it's lot easy to maintain and manage session variables (now HashMap data). Also, there will be only two session operations in each class, first: getting this java class object from session, second: storing back this java class object back to the session.

Implementing this thing was really fun and gave us lot of flexibility to maintain and manage session variables... :)

Thursday, February 5, 2009

-C-O-N-C-E-P-T- Series : Java

Difference between using "" and toString() method for converting object value to String:

Case I - Using "":

-----------------------------------------------------------------
BigDecimal dec = new BigDecimal("1234567");
String str = "" + dec; // converting dec to String
System.out.println("str: "+str);

o/p:
str: 1234567
-----------------------------------------------------------------

-----------------------------------------------------------------
BigDecimal dec = null;
String str = "" + dec; // converting dec to String
System.out.println("str: "+str);

o/p:
str: null
-----------------------------------------------------------------


Case II - Using toString() method:

-----------------------------------------------------------------
BigDecimal dec = new BigDecimal("1234567");
String str = null;

if( dec!=null ) {
str = dec.toString(); // converting dec to String
}

System.out.println("str: "+str);

o/p:
str: 1234567
-----------------------------------------------------------------

-----------------------------------------------------------------
BigDecimal dec = null;
String str = null;

if( dec!=null ) {

str = dec.toString(); // converting dec to String
}

System.out.println("str: "+str);

o/p:
str: null
-----------------------------------------------------------------

In first case, we don't require null check, but doesn't has readability.
In second case, we require null check, but has readability.

Each one has its own ease of use.


_________________________________________________________________________________________________________________

"Look at the sky. We are not alone. The whole Universe is friendly to us and conspires only to give the best to those who dream and work."

- Dr. A P J Abdul Kalam
_________________________________________________________________________________________________________________