Tuesday, April 30, 2013

SQL :: Convert Integer to String

Convert Integer to String
you can convert an integer to string using the following query:

select CAST(ID as varchar2(50)) as ID_STRING from TABLE_NAME.

Have a nice day.

Sunday, April 28, 2013

Java :: Quartz

Steps to use Quartz 

1- Download Quartz
From http://quartz-scheduler.org/downloads/catalog and after free registration.
Extract the archive file and copy quartz-2.1.7.jar (or the name of the downloaded version) to your project libraries.

2- Download slf4j
From http://www.slf4j.org/download.html copy slf4j-api-1.7.5.jar (or the name of the downloaded version) to your project libraries.

3- Create Main Class and Implement Job Interface
Use the bellow example:


public static void main(String[] args) {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched;
try {
sched = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Main.class)
.withIdentity("SimpleJob").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("SimpleJob")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

or you can download the source from:
https://github.com/firask86/QuartzExample

The example demonstrate simple and cron schedule.




Thursday, April 18, 2013

Java :: Singleton Pattern

Definition
Singleton pattern is one of the most commonly used patterns, it is used to ensure that only one instance of an object is created, and as in the implementation example you can see that a check for a STATIC variable if not exists create one (and it will be only the first time off-course) then the same instance will be returned always.
Focus on that the main constructor for the class should be implemented as private to make sure that no one can create an object for your class using else than getInstance method

Implementation

 public class SingletonClass {
   private static SingletonClass instance;

   private SingletonClass() {}

   public static SingletonClass getInstance() {
    if(instance == null) {
     instance = new SingletonClass();
     return instance;
    } else {
      return instance;
   }
 }

Sunday, March 17, 2013

Java :: HashMap Vs HashTable



 HashMap Vs HashTable

There are several differences between HashMap and Hashtable in Java:
  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  3. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
Since synchronization is not an issue for you, I'd recommend HashMap.
Reference: 
http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable

Monday, February 25, 2013

Java :: String ValueOf Vs String Concatenation

String ValueOf Vs String Concatenation

From stack over flow I found bellow answer, as conclusion:
"Never use (+ "") to convert values to string, instead use String.valueOf."




public void foo(){
int intVar = 5;
String strVar = intVar+"";    
}
This approach uses StringBuilder to create resultant String
public void foo();
  Code:
   0:   iconst_5
   1:   istore_1
   2:   new     #2; //class java/lang/StringBuilder
   5:   dup
   6:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
   9:   iload_1
   10:  invokevirtual   #4; //Method java/lang/StringBuilder.append:(I)Ljava/lan
g/StringBuilder;
   13:  ldc     #5; //String
   15:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/
String;)Ljava/lang/StringBuilder;
   18:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/la
ng/String;
   21:  astore_2
   22:  return

public void bar(){
int intVar = 5;
String strVar = String.valueOf(intVar);
}
This approach invokes simply a static method of String to get the String version of int
public void bar();
  Code:
   0:   iconst_5
   1:   istore_1
   2:   iload_1
   3:   invokestatic    #8; //Method java/lang/String.valueOf:(I)Ljava/lang/Stri
ng;
   6:   astore_2
   7:   return

Saturday, February 23, 2013

Java :: How to override equals

How to override equals

All what you need to do is add the following piece if code in your object:


@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
YourObject rhs = (YourObject ) obj;
if (!rhs.getId().equals(getId())) // For Example
return false;
return true;
}


Have a nice day.