Skip to main content

Posts

Using HTML5 SessionStorage in forms.

The two local storage mechanisms in HTML5 , 'LocalStorage' and 'SessionStorage' are really nifty when you need to store the data temporarily but don't want to involve any server side interaction. I wanted to create a small HTML-form page which would allow user to enter data in various fields and on submitting the form, just show the data submitted in a text-format (easy for copy-paste). And if needed allow the data to be edited by repopulating the data in the fields. Something like this: On submitting, the entered data shows up like this: On reloading the page, once again we get the first page with data populated in the fields. Pretty cool eh? I decided to use sessionStorage for this. Idea is very simple (and you will notice that implementation is as simple!). Before starting with sessionStorage or JS code, let's take a look at the form HTML code: <form id="bct" > < label>Problem Description/Current Behavior: < /l...

How does HashMap work in Java? (HashMap internals)

HashMaps are probably one of the most used and definitely one of the least understood Java classes. In this post let's look at the source code of HashMap.java to understand how is data inserted and retrieved from a HashMap. We know that HashMaps work on the principle of 'hashing' and put(key, value) is used to store a key and corresponding value and get(key) is used to retrieve the value for given key. Below image shows how HashMap stores the data. The blue squares represent the 'bucket' or indexes in the array, determined on the basis of hash value for each key . (We will see later, exactly how the hash value is calculated) At each index of the array, is stored a linked list which has nodes of the type 'Map.Entry'. Each node stores the key/value pair. Why is the linked list needed? Because two unequal objects can have same hash value (read more about equals and hashcode here ). Remember that a HashMap will store values as long as different keys ar...

Java Design Patterns : Adapter Pattern

Whenever and wherever you will read about Adapter pattern you will come across example of electric adapters. Be it a 3-pin to 2-pin adapter, round pin to flat pin adapter or AC to DC adapter, its purpose is to act as an intermediary between electrical equipment and the socket. Above is a representation of a 3-pin to 2-pin adapter which exposes a 3-pin socket which takes in a 3 pin plug and gives a 2 pin output. (These 2-pins can be plugged into any 2-pin socket.)  Note that the adapter contains a 3-pin plug inside and behavior of its 3 pins is altered to suit the behavior of the adapter pins which will fit in the 2-pin socket. Now if we interpolate this in terms of Java concepts, the 3-pin socket is an interface and every 3-pin plug implements this interface. Let's call this as 'adaptee' interface. Similarly every 2-pin socket is also an interface which is implemented by every 2-pin plug. Let's call the 2-pin socket (which our adapter is trying to be...

Java Design Patterns: Decorator Pattern

Before talking about Decorator design pattern, think of decorators in real life. Let's consider a very common  example. In an ice cream parlor you can order ice creams in Vanilla, Butter Scotch, Chocolate and Pineapple flavors with or without an assortment of toppings. Here are your choices: Chocolate sauce Strawberry sauce Maple Syrup Fruits and nuts or just a Cherry You can even do a mix and match of toppings according to your liking. So you can have a scoop (or two) of vanilla ice cream with Strawberry syrup topped with fruits and nuts and a cherry.  Each of these toppings is a 'Decorator'. As evident from name itself, Decorators in real life 'decorate' an existing object. They ADD something to an existing object. Decorator design pattern also solves the same problem. If you think of implementing this system, your first instinct would be to create a base class Icecream and make Vanilla , ButterScotch , Chocolate and Pineapple extend it. Co...

Hands on review: Yahoo! Mail app for Windows 8

Before you start mocking me for using Yahoo! Mail, let me explain that this is NOT my primary mail account and I use Gmail and Outlook.com for my daily use. Yes, this Yahoo! account was once my daily haunt but that was during college days and those days are long gone. I don't use Yahoo! Mail much but still there are some group subscriptions and I just log in once in a while to see what's happening in those forgotten groups. And due to this reason, I have always kept track of changes in Yahoo! Mail and the fact is that you can blame them for everything but not really lack of trying. Though most of those tries just ended up cluttering the UI and adding somewhat needless features. However, now we hear that new CEO Marissa Mayer is focusing once again on the mail and first major product to come out is Yahoo! Mail app for Windows 8. So how is it? The Login screen is pretty neat and purple background with an envelope watermark is very pleasing, very Yahoo!. However why ...

Java Design Patterns: Simple Factory pattern

Design Patterns are much talked about yet much feared of, much explained and yet much confused topic. A lot of fundamentals used in Design Patterns are actually application of basic Object Oriented Design principles. Take example of 'Simple Factory' (or just 'Factory') pattern, which is not actually a pattern but more of a good design practice. Think of a scenario where you need to create a different variant of some class depending on some condition. E.g. You have a base class House and various sub classes extending House, say Villa, Flat and Penthouse. Now depending on some user input, say price range, you need to use objects of Villa, Flat or Penthouse to perform some function. public double getHouseArea(double higherPriceRange){     House house;     if(higherPriceRange < 4000000){       house = new Flat();     }else if(higherPriceRange < 10000000){       house = new Villa();     }else{ ...