Sunday, August 3, 2014

Google Guava CacheBuilder

This cache is mainly used where

  • Where the same data is retrieved multiple times
  • Where the time required to access the data to be small
  • Cache size is limited and known

How to create

Cache to be created with or without CacheLoader. We will see only with CacheLoader. LoadingCache is the Cache implementation that can be created using CacheBuilder and add some properties to it and include CacheLoader to it.
         LoadingCache<String, Person> persons = CacheBuilder.newBuilder() 
                  .initialCapacity(30) 
                  .maximumSize(40) 
                  .recordStats() 
                  .build(loader);
Here Person is the object stored in Cache using key of type String. loader is the CacheLoader. See below on how to create
        CacheLoader loader = new CacheLoader()
        {
            public Person load(String key) throws Exception
            {
                return getPerson(key);
            }
        };
Loader has to be created with load method implemented with the basic operation on on how to load the object using the key.

CacheBuilder

CacheBuilder defines the properties of the cache like

  • Initial capacity: Initial capacity of the cache
  • Maximum size: The maximum size of the cache and the cache evicts the object before the size is reached.
  • Expire after access: Cache automatically removes the entry once the time is elapsed after the last access.
  • Expire after write: Cache automatically removes the entry once the time is elapsed after the last write.
  • Refresh after write: Cache retrieves the data and refreshes once the time is elapsed using load.
  • Record stats : Once this is called, the stats will be recorded on the cache and returns the status when called stats() method.

How to create CacheBuilder

Calling methods explicitly

Call each of the following methods on the CacheBuilder to set the properties 
LoadingCache<String, Person> persons = CacheBuilder.newBuilder()
                .initialCapacity(80)
                .maximumSize(20)
                .refreshAfterWrite(20, TimeUnit.HOURS)
                .expireAfterAccess(1, TimeUnit.DAYS)
                .recordStats()
                .build(loader);

Using CacheBuilderSpec

Set all the parameters in a string comma delimited in CacheBuilderSpec and pass to CacheBuilder.from(spec) to build the cache with the parameters
CacheBuilderSpec spec = CacheBuilderSpec
                .parse("initialCapacity=10,maximumSize=20,refreshInterval=20000s");
        LoadingCache<String, Person> p2 = CacheBuilder.from(spec).build(loader);

How to Access

Accessing the objects in the cache are simple using get and put methods as a HashMap.

Sample Code


        Person p = new Person();
        p.setName("Joe");
        p.setLocation("New Yrok");
        persons.put("P1", p);

        System.out.println("Size of Cache is : " + persons.size());

        Person p1 = new Person();
        p1.setName("Joy");
        p1.setLocation("New Jersy");
        persons.put("P2", p1);

        System.out.println("Size of Cache is : " + persons.size());

        Person d1 = persons.get("P1");
        System.out.println("Person is : " + d1.getName());

        System.out.println("Size of Cache is : " + persons.size());

Output will be

Size of Cache is : 1
Size of Cache is : 2
Person is : Joe
Size of Cache is : 2

Evict an object

There are different ways of evict options
Timed Eviction: Use the methods expireAfterAccess and expireAfterWrite to evict automatically after the time elapsed
Explicit Eviction:  Use the method invalidate(String) to evict one object with the given key and use invalidateAll to remove all.

Other Features

There are other important features that we can make use of when required.

Cache Stats

The cache can record stats when we explicitly call recordStats (Default is off). Once switched on, the stats() method returns the status like
  • Load Count
  • Load Exception Count
  • Load Success Count
  • Eviction Count
  • Hit rate
  • Miss rate etc.

asMap

The complete cache can be returned as map with key and values.

No InterruptedException

The cache doesn't throw InterruptedException but they are designed to make it throw. (But the documentation says (We could have designed these methods to support InterruptedException, but our support would have been incomplete, forcing its costs on all users but its benefits on only some.)

Happy Learning!!!

Sunday, May 25, 2014

Eclipse Templates

There is always a saying of re-use. Write less and do more. We will see how to create the most used templates and use them while writing the code in Eclipse.

Built-in

Eclipse provides some built-in templates for "for", "while", "ifelse" etc. Enter a character and Press Ctrl+Space to get the list of templates defined starting with that character.

Usage:

