Sunday, November 9, 2014

Cyclomatic Complexity ( CC )

One day  I was required to put application inside sonar for measuring code management percentage,and I found the term "Cyclocmatic complexity".

If you ask "why I should manage my code, the code is working fine".
I will tell you yes this is the good news but the bad news is your code now is unmanageable.
  • Benefits of Code Management:
Code management make easier to deal with code at the time of maintenance,
Code management may have improve the performance of your application.

Now lets's see:
  • What is the meaning of this term "Cyclomatic Complexity" ?
The Cyclomatic Complexity is a  measured by the number of checking for a decision to be made + 1.
Cyclocmatic complexity = Number of decision points + 1.

(&&, ||) operators and (if, while, do, for, ?:, catch, switch, case, return, throw) statements in the body of a class plus one for each constructor, method (but not getter/setter), static initializer, or instance initializer in the class and the last return statement in method, if exists, is not taken into account.

Example 1:
 

String str = "someString";

if ( str.equals( case1 ) )

do something;

if( str.equals( case2 ) )

do something;

else

do default thing;




Cyclomatic Complexity for previous code is = ( if for case1 ) + ( if for case2 ) + ( else ) +  1  =  4

Example 2:
 

if( name.equals(name1) || name.equals( name2 ) || name.equals( name3) && age != 23 )

{ do something }




Cyclocmatic complexity = if  +  ||  +  ||  +  &&  + 1 = 5
  • Avoiding Cyclomatic Complexity:
In that factory design there may be either switch case or several if else conditions.

Example:
at package model we write an interface , two classes implements it and a factory class.
 

public interface Handler

{

 public void handle();

}



public class AHandler implements Handler

{

 public void handle()

 {

  System.out.println("A handler");

 }

}



 

public class BHandler implements Handler

{

 public void handle()

 {

  System.out.println("B handler");

 } 

}



 

public class AbstractHandler

{

public static Handler getHandler( String handlerName )

 {

  Handler handler = null;

  try

  {

   if( handlerName.equals("A"))

    handler = new AHandler();

   if( handlerName.equals("B") )

    handler = new BHandler();

  }

  catch( Exception e )

  {

   System.out.println("There is no specific handler");

  }

  return handler;

 }



}



 

public class TestDynamicHandler

{

 public static void main(String[] args)

 {

  Handler handler = AbstractHandler.getHandler("B");

  handler.handle();

 }



}


In the above examples, there is nothing wrong in code,
but for every new case, you have to write a new class and you have to add one or more if clause in the class “AbstractHandler”.

Improvement is to modify class “AbstractHandler” in the following manner:

 

public class AbstractHandler

{

public static Handler getHandler( String handlerName )

 {

  Handler handler = null;

  try

  {

   handler = (Handler) Class.forName(

     "model." + handlerName + "Handler")

     .newInstance();

  }

  catch( Exception e )

  {

   System.out.println("There is no specific handler");

  }

  return handler;

 }

}


Now no need to update the class "AbstractHandler" if you make a new class and
Cyclomatic Complexity=0.

Deploy image as resource for other applications

Sometimes you have classes,images or any other files that is used in most of your applications(apps.),
copied and pasted in each new application.
Solution for this is to deploy these files to be used between apps as a shared resource.
This is an example of how to deploy folder of images and use it in other apps.:

1- Locate the META-INF folder YOUR_PROJECT_NAME/.adf/META-INF
and put the images folder there. 

2- Create new ADF Library Jar File.



3- Enter name for the  Deployment Profile.



4- Now we need to get a jar file then deploy it to the server, so choose deploy for your Deployment profile.

5- A new jar has been created and ready to be deployed,
Take the path for that jar.


6- Run the integrated server of jdeveloper, At deployments make new and write the path of the jar to deploy it.


Now you can use this jar files in other applications, but you need first to add a reference for your shared jar at web.xml file.



7- As a test I created a page and added image inside, in the source property write the path:
adf/image/YOUR_IMAGE_NAME

Run the application, the image will appear as following:


Wednesday, November 5, 2014

Mutiple time method executing based on timer

Some times you want to execute a method for each specified period of time, so you

1- Make a class that implements TimerListener interface and implements timerExpired method.
 

Class JobTime implements  TimerListener
 public void timerExpired(Timer timer) {
}          

2- Make a weblogic time manager that executes timerExpired method for every 30 seconds
 

Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);      
InitialContext ic = new InitialContext();
TimerManager tm = (TimerManager)ic.lookup("java:comp/env/tm/TimerManager");
tm.scheduleAtFixedRate(new JobTime(), cal.getTime(), 30 * 1000);

The control returns back to the user and request goes into the queue for processing.
This allows to continue normal user actions in ADF session and run long running jobs in the background, otherwise ADF applications session would be blocked.

To cancel the request make "JobTime" class implements interfae called CancelTimerListener
and overrides method:

    public void timerCancel(Timer timer) {
    }

and within code of timerExpired Method write

public void timerExpired(Timer timer) {
//Based on your condition cancel the timer
timer.cancel();
}

Deploy ADF Application














java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...