Wednesday, March 24, 2010

Email validation in Java

You can write your own code to validate email, but why bother if you have it ready. The Apache Commons project has a sub project called: Commons Validator. This project contains all sort of validation methods. One of the validations is used for email. In order to check if email address is valid all you have to do is:
EmailValidator.getInstance().isValid("some.mail.address@gmail.com");

Make sure to add the Commons Validator jar file and it's dependencies to your project.

Sunday, March 21, 2010

Java interview question: Check array for duplicate values in one pass

This question should be asked more precisely: How can array of size N filled with numbers up to N (0 to N – 1) be scanned for duplicate values (or more) with one scan (O(n))?

The answer lies in the body of the question: The fact that the array is filled with numbers up to N only, implies that a helper array of size N can be used to store the number of times each number appears in the first array. The value of the number in the first array is used as an index in the helper array. For example, if the first value in the array is: 5, then the 5th element in the helper array is incremented by 1. Of course, before incrementing the value in the helper array, we check if it is larger than 0. If it is larger than 0, the value of the first array was already counted once, and this current value is a duplicate.

The code of such method is very simple, lets have a look:

public class ArrayDuplicates {
  public static boolean isDuplicate(int[] arr)
  {
    int size = arr.length;
    int[] helperArr = new int[size];
    for (int i = 0; i < size; i++)
    {
      if (helperArr[arr[i]]++ > 0)
      {
        return true;
      }
    }
    return false;
  }
  public static void main(String[] args)
  {
    System.out.println(isDuplicate(new int[] { 2, 1, 0 }));
    System.out.println(isDuplicate(new int[] { 2, 0, 2 }));
  }
}


Note, that incrementing the index of the helper array (according to the value from the first array) and checking if we have already counted this number is all done the “if” sentence.

Friday, March 19, 2010

Java interview question: List “contains” method

This question verifies that you know how the “contains” method of List works.
Let’s have a look at this small piece of code:
Person person = new Person("guy");
List<Person> persons = new ArrayList<Person>();
persons.add(person);
System.out.println(persons.contains(person));

The question: Is it possible, that the output of this little piece of code returns: “false”.

At first look, the answer seems to be: NO, meaning, this code will always return “true”. So how can a “Person” object that was just created and added to a list, not be contained in it?

The answer, is in the way the “contains” method works. The “contains” method, is using the “equals” method in order to check if an object is contained in the list. Therefore, the code above can return “false” if the writer of the “Person” object overridden the equals method.

Java interview question: Count the number of “1” bits in a byte

Frankly, I think it is quite an idiotic question. Not because it is too easy or too hard question, but because I think this question doesn’t really imply on the practical programming capabilities of the person being interviewed. Anyway, a friend of mine was asked to solve this question, so I though it might be an interest for more developers looking for a job and having to handle all sort of weird questions.
In general, there are 2 solutions for this question:
  • First solution is iterating 8 times (a byte contains 8 bits). Each time the right most bit is extracted from the byte. and then the byte is shifted right one bit.
  • Second solution, is a little bit less straight forward, but more efficient: Divide the number by 2, if there is a modulo, count it. Repeat this operation as long as the number we are dividing is greater than zero.
