Daily Archives: March 26, 2010

Some interesting hashcode values


Some of the hashcodes of interest’s are in java wrappers

BooleanhashCode()1231 for ‘true’ and ‘1237’ for ‘false’
Integer hashCode() – will return the primitive int value
Byte hashCode() – primitive int value of the byte is returned as hashcode
CharacterhashCode() – primitive int value of the byte is returned as hashcode
Long hashCode() – value of the following expression

(int)(this.longValue()^(this.longValue()>>>32))

to know more about this please check hashcode-contract-with-equals

Advertisements

Magic.. Magic..


Ever wonder how files are identified correctly as jpeg, gif or even class file for that matter by the OS.

Its all magic.. 🙂

Magic numbers are those which are prefixed at the start of the file in ASCII code to identify the file format.

Some of the common magic numbers are

Class byte code starts with hex - CAFEBABE
GIF image files have 'GIF89a' (47 49 46 38 39 61) or 'GIF87a' (47 49 46 38 37 61)
JPEG image files begin with FF D8 and end with FF D9
PNG image files begin with "\211 P N G \r \n 32 \n" (89 50 4E 47 0D 0A 1A 0A)
ZIP files begin with 'PK' (50 4B)
PDF files start with '%PDF' (25 50 44 46)

Singleton pattern revisited


Singleton pattern

Definition: At any point of time we ensure that there is only object created for the class

Example:
participant classes:

  • Singleton.java
  • SingletonClient.java(client)

Diagram:

Singleton.java

  • This class includes a static member variable singleton with private scope
  • the constructor is private so that no one can instantiate it
  • we have a getCurrentInstance() method to instantiate the class. If the instance is already available then the instance is returned to the caller otherwise it is newly created
public class Singleton {

 private static Singleton singleton = null;

 private Singleton(){

 }

 public static synchronized Singleton getCurrentInstance(){
 if(singleton == null){
 return new Singleton();
 }
 else{
 return singleton;
 }
 }

 public void sayHello(){
 System.out.println("Hello");
 }
}

SingletonClient.java

use getCurrentInstance() method to get the instance of Singleton class and use it.

 public class SingletonClient {

 public static void main(String[] args) {

 Singleton singleton =  Singleton.getCurrentInstance();
 singleton.sayHello();
 }
}
 

contract:

cannot instatiate more than once.
getCurrentInstance() return only one instance at any point of time

Implementing Prototype pattern


Prototype pattern

Definition: the pattern is used to create a clone avoiding another creation. We clone the object if the cost of creating the object is expensive than cloning it.

Example:
participant classes:

  • Vinod.java
  • Rinku.java
  • Prototype.java
  • PrototpyeClient.java (client)

Diagram:


Prototype.java

This is an interface class defining signature for doClone() method. The return type is of Prototype type. This throws CloneNotSupportedException.

 public interface Prototype {
 public Prototype doClone() throws CloneNotSupportedException;
}

Vinod.java & Rinku.java

These are simple classes describing Vinod’s and Rinku’s characteristics. They have their attributes and their respective accessors defined in the class

These two classes implement the Prototype interface
doClone() method is implemented returning a new clone of the respective class

//Vinod.java
 public class Vinod implements Prototype,Cloneable{
 String name;
 String age;
 String sex;

 public String getName() {
 return "Vinod";
 }
 public String getAge() {
 return "25";
 }
 public String getSex() {
 return "Male";
 }
 @Override
 public Prototype doClone() throws CloneNotSupportedException {
 return (Prototype) this.clone();

 }

}

//Rinku.java

public class Rinku implements Prototype,Cloneable{
 String name;
 String age;
 String sex;

 public String getName() {
 return "Rinku";
 }
 public String getAge() {
 return "22";
 }
 public String getSex() {
 return "Female";
 }
 @Override
 public Prototype doClone() throws CloneNotSupportedException {
 return (Prototype) this.clone();

 }
}

PrototypeClient.java

Instantiating the objects and cloning the same

public class PrototypeClient {

 public static void main(String[] args) throws CloneNotSupportedException {

 Vinod vinod = new Vinod();
 Vinod vinod2 = (Vinod)vinod.doClone();

 System.out.println(vinod.hashCode() != vinod2.hashCode());
 System.out.println(vinod.getClass() == vinod2.getClass());

 }
}

contract:

x.clone() != x
x.clone().getClass() == x.getClass()

How to iterate the showDetailItem inside the panelTabbed component using af:forEach


Here is the code

       private List items;
       private ArrayList list = new ArrayList();

        public Bean(){
            list.add(new SelectItem("1"));
            list.add(new SelectItem("2"));
        }

         public List getItems() {
             return list;
        }
    
<panelTabbed id="pt1">
 <forEach var="row" items="#{Bean.items}">
 <showDetailItem text="showDetailItem #{row.label}" id="sdi1" />
 </af:forEach>
 </af:panelTabbed>
    

and it renders like