Overriding toString, equals, hashCode and compareTo

Hi,

do you guys think this class is ok regarding to equals, compareTo, toString and hashCode? I think it's ok but I might be missing some hole.

publicclass AutenticationDataVOimplements Serializable, Comparable{

privatefinal String login;

privatefinal String password;

public AutenticationDataVO(String login, String password){

if (login ==null)

thrownew IllegalArgumentException("Login n鉶 pode ter o valor null");

if (password ==null)

thrownew IllegalArgumentException("Password n鉶 pode ter o valor null");

this.login=login;

this.password=password;

};

public String getLogin(){

return login;

}

public String getPassword(){

return password;

}

public String toString(){

StringBuffer sb =new StringBuffer();

sb.append("[");

sb.append("login=");

if (login !=null){

sb.append(login);

}

sb.append(";");

sb.append("password=");

if ( password!=null){

sb.append(password);

}

sb.append("]");

return sb.toString();

}

publicint hashCode(){

return this.toString().hashCode();

}

publicboolean equals(Object obj){

boolean answer =false;

if (objinstanceof AutenticationDataVO)

{

answer = (this.toString().equals(((AutenticationDataVO)obj).toString()));

}

return answer;

}

publicint compareTo(Object obj){

if (obj ==null)

thrownew IllegalArgumentException("Compara珲es com objectos null n鉶 s鉶 permitidas");

if ( !(objinstanceof AutenticationDataVO) )

thrownew IllegalArgumentException("Compara珲es com objectos de diferentes classes n鉶 s鉶 permitidas");

return (this.toString().compareTo(((AutenticationDataVO)obj).toString()));

}

}

thanks in advance!

Manuel Leiria

[4282 byte] By [manuel.leiriaa] at [2007-11-27 7:03:42]
# 1

toString() - looks okay, you can't do much wrong with that. Although you might want to store the result of that concatenation operation and re-use it on subsequent calls, as your instances are immutable.

Considering your increased usage of that method, it might save some runtime...

As for equals(): IMO it would be clearer if you just compared the two attributes, with the same effect. hashcode() could remain untouched.

CeciNEstPasUnProgrammeura at 2007-7-12 18:54:57 > top of Java-index,Java Essentials,Java Programming...
# 2
The compareTo method should throw a ClassCastException when the type of the object obj doesn't allow it to be compared, not IllegalArgumentException. Otherwise everything looks good.
jsalonena at 2007-7-12 18:54:57 > top of Java-index,Java Essentials,Java Programming...
# 3

> toString() - looks okay, you can't do much wrong with

> that. Although you might want to store the result of

> that concatenation operation and re-use it on

> subsequent calls, as your instances are immutable.

>

> Considering your increased usage of that method, it

> might save some runtime...

thanks for the tips!

> As for equals(): IMO it would be clearer if you just

> compared the two attributes, with the same effect.

> hashcode() could remain untouched.

you mean something like this:

public boolean equals(Object obj){

boolean answer = false;

if (obj instanceof AutenticationDataVO)

{

if(this.login.equals(((AutenticationDataVO)obj).getLogin() && this.password.equals(((AutenticationDataVO)obj).getPassword())

answer = true;

}

return answer;

thanks

manuel.leiriaa at 2007-7-12 18:54:57 > top of Java-index,Java Essentials,Java Programming...
# 4

> The compareTo method should throw a

> ClassCastException when the type of the object obj

> doesn't allow it to be compared, not

> IllegalArgumentException. Otherwise everything looks

> good.

you're right! my mistake.

thanks,

Manuel Leiria

manuel.leiriaa at 2007-7-12 18:54:57 > top of Java-index,Java Essentials,Java Programming...