Monday, December 31, 2012

Marker Interface in java

Marker Interface in java :

Interfcaes with no methods are known as Marker interface.

Java Marker Interface Examples :
    java.lang.Cloneable
    java.io.Serializable
    java.util.EventListener
   
Suppose you need to persist(save) the object then need to implement the
java.io.Serializable interface otherwise compiler will throw exception.
We can take another example we need to clone any object that class neither
implemented java.lang.Cloneable interface nor that superclass implemented .
Otherwise while calling clone() method will throw error.

Thursday, December 13, 2012

For Spliting a String based on given length

// For Spliting a String based on given length and return as a String array
public static String[] splitByLength(String s, int chunkSize) {
    int arraySize = (int) Math.ceil(s.length() / chunkSize);
    if (s.length() % chunkSize != 0) {
        arraySize += 1;
    }
    String[] returnArray = new String[arraySize];
    for (int i = 0; i < returnArray.length; i++) {
        int beginindex = i * chunkSize;
        int endindex = beginindex + chunkSize;
        if (endindex > s.length()) {
            endindex = s.length();
        }
        returnArray[i] = s.substring(beginindex, endindex);
    }
    return returnArray;
}

Command Pattern

Command Pattern :
    It is used to encapsulate request as an object and pass to an invoker, wherein the invoker
doesnot knows how the service the request but uses the encapsulated command to perform an action.

Key Terms:
Command  : It is an interfcae with excecute method . It is core of contract.
Command Implementation :its instance creates a binding between receiver and an action.
Client : Its creates an instance of Command implementation and associates with a receiver.
Invoker : It instructs command to an action.
Receiver: It knows the actual steps to perform an action.

When to use:
1. A History of requests needed.
2. Need Callback functionality.
3. Requests need to handled at varient time or varient orders.
4. A invoker should be decupled from the object handling the invocation.

Example:

Take an Example of Light process .

1. Create Command interface have execute method

public interface Command {
    public void execute();
}

2. Create Receiver Class , it's knows actual steps to perform an action

public class Light {
    boolean on;
    public void switchOn()   {
       on = true;
       System.out.println("Light is on");
   }
   public void switchOff()  {
       on = false;
       System.out.println("Light is off");
   }
}

3. Create a Command Implemention associated with receiver

// For On
public class LightOnCommand implements Command{
    // associated receiver
    Light light;
    public LightOnCommand(Light light)  {
        this.light = light;
    }
    public void execute() {
        light.switchOn();
    }
}

// Similarly for Off
public class LightOffCommand implements Command {
    // associated receiver
    Light light;
    public LightOffCommand(Light light)   {
        this.light = light;
    }
    public void execute() {
        light.switchOff();
    }
}

4. Create Inoker class ,it's instructs the command to an action

public class RemoteControl {
    private Command cmd;
    public void setCommand(Command cmd)
    {
        this.cmd = cmd;
    }
    public void pressButton()
    {
        cmd.execute();
    }
}

5. Finally a Client class, Its creates an instance of Command implementation and associates with a receiver

public class Client {

    public static void main(String arg[])
    {
        RemoteControl rc = new RemoteControl();
        // Receiver
        Light light = new Light();

        // creates an instance of Command implementation
        Command lightOn = new LightOnCommand(light);
        Command lightOff = new LightOffCommand(light);     

        // For Switch on
        rc.setCommand(lightOn);
        rc.pressButton();

        // For Switch off
        rc.setCommand(lightOff);
        rc.pressButton();
    }
}




Friday, August 10, 2012

Oracle Qrery for get time and update time

// Select 
select to_char(COL_NAME,'MM/DD/YYYY HH:MI:SS AM') from TABLE_NAME
// Update
update TABLE_NAME set COL_NAME=to_date('2012-07-30 11:13:39','MM/DD/YYYY HH:MI:SS AM')

To get coming Start of the day in java

public Date getComingStartoftheDay()
    {
        java.util.GregorianCalendar today = (GregorianCalendar) Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        Timestamp ts = new Timestamp(today.getTime().getTime()+86400000);
        Date date = new Date(ts.getTime());
        System.out.println(date);
        return date;
    }

Tuesday, March 13, 2012

Disable annoying Java Update notification

Disable annoying Java Update notification

If you’re sick of getting prompted to update Java, or if you have some web application like Banner that requires a particular version, you can use a simple registry hack to disable notification of available updates.

  1. Open the Registry Editor by going to the Start button and typing in regedt32.
  2. Navigate through to the following key: HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Update\Policy
  3. Change the value of EnableAutoUpdateCheck to 0 and the value of EnableJavaUpdate to 0.
Java should no longer prompt you for the annoying updates.