Monday, September 14, 2009

equals(), hashCode() and toString() methods in Object


//hashCode in Object class

public native int hashCode();

//Java doc for hashCode()

int java.lang.Object.hashCode()


Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

* Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
* If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
* It is not required that if two objects are unequal according to the java.lang.Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

//equals() method in Object class
public boolean equals(Object obj) {
return (this == obj);
}


package object;

public class User {

private String firstName;
private String lastName;
private Integer mark;
public User(String firstName,String lastName, Integer mark){
setFirstName(firstName);
setLastName(lastName);
setMark(mark);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getMark() {
return mark;
}
public Integer setMark(Integer mark) {
this.mark = mark;
}
}


package object;

public class ObjectTest {

public static void main(String[] args) {
User user1=new User("Vijayan", "Srinivasan", new Integer(100));
User user2=new User("Vijayan", "Srinivasan", new Integer(100));
System.out.println("[user1==user2]="+(user1==user2));
System.out.println("[user1.equals(user2)]="+user1.equals(user2));
}
}


So both case it is giving false becasue, both of the statements uses the same logic to compare 2 objects. For getting this right we have to do the following.

//Override equals() method in User class

public boolean equals(Object object) {
if(object instanceof User){
User user=(User) object;
return
equals(firstName,user.firstName) &&
equals(lastName,user.lastName) &&
equals(mark,user.mark);
}
return false;
}

// Implementation of toString() in Object class
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}


Although it is sufficient that overriding equals methods alone sufficient here it is adviced to override also for better performance. //Why?


// implementation of put() method in Hashtable class

public Object put(Object key, Object value) {
// Make sure the value is not null
if (value == null) throw new NullPointerException();

// Makes sure the key is not already in the hashtable.
HashtableEntry e;
HashtableEntry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;

for (e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) { //<------ Look here
Object old = e.value;
e.value = value;
return old;
}
}


Please note if we have proper hashCode() impelentation there is no need of calling the equals() method at all. Hence if the object is different we can let the Hashtable know ahead of calling equals() method. That is the reason it is always important to implement hashCode() when ever we override the equals()

Wednesday, September 09, 2009

Seting gnome on Linux VNC server

In redhat linux, when we start VNC server, by default it used to start with a simple termial based view. Inorder to start it as gnome based session the following command can be used

  1. Set the vnc session as gnome
    > vi ~/.vnc/xstartup (remove all the lines and add only the following lines)
    unset SESSION_MANAGER
    vncconfig -iconic &
    gnome-session &
  2. Start VNC server
  3. > vncserver - geometry 1024x768
Use the correct ip address and X-display id to connect to the VNC e.g. 10.66.92.127:1

Monday, September 07, 2009

Visio type tool on SAAS

Most of would have used Microsoft Visio for creating various diagrams such as Class, Network, Flowchart etc., Here is a tool that does everything online ( http://creately.com/ ). This tool has developed in Flex also allows us to save and collaborate among team members.

Thursday, August 20, 2009

Jboss JDBC Driver Loading Issue

If we configure data source throug *-ds.xml (e.g. mysql-ds.xml) file in Jboss and the corresponding JDBC driver jar is only available in a WEB-INF/lib folder of an application, even though the same application tries to get a connection through configured data source, first time it will throw an ClassNotFoundException. As the class would not have loaded until the first call.

But if we try to get connection second time it will work with out any exception. This problem can be rectifed by keeping the copy of the JDBC driver jar in /server//lib folder (e.g. c:\jboss 4.0.2 \server\default\lib). This will ensure that the driver jar will be loaded in to ClassLoader at the time of server starting.

Monday, August 17, 2009

Incorrect arguments to mysql_stmt_execute

When we use MySQL connector mysql-connector-java-5.0.4-bin.jar with PreparedStatement contains more number of columns we may get the following Exception
java.sql.SQLException: Incorrect arguments to mysql_stmt_execute
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)

To fix this issue we have to use mysql-connector-java-5.1.8-bin.jar

Tuesday, June 30, 2009

Observer and Observable Example

In Java we have Observer interface and Observable class, Below program gives a sample use of Observer and Observable

import java.util.Observable;

public class Employee extends Observable {
float salary;

public void setSalary(float newSalary) {
salary = newSalary; // salary has changed
setChanged(); // mark that this object has changed, MANDATORY
notifyObservers(new Float(salary)); // notify all observers, MANDATORY
}
}

