From ef756bdcc1914eddf04ec41791fc0779aef9e2e5 Mon Sep 17 00:00:00 2001 From: hns Date: Tue, 2 Oct 2001 20:50:52 +0000 Subject: [PATCH] Hash codes of key classes are now cached lazily. Slightly changed the algorithm according the recommendations in "Effective Java" by Joshua Bloch. --- src/helma/objectmodel/db/DbKey.java | 10 +++++++++- src/helma/objectmodel/db/SyntheticKey.java | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/helma/objectmodel/db/DbKey.java b/src/helma/objectmodel/db/DbKey.java index d5fc2a77..ff8b5041 100644 --- a/src/helma/objectmodel/db/DbKey.java +++ b/src/helma/objectmodel/db/DbKey.java @@ -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 () { diff --git a/src/helma/objectmodel/db/SyntheticKey.java b/src/helma/objectmodel/db/SyntheticKey.java index 86934505..7f1911ac 100644 --- a/src/helma/objectmodel/db/SyntheticKey.java +++ b/src/helma/objectmodel/db/SyntheticKey.java @@ -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; }