For example, let's look the following example. I would like to iterate over a list and do some processing over it. Enter "for" and then Ctrl+Space. Eclipse shows a list of options for "for"


Select one of the template and Enter. The selected code will be replaced and the variables that you would like to replace will be highlighted.
Here, iterator is the variable highlighted. Change the first occurrence of the iterator, which will change all the others.
Finally, the code looks like.

How to create custom templates

Goto Preferences. Select Java -> Editor -> Templates. There we can find list of built-in templates provided by Eclipse.
Click on "New", Opens a dialogue and start writing.

Example:

I will show how to create a template for create logger and logging statements.

Logger

${:import(org.slf4j.Logger,
org.slf4j.LoggerFactory)}

private static final Logger ${log_name} = 
   LoggerFactory.getLogger(${enclosing_type}.class.getName());

Logger Statements

Debug Logging
if(${logger:var(org.slf4j.Logger)}.isDebugEnabled()) 
   ${logger:var(org.slf4j.Logger)}.debug(${loggerstring});
${cursor}
Info Logging
${logger:var(org.slf4j.Logger)}.info(${loggerstring});
${cursor}
Error Logging
${logger:var(org.slf4j.Logger)}.error(${loggerstring},${exception_variable_name});
${cursor}

Explanation:


  • All the statements/words which comes starting with $ are the variables which can be replaced
  • ${log_name} is the name of the Logger that we can replace when used the template
  • :import will import the necessary classes when the particular template is used.
  • var(some_class) will be replaced with the variable of type "some_class" in the context.
  • ${cursor} will be the position of the cursor after template insertion and modification of the variable names.

More and more

There are many other default variables/code definitions used.
  • ${cursor} Specifies the cursor position when the template edit mode is left. This is useful when the cursor should jump to another place than to the end of the template on leaving template edit mode.
  • ${date} Evaluates to the current date.
  • ${dollar} Evaluates to the dollar symbol ‘$’. Alternatively, two dollars can be used: ‘$$’.
  • ${enclosing_method} Evaluates to the name of the enclosing name.
  • ${enclosing_method_arguments} Evaluates to a comma separated list of argument names of the enclosing method. This variable can be useful when generating log statements for many methods.
  • ${enclosing_package} Evaluates to the name of the enclosing package.
  • ${enclosing_project} Evaluates to the name of the enclosing project.
  • ${enclosing_type} Evaluates to the name of the enclosing type.
  • ${file} Evaluates to the name of the file.
  • ${line_selection} Evaluates to content of all currently selected lines.
  • ${primary_type_name} Evaluates to the name primary type of the current compilation unit.
  • ${return_type} Evaluates to the return type of the enclosing method.
  • ${time} Evaluates to the current time.
  • ${user} Evaluates to the user name.
  • ${word_selection} Evaluates to the content of the current text selection.
  • ${year} Evaluates to the current year.
