Skip to main content

Posts

Showing posts from 2012

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. Correc

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{       house = new Penthouse();     }          return ho

Java is always Pass-By-Value. Never Pass-by-Reference. Explanation.

We often read about how Java is pass by value but very often there is a confusion that behavior is different in case of primitives and object references leading to following statement (which is kind of 'aid to memory' rather than anything else): Java does pass-by-value for primitives and pass-by-reference for object references. Consider following code snippet. public class House{   String type;   public  House (String type){     this. type =  type ;   }      public String get HouseT ype (){    return this. type ;    }      public void set HouseT ype (String  type ){    this. type =  type ;   } } public class MainClass{   public static void main(String[] args){     House myHouse; ///Line 0     myHouse = new House("Flat"); ///Line 1     System.out.println("Output1: "+myHouse. get HouseT ype ());          foo(myHouse); ///Line 2     System.out.println( "Output2: "+ myHouse. get HouseT

What does it mean by Vector being synchronized?

So you found that there is a very famous (and hence commonly found on all the question sites and hence not asked anywhere anymore) interview question: What is difference between Vector and ArrayList? And the answer is simple: Vector is synchronized but ArrayList is not. Fine. But what does this mean? If you understand how synchronized works I will tell you the simplest way to understand the difference. Just look at the implementation of Vector ( here ) and ArrayList ( here ). In the implementation of Vector you will find that many of the methods are synchronized. size(), indexOf(), isEmpty(), set() etc. all are synchronized and hence all these methods can be accessed by one thread at a time. So the state of the Vector can be modified/accessed by only one thread at a time. On the other hand, none of the methods in ArrayList are synchronized. This is what Java doc says: Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concur

Hands On Review : AOL's Alto Mail

I am a sucker for new online services, specially emails and when AOL announced its new offering Alto Mail, I was really excited as anything coming on the heels of Microsoft's Outlook.com with a premise of making mail arrangement a more visual affair had to be definitely worth a try. So does it really deliver the goods? First of all let's get this straight: Alto Mail is NOT a new Email service, it is just a new web based email client which you can connect to view your Gmail/Yahoo/AOL/iCloud content. (Only Yahoo.com accounts will work, no ".co.in" or ".co.uk" or any of the other Yahoo local domain names work with Alto). Once you login to Alto with any of your above mentioned accounts, give it a few hours (well it will depend on the size of your inbox and folders) to pull in all the emails/contacts from your account. But even before the import is completed, any emails sent from Alto will reflect in your 'Sent' folder of your linked mail account.

Javascript : How to setfocus on an element created at runtime?

In my web page, I created a new row in my data table at runtime. The new row consisted of two text boxes and I needed to set focus on first text box in the row. I got the id of the text box element by inspecting the HTML code and tried to do the following: In java code: inputId = ; executeJavaScript("selectDPTypeRow('"+inputId+"')" );   ('executeJavaScript' is a custom method to execute JS code from Java code) In JS code: function selectNewRow(textBoxElementId){        var textBox = document.getElementById(textBoxElementId);        textBox.setFocus(); } But this never worked as 'textBox' always gave null. I was baffled as to why should the element be not found in the page DOM since inputId was always correct. Later I realized that by the time the above code is called, the new elements have not yet been rendered on the page and hence the elements are not found. To overcome this I introduced a timelag of half a second

Quick Java : How to remove duplicate elements from an ArrayList or Vector

There are various algorithms to do so and if you want to implement any of those you are free to do so, but if you are using java this can be done in two lines! As you know Sets don't allow duplicate elements, so why not use this property. Convert your ArrayList (or Vector or any other Collection for that matter) to Set and convert it back to your Collection. In below code snippet, I have an ArrayList of integers named 'listOfNumbers'. HashSet uniqueNumbers = new HashSet (listOfNumbers);   Using HashSet will mess up the order of your elements, so if you are concerned about the order, use LinkedHashSet. After conversion to set the duplicates are removed, convert it back to Collection object. ArrayList listOfUniqueNumbers = new ArrayList (uniqueNumbers); And you are done! How cool is that :)

ADF: How to remove components at run time!

In an earlier post we saw h ow to add ADF components in a page at runtime . It is easy enough. After some time I got a bit confused as to how to remove components, either predefined or these dynamically created components at runtime. This is easy too and I cursed myself for getting stuck over such a trifle issue. Let's assume that you have a panel group layout whose child components you want to remove and you have bound this panel group layout in your backing bean. In the event handler method of the action on which you want the components to be removed, do this: getMyPanelGroupLayout().getChildren().clear(); Simple isn't it? Since ADF exposes children of any component in form of a List, adding and removing any component is as easy as adding or removing to a list. Here I have removed all the child components of the panel group layout using 'clear()'. If you want to selectively remove components, based on id or name or any other property, you can traverse the l

ADF: Using Input Range Slider (RichInputRangeSlider)

Input Range Slider is one of the coolest components of ADF which is not only very intuitive for users but also gives a 'cool' look to the page :) So how do we get minimum and maximum values selected on the slider? Among other properties of this component are 'minimum' and a 'maximum' and then there is 'value'. This causes some confusion as to  how can we get the values selected on the slider. This is not half as complex as it seems! Let's focus first on the 'value'. The value of this component is returned in form of a oracle.adf.view.rich.model.NumberRange object. oracle.adf.view.rich.model.NumberRange minMax = (oracle.adf.view.rich.model.NumberRange)getInputRangeSlider().getValue(); You can extract both the minimum and maximum values from this object like this:         int startValue = minMax.getMinimum().intValue();         int endValue = minMax.getMaximum().intValue(); and that's it. You have both the values return

What is reordering and how does Volatile help?

Following excerpt is from  JSR 133 (Java Memory Model) FAQ What is meant by reordering of instructions? There are a number of cases in which accesses to program variables (object instance fields, class static fields, and array elements) may appear to execute in a different order than was specified by the program. The compiler is free to take liberties with the ordering of instructions in the name of optimization. Processors may execute instructions out of order under certain circumstances. Data may be moved between registers, processor caches, and main memory in different order than specified by the program. For example, if a thread writes to field  a  and then to field  b , and the value of  b  does not depend on the value of  a , then the compiler is free to reorder these operations, and the cache is free to flush  b  to main memory before  a . There are a number of potential sources of reordering, such as the compiler, the JIT, and the cache. The compiler, runtime, a

How to check individual cookies in IE, Firefox and Chrome

How often has this happened with you that you visited a site and after that which ever site you went to, you started seeing the ads of the site you visited? If you know anything about web and browsers you know it is the cookies that were planted by the site. Cookies are nothing but small text files containing some information about the client machine. Often harmless and mostly used by sites to give you a tailor-made experience whenever you visit the site next, but very annoying in some cases like the one I just mentioned. Browsers give you facility to clear either the entire browsing history (which includes visited sites, cookies, cache, form data etc.) or individual items and you can delete all the cookies in one go. But if you want to see which sites have planted cookies in your machine and clear only the ones you want to, read on. Chrome:  Click on the 'Wrench Menu'. Click on 'Show Advanced Settings'. Under 'Privacy', click on 'Content Settings