Sunday, May 27, 2012

It was 26-May-2012. Saw this beautiful snake in the passage of my Wing at the ground floor at around 9PM. I think it was ~2.5 Feet in length and was so shiny and attractive!!

Luckily, my friend Ankur Mehta heard someone saying "Kill it...here it is...." etc with a thick stick in his hand. Ankur shouted from where he was, asking not to kill and wait.

Montane Trinket Snake(Non Poisonous)

Both of us went there and saw it. It was "Montane Trinket Snake". It was a relatively common snake and NON VENOMOUS. Luckily we were able to identify it and convinced that guy not to kill it, as it was NOT harmful at all. It must be after a rat/mice/frog probably.

Actually, more than 80% times we encounter non-venomous snakes, but its just fear (which I completely agree with) which makes us feel that it is a threat. But believe me, more than you, the snake itself will try not to have any encounter with Human. Their first reaction is gonna be, to run away. And we should give due respect to it and let it go if we encounter it in a open area like garden/bushes or something, as they must be living there for quite a while and if there has been no incidents so far, means they are not a threat for sure.

Infact even if a Poisonous snakes bites, more likely its gonna be a Dry bite, ie. simply a bite with NO poison injected. It would use poison as a last resort where it is provoked.

Montane Trinket Snake(Non Poisonous)

Few more people came by that time, and it was looking like a circus show and most people preferring to kill it because of fear. But I was trying NOT to handle it in-front of all, as in future that simply might arouse few over-confident guys to start handling it and which might cause injuries to such sweet creatures.
So later in the night, went with Ankur and we took it to the backside of the society where there is a open space with bushses etc. so that it can find a suitable place to hide as well as something to eat as there are good number of frogs as well there.

This was my first handling of a snake in wild. Although it was non-poisonous, frankly speaking my heart was beating at double pace. Because, when I lifted it with hook with his tail in my left hand, it started twisting its body, and I had to keep rotating it in same direction in my hand, else I was afraid that its twist would have damaged its own muscles because of twists.

But luckily, all went right and thanks for Ankur's help. Also thanks to all guys in society who co-operated and didn't panic or over reacted!!

Saturday, April 28, 2012

Content of this post expects that you are already familiar with Core Spring Concepts like IOC etc. Other very important aspect of the Spring is its Scopes of the Bean which we define in the configuration either via Annotation or via xml.

What Is Spring:

But to give little brief, Spring is a framework which we can use to create an architecture where Classes and its dependencies can be fulfilled using the Declaration rather than Handcoding. So basically, most objects that you use/need for ur program during runtime, will come from the Factory which we known as Spring Framework. I know, I am using some layman terminology here, but I think this way it will be lottle clear to beginners.

So, you will create most important objects of you application like some Service Objects, some DB connection/pool objects etc. via Spring. And it has its own advantage which you can get know from many online tutorials.

What is Bean: Classes which are configured to get created via Spring. Every instance of such classes are called "bean"

Want to focus on "Singleton" scoped bean here. It makes sure that only one instance of that bean will be present in the "Spring Container" where it is defined. Please dont get fooled by thinking that it is same Singleton object in our regular day to day life code.

Singleton scope does have an advantage that it can give:
=> High throughput: because the same object is getting referred and used every time its required, so there is no need to create a new instance again and again.
=> Low Memory Foot print: because there is no need to new instance again once a instance got created.

But with aforesaid advantages, developer must also take due care else will fall in a big trouble in no time. As you know, only single instance is available in that container during runtime, so everytime when it is referred or asked for, that very instance will be returned by Spring factory (Context). Here come the possible gotcha, where same bean might be getting referred by multiple threads performing may same/different method calls on that object.So, by-chance, if there are any state, that you maintain in that singleton object, may be visible to other method calls as well. And this might be un-desirable.
By saying state we mean, some value in the instance variables of the bean. One issue I sufferred very recently can be referred by clicking here.

Sunday, January 22, 2012

a. public boolean equals(Object obj) : Determines if the current (this) object and passed object (obj) is same/equal to each other, more precisely from the business front of view, whether "this" and obj represent same data or NOT even if they both may be two different physical object in the memory.

b. public int hashCode() : Determines the hash value of the current object to be used for direct access mechanisms like in Set, Map etc.

