Let's see how IntelliJ IDEA 11 copes with a simple class with 3 fields:
Even though the class seems simple, the methods' size is a little bit scary.
Using EqualsBuilder and HashCodeBuilder from Apache Commons Lang makes the methods much smaller, however introduces one major flaw: it uses reflection, which isn't a speed demon.
To the rescue comes Guava, which makes these methods simpler and more readable:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Book { | |
private String author; | |
private String title; | |
private int pages; | |
public Book(String author, String title, int pages) { | |
this.author = author; | |
this.title = title; | |
this.pages = pages; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (getClass() != obj.getClass()) { | |
return false; | |
} | |
final Book other = (Book) obj; | |
return Objects.equal(this.author, other.author) && Objects.equal(this.title, other.title) && Objects.equal(this.pages, other.pages); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hashCode(author, title, pages); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Book { | |
private String author; | |
private String title; | |
private int pages; | |
public Book(String author, String title, int pages) { | |
this.author = author; | |
this.title = title; | |
this.pages = pages; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (getClass() != obj.getClass()) { | |
return false; | |
} | |
final Book other = (Book) obj; | |
return Objects.equals(this.author, other.author) && Objects.equals(this.title, other.title) && Objects.equals(this.pages, other.pages); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(author, title, pages); | |
} | |
} |
http://plugins.intellij.net/plugin/?idea&id=6875
No comments:
Post a Comment