Here is how the first solution looks in Java code:
public class GetBits {
public static int countBits(byte num)
{
  int count = 0;
  for (int i = 0; i < 8; i++)
{
    if ((num & 1) == 1) // check if right most bit is 1
{
count++;
}
num = (byte)(num >>> 1); // shit right 1 bit, including the sign bit
}
  return count;    
}


Here is how the second solution looks like:

public static int countBits(byte num)
{
  int count = 0;
  while (num > 0)
{
    if (num % 2 == 1) // check if number have modulo
{
count++;
}
num /= 2; // divide the number by 2
}
  return count;    
}

Note, that the first solution is a bit less efficient, because it always iterates 8 times. But, it handles both positive and negative values. The second solution will simply won’t work with negative numbers.

Monday, March 15, 2010

Excel Pivot Tables and Dynamic Range

Excel Pivot Table is a great tool for viewing and aggregating data. The data shown in a Pivot Table is taken from some range in one of the sheets of the excel file. The problem with Pivot Tables, is when you add new data. After refreshing the Pivot Table the new data is not shown. To solve this issue we can use a dynamic range. A dynamic range knows to grow automatically according to the rows we add. Suppose we have an excel file that contains 2 sheets: “data” and “pivot table”. We would like to create from the raw data in the sheet: “data” a dynamic range. This will allow us to add more rows to the “data” sheet, and just refresh the pivot table to see our new rows.
This is how our “data” sheet looks like:
1_pivot_data

And this is how our “pivot_table” sheet looks like:


2_pivot_table

In order to create a dynamic range from our raw data, we should go to the “Formulas” tab on excel and choose the “Define Name” option. A pop up window will be opened. In the window we should name the range. For example: “pivot_data_source”. In the “Refers to” field we define the dynamic range. The value that should be put there look like:
=OFFSET(data!$A$1,0,0, COUNTA(data!$A:$A), COUNTA(data!$1:$1))

Where the “data” is the name of the sheet in which the dynamic range is located.
The popup window looks like this:

image

After defining the dynamic data source, we should connect the pivot table with it. We do it by standing on the pivot table and selecting the “Options” tab from the main menu and choosing the “Change Data Source” option. A popup window is opened. In the field: “Table/Range” we should put the name of the dynamic range we defined: “pivot_data_source”.
The popup windows looks like:

5_pivot_data_source_change

Finally we would like the pivot table to be automatically refreshed every time our excel file is being opened. We do it by standing on the pivot table, then selecting “PivotTable Options…”. A popup window will be opened. On the “Data” tab we should check the option: “Refresh data when opening the file”. The popup window looks like:

7_pivot_table_refresh_on_load

Note that if you add new rows to the raw data, and you immediately want to see the changes in the pivot table, you should right click on the pivot table and choose the option “Refresh”. Press here to see an example of an excel file with pivot table and dynamic range defined.

Wednesday, March 3, 2010

Seam and Quartz integration

Seam is a great framework for easily building Internet applications in Java. It integrates with lots of well known existing java technologies and serves as a complement to JSF (Java Server Faces).
Quartz is a job scheduling framework allowing to easily add the capability of running scheduled jobs in your applications.
Quartz can be easily integrated with Seam. By integrating both technologies you will be able to enjoy Seam approach to managing beans on your web application.
Let’s take a look at a simple example:
First make sure you have all the jars needed to run Quartz and Seam.
Then go to your Seamcomponents.xml” file and add these lines:
<async:quartz-dispatcher/>
  <event type="org.jboss.seam.postInitialization">
  <action execute="#{quartzController.scheduleTimer}"/>
</event>
Now, put in your sources root directory (usually the “src”) the file: “seam.quartz.properties” file. A typical file may look like this:
org.quartz.scheduler.instanceName = Sched1
org.quartz.scheduler.instanceId = 1
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
Now, to the Java code. The code is constructed from 2 Seam beans. The first bean contains the Quartz trigger class. I looks like:
package com.bashan.blog.job;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.async.QuartzTriggerHandle;
import java.io.Serializable;
import java.util.Date;
/**
* @author Bashan
*/
@Name("quartzController")
@AutoCreate
public class QuartzController implements Serializable {
@In
ScheduleProcessor processor;
private QuartzTriggerHandle quartzTriggerHandleDoJob;
public void scheduleTimer() {
quartzTriggerHandleDoJob = processor.doJob(new Date(), "0 0/1 * * * ?");
}
}
Note, that when calling to “doJob” method, a cron expression is passed. This expression describes the frequency in which the job will run (event minute in the example).

The second class contains the actual task we would like to do:
package com.bashan.blog.job;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.async.Asynchronous;
import org.jboss.seam.annotations.async.Expiration;
import org.jboss.seam.annotations.async.IntervalCron;
import org.jboss.seam.async.QuartzTriggerHandle;
import org.jboss.seam.log.Log;
import java.util.Date;
/**
* @author Bashan
*/
@Name("processor")
@AutoCreate
@Scope(ScopeType.APPLICATION)
public class ScheduleProcessor {
 
  @Logger
  static Log log;
 
  @Asynchronous
  public QuartzTriggerHandle doJob(@Expiration Date when, @IntervalCron String interval) {
    System.out.println("I am a Quartz job.");
    log.debug("Pass some information to the log");
    return null;
  }
}
The method “doJob” is a Seam asynchronous method. This method will be executed according to the data of the cron expression given in the class: QuartzController.