To me, one of the reason why this is happening is that, most people (including me as well) generally end up putting instances of String, Integer, Float, Double etc. in Set or as Key in Map. And all the magic to make sure that access will be fast is already provided by that respective class. How??, No prize to guess those two methods ;) (equals and hashCode).

But you will start facing issues once started using your custom class to be added in Set or used as Key in the Map. Please continue reading to understand it in details.

What actions are performed by HashMap's Put method internally? Assuming ck1 is NOT NULL

1. It calls hashCode method of ck1 and determines the bucket where it can drop the data (user)

2. If Bucket is empty, its good and simply put the KEY + Data in the bucket.

3. But, it might happen that, there is already some Key in the map (effectively pointing to same bucket) with the same hashCode, in that case:

a. Apply equals method of current object (ck1) with all Key objects in that bucket until a match is found (ie. equals method returns true) OR all KEY objects in that bucket checked and confirmed to be NOT equal to ck1. This is the reason why people say insertion is slow in Map, Set, as it has to do some computation before really dumping the data.

b. If a match is found for the KEY (ck1), means he has to replace existing "User" object with User object passed with this method call as parameter. It is as good as passing the same String valued object as KEY again to the HashMap object with a new value in second parameter of the put function.

c. If match is NOT found, then its a new KEY with same hashCode, and will again put the KEY+Data in the bucket.

2. Getting Data back:
Lets assume, we have following code:

HashMap<CustomKey, User> hm = ....;
User user1 = hm.get(ck1);

What actions are performed when get is called on Map (or contains on Set):
a. Call "hashCode" on ck1 to find bucket where it can be residing. Because hasCode on (physically) same and also two object which returns "true" when compared will always return same value no matter how many times we call it, it will refer to same bucket where Key would have been stored while putting using put method.
b. If Bucket is empty, its sure that KEY ck1 doesn't exists in the Map (Set) and it will return null.
c. If Bucket is NOT empty,
i. Compare ck1 with all KEY objects, until a equal KEY object is found OR all Key object are compared (even if only one Key is there in the Bucket) in bucket to find exact KEY object whose Data has to be returned. Don't forget, we have to do this because to different (un-equal) objects can still have SAME hashCode :) But still, this is the reason why people say retrieval is FAST in Map, Set, as it has to seach in only a subset of the whole data in the Map/Set in ideal conditions to reach to the data. Imagine otherwise you will have compare ck1 with many more KEY objects others wise.
d. If No KEY is found which is equal to ck1, return null.

3. So, Where is the issue???
Some of you might have already got answer for above question by now, but for those who still scratching their head or pulling their hair, don't worry ;) , I will try my level best to explain it.

Answer/Problem lies in the Default behavior of equals and hashCode methods. When we declared our own Key class, KeyClass, it is extending Object class inherently. So the implementation of equals and hashCode is already there with this KeyClass as well. But its a very rigid implementation as Object class doesn't know "what business logic you have to decide whether two instances of same class are logically same to you from the business perspective. Also what are the attributes in your class that you can use to make sure that hashCode method returns proper hashCode" following all the guidelines to be followed for hashCode method.
For these reasons, the default implementation of equals and hashCode methods are as followed in the Object class:a. equals: If both object refs are to physically same location in the memory its assumed to be equal.b. hashcode: Basically the default implementation of hashCode() provided by Object is derived by mapping the memory address to an integer value. If look into the source of Object class , you will find the following code for the hashCode.

public native int hashCode();

It indicates that hashCode is the native implementation which provides the memory address to a certain extent.

So if you make hashCode method to return hard-coded value (say 1) for all object of the class KeyClass, then all the KEY+Data will be kept in the same bucket in the Map's put operation. Effectively forcing java to to do sequential search on all the Key objects in the Map while retrieving the data back. If you by mistake apply logic to return some random hashCode even for the same object with multiple call to hashCode method, you might endup with inability to retrieve the data back from the Map as it might point to different bucket while getting data back.

Similarly, if you don't implement equals correctly, you might endupto situation where you can't retireve the data back from the Map/Set.

So, if you add Key + Value object in the Map/Set and then to retrieve the data back from Map, you will need physically same instance of KEY, else code will NOT return data even if the KEY object pass while retrieving is logically same from the business perspective!!!!