Java specific templates are:

  • ${id:field(type)} Evaluates to a field in the current scope that is a subtype of the given type. If no type is specified, any non-primitive field matches. Example: ${count:field(int)}
  • ${id:var(type)} Evaluates to a field, local variable or parameter visible in the current scope that is a subtype of the given type. If no type is specified, any non-primitive variable matches. Example: ${array:var(java.lang.Object[])}
  • ${id:localVar(type)} Evaluates to a local variable or parameter visible in the current scope that is a subtype of the given type. If no type is specified, any non-primitive local variable matches.
  • ${array} is a shortcut for ${array:localVar(java.lang.Object[])}, but also matches arrays of primitive types.
  • ${collection} is a shortcut for ${collection:localVar(java.util.Collection)}.
  • ${iterable} is a shortcut for ${iterable:localVar(java.lang.Iterable)}, but also matches arrays.
  • ${id:argType(variable, n)} Evaluates to the nth type argument of the referenced template variable. The reference should be the name of another template variable. Resolves to java.lang.Object if the referenced variable cannot be found or is not a parameterized type. Example: ${type:argType(vector, 0)} ${first:name(type)} = ${vector:var(java.util.Vector)}.get(0)
  • ${id:elemType(variable)} Evaluates to the element type of the referenced template variable. The reference should be the name of another template variable that resolves to an array or an instance of java.lang.Iterable. The elemType variable type is similar to ${id:argType(reference,0)}, the difference being that it also resolves the element type of an array.
  • ${array_type} is a shortcut for ${array_type:elemType(array)}.
  • ${iterable_type} is a shortcut for ${iterable_type:elemType(iterable)}.
  • ${id:newName(reference)} Evaluates to an non-conflicting name for a new local variable of the type specified by the reference. The reference may either be a Java type name or the name of another template variable. The generated name respects the code style settings. ${index} is a shortcut for ${index:newName(int)}.
  • ${iterator} is a shortcut for ${iterator:newName(java.util.Iterator)}.
  • ${array_element} is a shortcut for ${array_element:newName(array)}.
  • ${iterable_element} is a shortcut for ${iterable_element:newName(iterable)}.
  • ${array} Evaluates to a proposal for an array visible in the current scope.
  • ${array_element} Evaluates to a name for a new local variable for an element of the ${array} variable match.
  • ${array_type} Evaluates to the element type of the ${array} variable match.
  • ${collection} Evaluates to a proposal for a collection visible in the current scope.
  • ${index} Evaluates to a proposal for an undeclared array index.
  • ${iterator} Evaluates to an unused name for a new local variable of type java.util.Iterator.
  • ${iterable} Evaluates to a proposal for an iterable or array visible in the current scope.
  • ${iterable_element} Evaluates to a name for a new local variable for an element of the ${iterable} variable match.
  • ${iterable_type} Evaluates to the element type of the ${iterable} variable match.
  • ${todo} Evaluates to a proposal for the currently specified default task tag.
Happy Learning!!!

Friday, March 21, 2014

CODING Standards

This could be most briefed post in my blog but one of the most important posts.

We are going to talk about most generic and important topic so called "Standards" (which i think to be considered for writing).

In my view, there are no coding standards that to be followed unless you like it. If you ask me write me standards i will write hell of standards. The most important point in developer world should be, the code written by one has to be understandable and able to change by others and the code should be able to evolve with the changes and shouldn't be obsolete in near future atleast .

Few points to consider while coding any kind of program/project.

  • We are capable of doing of miracles with the flexibility and feasibility given by the programming language, IDEs, frameworks, APIs but cleverness lies in how readable is it. So focus on readability.
  • Read the standards provided by the developers. For example Sun/Oracle/etc provided some standards to be followed while writing Java.  The one i like the most is Google java style
  • Every code works unless you find problems in it. Robustness lies in the way it works and how easy to find the issues and fix it.
  • Know what to write where (Read the pros or cons of any standard/usage before applying it otherwise it may go weird.)
  • Configuration v/s Performance. There is a trade of between configuration and performance. If it is too configurable, then it will be less performance and vice versa. (This is not true in all cases).
  • Judge between different ways. There can be different ways to do the same task. Analyze before implementing it. 
  • Time to Live. we heard the time to live only in messaging but developers has to think what is the life time of the code that is being written. Because after some days it may be obsolete because of the evolution of the programming languages and techniques. 
  • Research, research and research. According to me, there should enough research and reason before development, don't develop and research for problem solving. Reason enough to develop. 
Most important point i want to conclude is 
"Read, Research, Understand, Justify and write standard, re-usable and readable code which can evolve with the changing world.".

When you think about most successful frameworks and programs thats the only secret.



Sunday, March 2, 2014

Generics - Java

Generics

The main concept of Generics is to define typed variables and make the compiler to pre-check the types v/s values before actual execution of the program. So, in short, Generic type is a class or an interface which can be parameters for type. 

How to define

Generic is defined by using an angular brace like <T>.
Syntax:
class name<T1,T2,.... >
Example:
List<String> list = new ArrayList<String>();
Most of the classes (In fact all) of Collections are generic. So, when you look at the method of List class.
boolean add(E e);
E could be any type that you have defined while creating the instance of the List.

Points to be noted

So, there are couple of points to be noted before getting into the details of the generics.

What types:

Instead of looking for what types are allowed lets look at what types are not allowed because all types are allowed expect for primitives ;) 

How many:

There is no limit for the number of types that can be defined. We can define any number of types.

Where to define:

Generics can be defined for
  • Classes
  • Interfaces
  • Constructors
  • Methods