import java.util.Observable;
import java.util.Observer;

public class Manager implements Observer {

public void update(Observable obj, Object arg) {
System.out.println("A change has happened and new salary is " + arg);
}
}


public class Demo {
public static void main(String[] args) {
Employee e = new Employee();
Manager m = new Manager();
e.addObserver(m); // register for observing
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if (i == 5) {
e.setSalary(100);
}
}
}
}

அழகாக தெரிகிறது தமிழ்...

உனக்கு வாழ்த்து
எழுதும்போது மட்டும்
அத்தனை அழகாக
தெரிகிறது தமிழ்...

உன் பெயரை
உச்சரிக்கும்போது மட்டும்
சுகமாக ஒலிக்கிறது
என் குரல்...

என்னோடு நீ
இருக்கும்போது மட்டும்
சொர்க்கமாக தெரிகிறது
இந்த உலகம்...

என்னோடு நீ
நடக்கும்போது மட்டும்
சிறியதாக குறைகிறது
சாலையின் நீளம்...

மொத்தத்தில் உனக்காக
இந்த வாழ்த்து மடல்
எழுதுவதில் என் மனம்
சந்தோசத்தில் நிறைகிறது...

இனிய பிறந்தநாள்
வாழ்த்துக்கள்!!!

விஜயன் சீனிவாசன்

Monday, June 29, 2009

Reading CSV Files Using csvjava 2.0

Recently I have gone through a new CSV Reader called csvjava 2.0 It is a very simple tool which has only 2 classes CSVReader and CSVWritter but yet very powerful. Below I have wrote one sample program using CSVReader class of csvjava 2.0.

This CSV Reader supports all escapping that has been supported by CSV file format.

e.g.
Hi Vijayan This is a Test,"""Simple Test with , and """,A,B,CV
Will be treated as follows
Column 1: Hi Vijayan This is a Test
Column 2: "Simple Test with , and ""
Column 3: A
Column 4: B
Column 5: CV

import java.io.FileNotFoundException;
import java.io.IOException;

import com.csvreader.CsvReader;


