Ruminations of a Programmer: Domain Driven Design - Inject Repositories, not DAOs in Domain Entities
"getEmployeesByName"
Java Practices -> Avoid clone
"//get initial bit-by-bit copy, which handles all immutable fields Fruit result = (Fruit)super.clone(); //mutable fields need to be made independent of this object, for reasons //similar to those for defensive copies - to prevent unwanted access to //this object's internal state result.fBestBeforeDate = new Date( this.fBestBeforeDate.getTime() ); return result; "
Overriding equals and hashCode in Java - Stack Overflow
"The theory (for the language lawyers and the mathematically inclined):
equals() (javadoc) must define an equality relation (it must be reflexive, symmetric, and transitive). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value). Furthermore, o.equals(null) must always return false.
hashCode() (javadoc) must also be consistent (if the object is not modified in terms of equals(), it must keep returning the same value).
The relation between the two methods is:
Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
In practice:
If you override one, then you should override the other.
Use the same set of fields that you use to compute equals() to compute hashCode().
Use the excellent helper classes EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. An example:
public class Person {
private String name;
private int age;
// ...
public int hashCode() {
return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
// if deriving: appendSuper(super.hashCode()).
append(name).
append(age).
toHashCode();
}
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
Person rhs = (Person) obj;
return new EqualsBuilder().
// if deriving: appendSuper(super.equals(obj)).
append(name, rhs.name).
append(age, rhs.age).
isEquals();
}
}
Also remember:
When using a hash-based Collection or Map such as HashSet, LinkedHashSet, HashMap, Hashtable, or WeakHashMap, make sure that the hashCode() of the key objects that you put into the collection never changes while the object is in the collection. The bulletproof way to ensure this is to make your keys immutable, which has also other benefits."
Some Objects are More Equal Than Others
"if (!super.equals(other)) return false;
// cast other to Manager and compare fields
return bonus == ((Manager)other).bonus;"
Top Tags
Sponsored Links
View All Recent Tags (46)
- 58flex,
- 9java,
- 4android,
- 3hibernate,
- 2windows,
- 2maven,
- 2photoshop,
- 2audigy,
- 2creative,
- 2swiz,
- 1tweaks,
- 17,
- 1vps,
- 1webhosting,
- 1clone,
- 1restaurant,
- 1chinees,
- 1ee,
- 1equals,
- 1hashcode,
- 1jsf,
- 1flexmojos,
- 1graniteds,
- 1software,
- 1dns,
- 1phpbb,
- 1css,
- 1color,
- 1freelance,
- 1phpbb3,
- 1griep,
- 1broadcom,
- 1driver,
- 1logies,
- 1icons,
- 1metadata,
- 1outlook,
- 1eclipse,
- 1junit,
- 1datagrid,
- 1google,
- 1calendar,
- 1wordpress,
- 1plugin,
- 1jpa,
- 1blazeds
Public Tags (1027)
Wim Bervoets's Public Lists (0)
No lists have been created yet.
"List" is a great way to organize, share and display your specific collection of bookmarks.
Diigo is about better ways to research, share and collaborate on information. Learn more »
Join Diigo