Uses

  • Introduce strong types for the custom classes, collections
  • Remove the explicit casting of objects.
  • Autoboxing / Autounboxing features can be used directly with generics.
  • Wildcards works (? extends and ? super)with generics which made the hierarchy typing as easy.

Explanation

Let's see the uses with an example for each

Strong Types and Casting

Let's define a List.
//Normal 
List oList = new ArrayList();

//Generic type
List<String> gList = new ArrayList<String>();
Here
  • aList - can store any type of element in list object and developer has to be careful enough while reading and writing into it. 
  • gList - can store only String types and compiler throws error while writing elements into it other than Strings.

Reading:
aList.add("name"); //Allowed
aList.add(1); //Allowed

gList.add("name"); //Allowed
gList.add(1) //Now allowed
Writing:
//aList operations
String str = (String)aList.get(0); //Requires explicit casting
Integer aInt = (Integer)aList.get(1); //Requires explicit casting

Integer incorrect = (Integer)aList.get(0); //No compilation error but throws runtime exception.

//gList operations
String a = gList.get(0) //No explicit casting.

Integer b = gList.get(1) //Not allowed. Compiler throws error. but still you can explicitly convert it to Integer.
Iterating:
//Using iterator
for (Iterator iter = gList.iterator(); iter.hasNext();) {
  String s = iter.next();
  System.out.print(s);
}

//Using foreach
for (String s: gList) {
   System.out.print(s);
}

Autoboxing / Autounboxing

We can define generics for any type expect for built-in types but autoboxing and unboxing works with generics like below without any issues.
//Autoboxing
Set set = new HashSet();
set.add(1);
set.add(3);

//Autounboxing
for(int s : set)
{
  System.out.println(s);
}

Wildcards

Even if we define a class with generic types, sometimes we may need to use them based on the hierarchy. It can be either a subclass declaration or super class declaration. The keywords for the wildcards used are
  • ? extends T - Any class which extends T
  • ? super T - Any class which is super to T
void eat(List<? extends Fruit> fruits); //This method accepts parameter any list of objects which is child of Fruit
void eat(List<? super Fruit> fruits); //This method accepts parameter any list of objects which is parent of Fruit 
Happy Learning!!!!

Sunday, February 23, 2014

Instrumentation using AspectJ

Many of us start writing the code without worrying about how performant it will be? Once the program started working, we will be making changes like adding logs, timing statements to check the performance.
I too used to do the same before i came across AspectJ (Even though it was there since 2003, i came across it very recently).
AspectJ is available as
  1. Eclipse plugin
  2. Standalone compiler 
Let me show you an example on how to check the execution time of a method in a Test program.

Aspect

It's pretty simple. It is more or less a Java Class with special keywords. The following is the aspect which will be called before and after a method call
mport org.aspectj.lang.Signature;

public aspect MethodExeuctionTime {
    pointcut traceMethods() : (((execution(* com.instrument.test.CommonProcessor.test(..)))
            || (execution(* com.test.InstrumentProcessor.process1(..)))
            || (execution(* com.instrument.test.CommonProcessor.test(int)))
            || (execution(* com.test.InstrumentProcessor.process3(..))))
         && !cflow(within(MethodExeuctionTime)));

    before(): traceMethods(){
        Signature sig = thisJoinPointStaticPart.getSignature();
        String name = Thread.currentThread().getName();
        TimingUtils.addStart(sig.getName(), sig.getDeclaringTypeName(), System.currentTimeMillis(),name );
    }

    after(): traceMethods(){
        Signature sig = thisJoinPointStaticPart.getSignature();
        String name = Thread.currentThread().getName();
        TimingUtils.addEnd(sig.getName(), sig.getDeclaringTypeName(), System.currentTimeMillis(), name);
    }
}

Explanation:

  • pointcut is a rule where the aspect to be implemented. Syntax is pointcut <method-name> : {conditions to execute}
  • Conditions - Here in the example says during the execution of methods provided.
  • Conditions can be specified using wild chars like (*).  Logical expressions (|| or &&).
  • Finally defined the actual method. Here we see that there are two definitions of method traceMethods (In fact they are not definitions). Which says what code to execute before the method call and after method call respectively.
