Hash codes of key classes are now cached lazily.

Slightly changed the algorithm according the recommendations
in "Effective Java" by Joshua Bloch.
This commit is contained in:
hns 2001-10-02 20:50:52 +00:00
parent 17b82f0d43
commit ef756bdcc1
2 changed files with 15 additions and 2 deletions

View file

@ -21,6 +21,9 @@ public final class DbKey implements Key, Serializable {
// the id that defines this key's object within the above storage space
private final String id;
// lazily initialized hashcode
private transient int hashcode = 0;
/**
* make a key for a persistent Object, describing its datasource and id.
*/
@ -44,7 +47,12 @@ public final class DbKey implements Key, Serializable {
}
public int hashCode () {
return storageName == null ? id.hashCode () : storageName.hashCode() + id.hashCode ();
if (hashcode == 0) {
hashcode = storageName == null ?
17 + 37*id.hashCode () :
17 + 37*storageName.hashCode() + +37*id.hashCode ();
}
return hashcode;
}
public Key getParentKey () {

View file

@ -16,6 +16,9 @@ public final class SyntheticKey implements Key, Serializable {
private final Key parentKey;
private final String name;
// lazily initialized hashcode
private transient int hashcode = 0;
/**
* make a key for a persistent Object, describing its datasource and id.
@ -39,7 +42,9 @@ public final class SyntheticKey implements Key, Serializable {
}
public int hashCode () {
return name.hashCode () + parentKey.hashCode ();
if (hashcode == 0)
hashcode = 17 + 37*name.hashCode () + 37*parentKey.hashCode ();
return hashcode;
}