public class CSVReaderSample {

public static void main(String[] args) {
try {
CsvReader reader=new CsvReader("Vijayan.csv");

while(reader.readRecord()){
System.out.print(reader.getCurrentRecord()+")");
System.out.print(reader.get(0));
System.out.print("\t");
System.out.print(reader.get(1));
System.out.print("\t");
System.out.print(reader.get(2));
System.out.print("\t");
System.out.print(reader.get(3));
System.out.print("\t");
System.out.println(reader.get(4));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Friday, June 26, 2009

Java FTP Sample Code

Writing FTP Code using ANT is very simple compare to we write the same using FTPClient class. This post will explain you how to do basic FTP operation through ANT FTP task classes.

import java.io.File;

import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.optional.net.FTP;
import org.apache.tools.ant.types.FileSet;

public class FtpUsingAnt {

public static void main(String[] args) {
ftpGetSample();
ftpDeleteTaskSample();
}


public static void ftpGetSample(){
FTP ftp=new FTP();
FTP.Action action=new FTP.Action();
action.setValue("get");
ftp.setAction(action);
ftp.setServer("server.com");
ftp.setUserid("user-name");
ftp.setPassword("password");
ftp.setRemotedir("/remote-folder");
FileSet fileSet=new FileSet();
File dir=new File("c:\\local-folder");
fileSet.setDir(dir);
fileSet.setIncludes("**/*.csv");
ftp.addFileset(fileSet);
callAntTask(ftp, "ftp");
}


public static void ftpDeleteTaskSample() {
FTP ftp=new FTP();
FTP.Action action=new FTP.Action();
action.setValue("del");
ftp.setAction(action);
ftp.setServer("server.com");
ftp.setUserid("user-name");
ftp.setPassword("password");
ftp.setRemotedir("/remote-folder");
FileSet fileSet=new FileSet();
File dir=new File("."); // Not Needed
fileSet.setDir(dir);
fileSet.setIncludes("**/*.csv");
ftp.addFileset(fileSet);
callAntTask(ftp, "ftp");
}


public static void callAntTask(Task task, String taskName){
DefaultLogger logger=new DefaultLogger();
logger.setMessageOutputLevel(Project.MSG_DEBUG);
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);

Project project=new Project();
project.addBuildListener(logger);
task.setProject(project);
task.setTaskName(taskName);
task.execute();
}

}

Monday, June 22, 2009

How Class.forName() registers driver?

Most of the time for registering driver to DriverManager, we use the following code.

Class.forName("com.mysql.jdbc.Driver");

But Class.forName() method will not do any registration of Driver with DriverManager. If that is the case how DriverManager.registerDriver() (registration) is called?

static {

try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (java.sql.SQLException E) {
throw new RuntimeException("Can't register driver!");
}

}

This is snippet is taken from com.mysql.jdbc.Driver class's source code. So if you closely watch this code, when ever
 Class.forName("com.mysql.jdbc.Driver");
is invoked as soon as the class is loaded in to the memory, all static blocks of that classes are also being invoked. Insite one of the static block all driver classes are registering them self to DriverManager.

Thursday, June 18, 2009

வட்டத்தின் இரு புள்ளிகள்

நீயும் நானும்
வட்டத்தின் இரு புள்ளிகள்...
நான் சென்னையிலும்,
நீ பெங்களுரிலும் இருந்தபோது,
விட்டத்தின் இரு முனைகளில்
ஆளுக்கு ஒன்றாய் நின்றோம்...

நான் அமெரிக்கா சென்றவுடன்
நீ விட்டத்தின் முனையிலிருந்து
வட்டதின் மையத்திற்கு
இடம்பெயர் ந்தாய்...

நானும் பெங்களூர் வந்தவுடன்
என்னையும் ஈர்த்துக்கொண்டாய்
வட்டத்தின் மையத்திற்கு...

என்றுதான் இத்தனைநாள்
நினைத்திருந்தேன், அனால்
இன்றுதான் உணர்கிறேன்
நீ இருக்கும் இடத்திலேயே
இருந்துகொண்டு வெற்றிடங்கள்
அனைத்தையும்
உன் நட்புப் புள்ளிகளாலும்,
அன்புப் புள்ளிகளாலும்,
நிறப்பி இருக்கிறாய் என்று...

இனிய பிறந்தநாள்
நல்வாழ்த்துகள்!!!

அன்புடன்,
விஜயன் சீனிவாசன்

உனக்கும் எனக்கும்

உனக்கும் எனக்கும்
எத்தனையோ நண்பர்கள்
நம் அறிமுகத்திற்கு
முன்னறும் பின்னறும் ...

இருப்பினும்
நமது நட்ப்பில் மட்டும்
வித்தியாசத்தை உணர்கிறோம்
நீயும் நானும்...

இடையில் எத்தனையோ
எற்றங்கள் இறகங்கள்...
இருப்பினும்
எதுவும் மாறவில்லை,
நம் இருவருக்கும்
இடையில்...

என்ன தவம் செய்தேனென
தெரியவில்லை எனக்கு...
எததனையோமுறை கேட்டும்
சொல்லத் தெரியவில்லை உனக்கு...

ஒன்று மட்டும்
என்மனம் திரும்ப திரும்ப
என்னிடம் சொல்கிறது,
உன் நட்ப்புமட்டுமே
என்னை
ஒவ்வொரு நொடியும்
சுவாசிக்கவைக்கிறது....


என் மனமாற்ந்த நன்றிகள்
உன் பிறந்தநாளுக்கு...

இதையெல்லாம்
உன்னிடம் சொல்ல
எனக்கு ஒரு வாய்ப்பு
தந்ததற்க்கு....

இனிய பிறந்தநாள் வாழ்த்துக்கள்!!!

நட்புடன்,
விஜயன் சீனிவாசன்

என் இரவுகள்...

என் கனவுகளின்
தொழிற்சாலை
நீ

ஒவ்வொரு நாளும்,
அலுவலகம் விட்டு
வரும் போதும்
இன்னுமொரு
தொழிற்சாலையில் தான்
அடியெடுத்துவைக்கிறேன்.

சில மணிதுளிகள்
அன்றைய நிகழ்வுகளை
வெள்ளோட்டம் விடுவேன்
என் விழித்திரையில்.

பின்னர் தலையணை
கட்டியணைத்து...
கனவுத் தொழிற்சாலைக்குள்
மெல்ல மெல்ல நடந்து செல்வேன்...

நீ அன்று அன்போடு
வழியனுப்பி வைத்திருந்தாள்...

மாறாக,
வீடு சேர்ந்த
சில நொடிக்கெல்லாம்,
தலையணை நனைத்திருப்பேன்...
என் கண்ணீர் துளிகளால்...
நீ ஏதோ ஏசியிருந்தால்...

இப்படித்தான் நகர்கின்றன
என் இரவுகள்..

உன் நினைவுகளோடு,
விஜயன் சீனிவாசன்.

திருடி வைத்திருக்கிறேன்

உனக்காக
ஒரே முறையில்
ஒரு கவிதை
எழுதியதாக
நினைவில்லை

ஒரு நாளும்
விடுபட்ட அழைப்பு
கொடுக்காமல்
என் விழிகள்
உறக்கம்
கொண்டதாக
நியாபகம் இல்லை

இதுவரையில்
விடிந்த உடன்
உன் காலை வணக்க
குறுந்தகவல் தேட மறந்ததில்லை

தினம் தினம்
இப்படி எததனையோ
என் செயல்கள்
அணிச்சை செயல்களாக
அரும்பிக் கொண்டுள்ளன...

உன் கூந்தல் ஏறி
உதிர்ந்த பூக்கள்
எதையும்
எடுத்து வைத்து
இருக்கவில்லை நான் ...

கடைசியாக
நீ என்னை
சந்தித்த போது
கைது செய்யவில்லை
காற்றிலே
கலந்திருந்த
உன் முகப்பூச்சு
வாசணையை

ஆனாலும்
ஒன்றை மட்டும்
திருடி வைத்திருக்கிறேன்...
அது உன் இதயம்...

எங்கே வைத்திருக்கிறேன்?
என்று மட்டும்
கேட்காதே?
ஏனென்றால்
வைத்திருந்த இடத்தை
தேடதான் திருடி
இருக்கிறேன் உன்
இதயத்தை!!!

உன்னிடம் சொல்ல சொன்னார்...

இறைவா இன்னுமா
இரக்கம் வரவில்லை
உன்னக்கு?

என்னவளை இன்னும்
எத்தனை தோல்வி படிகளை
ஏற சொல்லுவாய்?
என்றேன் இறைவனிடம்...

கவலை கொல்லாதே
கண்மணியே...
நிச்சயம் ஒருநாள்
தோல்வி படிகள்
முடிந்திருக்கும்...

அன்று
நீ நிற்கும் உயரம்
அனைவரையும்
அயரவைக்கும்...
என்று சொல்ல
சொன்னார் உன்னிடம்...

உனக்காகவே,
விஜயன் சீனிவாசன்

இதோ பிறக்கப் போகிறது

இதோ
பிறக்கப் போகிறது
ஒரு பூவின்
பிறந்தநாள்
இன்னும்
சில மணிநேரத்தில்...

வாழ்த்து மடல்
தயாரிக்க
ஏதேதோ எழுதி
வாசித்துப் பார்த்தேன்...
எதுவும் ஒலிக்கவில்லை
உன் பெயரை
விட சிறப்பாக ...

இருந்தும்
இதோ
ஒரு சில வரிகள்
உனக்காகவே ...

இதுவரை
இல்லாத வகையில்
இனிதே துவங்கட்டும்
இந்த பிறந்தநாள் ...

எட்டாத
உயரத்தை
இனி எட்டட்டும்
உன் முயற்சிகள்யாவும் ...

என்ற வாழ்த்துகளுடன்
நிறைவு செய்ய
நினைத்தேன் ...

ரோஜாவை மட்டும்
வாழ்த்திவிட்டு ...
தோட்டக் காரர்களை
மறந்த உணர்வோடு ...
மீண்டும் தொடர்கிறேன் ...

உன் பெற்றோருக்கும்
வாழ்த்து சொல்ல ...

வாழ்த்த வயதில்லை
என்றாலும்,
நன்றிகலந்த வணக்கத்தை
தெரிவிக்க விரும்புகிறேன்..

உன்னை தோழியாக
தந்ததனால்…

இனிய பிறந்தநாள் வாழ்த்துகள் !!!
விஜயன் சீனிவாசன்

முதல் பிறந்தநாள் வாழ்த்து

ஏதோ ஒரு மாலை வேளை
Yahoo! Chatல் உன்னை
பதிவு செய்ததாக ஞாபகம்...

Hi! என துவங்கியது
நமது உரையாடல்...

சில நிமிடங்களில் இருந்த
உரையாடல் நீளம்
இப்போது
சில மணி நேரமாக
நீண்டிருகிறது...

சிலரது நட்பை
பெற நாட்கள்
பல தேவைப்படும்...

சிலரது நட்பு
தானாக உருவாகும்...

உருவாக்கிய நட்பக்கட்டிலும்,
உருவாகிய நடப்பை
பெரிதும் மதிப்பவன்- நான்

இந்த வகையில்
உருவாகிய நடப்பு
நம் நட்பு

இதை இன்று
உன் பிறந்தநாளில்
நினைவு கூர்வதில்
நம் நட்பின் ஆழம்
நீள்வதாக உணர்கிறேன்...

இனிய பிறந்தநாள் நல்வாழ்த்துக்கள்...
விஜயன் சீனிவாசன

இப்படித்தான் விடிந்தது இன்று

மெல்ல மெல்ல
இதையத்தை துளையிட
எங்கு கற்றுகொண்டாய்?

ஒவ்வொரு முறை
நீ அனுப்பும்
குறும் தகவல்கள்
என்னை
கொஞ்சம் கொஞ்சமாய்
கரைப்பதை
நீ அறிவாயா?


காற்றில்லாத உலகம்
உன் கனவில்லாத
உறக்கம்
இரண்டிற்கும்
வித்தியாசம்
ஏதும் தெரியவில்லை
எனக்கு.

எப்போதும் சீக்கிரம்
துயிலெழ
மறுக்கும் என் விழிகள்
இன்று
அதிகாலையிலேயே
விழித்துகொண்டது
நீ இல்லாத தூக்கம்
பிடிக்காததால்...

எழுத்தவுடன்
உன் விடுபட்ட
அழைப்பை
கண்டதும்
கலங்கிப்போனது
சந்தோசத்தால்
என் கண்கள்...

சிலமணி துளிகள்
பின்னர் மற்றுமொரு
குறும் தகவல்
ஆசை ஆசையாய்
திறந்தேன் நீ
அனுப்பியிருப்பாயென...

நண்பனின் காலை
வணக்கமது..

இப்படித்தான் விடிந்தது
இன்று எனக்கு..

2007 புத்தாண்டு வாழ்த்துக்கள்

ஏதோ நேற்றுதான்
புத்தாண்டு வாழ்த்து
அனுப்பியதாக நியாபகம்
2006 க்காக...

362 நாட்கள் கடந்துவிட்டது
என்பதே இன்னுமொரு
புத்தாண்டு வரும்போதுதான்
நினைவுக்கு வருகிறது...

இடைப்பட்ட நாட்களில்
எத்தனையோ சோதனைகள்
எத்தனையோ சாதனைகள்
உங்களை சந்த்திதிருக்கலாம்...

இனிவரும் புத்தாண்டில்
உங்களை சந்திக்க
நடுங்கட்டும் சோதனைகள்...
சட்டைப்பை பிடிக்காமல்
சிந்தட்டும் சாதனைகள்...
இவையாவும் மெய்பட
இறைவனும் துணையிருக்க
வாழ்த்துகிறேன்!!!

புத்தாண்டு நல்வாழ்த்துக்கள்!!!

அன்புடன்,
விஜயன் சீனிவாசன்

2006 புத்தாண்டு வாழ்த்துக்கள்

இதுவரை இல்லாத
ஏதோ ஒரு சிறப்பு
இந்த புத்தாண்டில்
இருபதாக உணர்கிறேன்...

முதல் முறையாக
உங்களை வாழ்த்த
ஒரு வாய்ப்பளித்ததால்...

முதல் முறையாக
இருந்தாலும் முழுமையாக
வாழ்த்த விரும்புகிறேன்...

உங்கள் கனவுகள் அனைத்தும்
வரவிருக்கும் நாட்களில்
நனவாக வேண்டுமென்று...

இனிய புத்தாண்டு நல்வாழ்த்துக்கள்!!!

அன்புடன்,
விஜயன் சீனிவாசன்