TimingRecord.java and TimingUtils.java are as follows
public class TimingRecord {
    private long startTime;
    private long endTime;
    private String methodName;
    private String packageName;
    private String threadName;

    public TimingRecord(long startTime, String methodName, String packageName, String threadName) {
        this.startTime = startTime;
        this.methodName = methodName;
        this.packageName = packageName;
        this.threadName = threadName;
    }
    //getters and setters
}
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class TimingUtils {

    private static Map map = new HashMap();

    public static void addStart(String methodName, String type, long time, String threadName){
        String key = threadName+type+methodName;
        TimingRecord record = new TimingRecord(time, methodName, type, threadName);
        Stack stack = (Stack) map.get(key);
        if(stack == null)
            stack = new Stack();
        stack.push(record);
        map.put(key, stack);
    }

    public static void addEnd(String methodName,String type, long time, String threadName){
        String key = threadName+type+methodName;
        Stack stack = (Stack) map.get(key);
        if(stack == null || stack.size() == 0)
            return;

        TimingRecord e = (TimingRecord) stack.pop();
        if(e != null)
        {
            long millis = time - e.getStartTime();
            System.out.println("Time Taken to Execute : Type : "+type+" Thread: "+threadName+" Method : "+methodName + " Time : "+ millis +" ms.");
        }
        map.put(key, stack);
    }
}

How to Compile

Now to compile this, set ajc in your PATH and aspectjrt.jar in CLASSPATH and run the following command
  • ajc * -d ../classes -outxml -outjar aspectj.jar
This creates a jar named aspectj.jar with an xml file ajc-aop.xml inside it which tells whats the aspect name to be injected.

How to Run

  • Add the following lines to the command line of your program execution.
    • -javaagent:<path-to-aspectjlib>/aspectjweaver.jar
    • -cp <class-path>:<path-to-project>/aspectj.jar

Sample Program

Look at the following example program
package com.test;

import com.instrument.test.CommonProcessor;

public class InstrumentTest extends Thread
{
    public static void main(String[] args)
    {
        InstrumentTest i1 = new InstrumentTest();
        i1.setName("I1");
        InstrumentTest i2 = new InstrumentTest();
        i2.setName("I2");
        InstrumentTest i3 = new InstrumentTest();
        i3.setName("I3");
        i1.start();
        i2.start();
        i3.start();
    }

    public void run()
    {
        try {
            System.out.println("Started Processing : "+getName());
            InstrumentProcessor p1 = new InstrumentProcessor();
            p1.process1();
            p1.process2();
            p1.process3();
            System.out.println("Completed Processing : "+getName());
        } catch(Exception e) {
            
        }
    }
}
package com.test;

public class InstrumentProcessor
{
    public void process1() throws Exception {
        Thread.sleep(3000);
    }
    
    public void process2() throws Exception {
        Thread.sleep(4000);
    }
    
    public void process3() throws Exception {
        Thread.sleep(5000);
    }
}

Run the program 

Without instrumentation 

  • The log will be similar to
Started Processing : I1
Started Processing : I3
Started Processing : I2
Completed Processing : I2
Completed Processing : I1
Completed Processing : I3

With Instrumentation 

  • Add the parameters mentioned above (-javaagent and aspectjrt.jar,aspectj.jar to class path). 
  • The log will be
Started Processing : I1
Started Processing : I3
Started Processing : I2
Time Taken to Execute : Type : com.test.InstrumentProcessor Thread: I3 Method : process1 Time : 3006 ms.
Time Taken to Execute : Type : com.test.InstrumentProcessor Thread: I2 Method : process1 Time : 3006 ms.
Time Taken to Execute : Type : com.test.InstrumentProcessor Thread: I1 Method : process1 Time : 3006 ms.
Time Taken to Execute : Type : com.test.InstrumentProcessor Thread: I1 Method : process3 Time : 5001 ms.
Time Taken to Execute : Type : com.test.InstrumentProcessor Thread: I2 Method : process3 Time : 5001 ms.
Completed Processing : I2
Time Taken to Execute : Type : com.test.InstrumentProcessor Thread: I3 Method : process3 Time : 5003 ms.
Completed Processing : I1
Completed Processing : I3

There are so many other cases you can instrument using AspectJ. This is just one of them.

Happy Learning!!!!!