From: Tuukka Lehtonen Date: Thu, 19 Dec 2019 10:35:40 +0000 (+0200) Subject: Added GECacheKey.toString() to allow debugging hashcode/equals problems X-Git-Tag: v1.43.0~136^2~18 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=c514d4d064547c6bd0bf2b7c182e5f8c7e389c6c Added GECacheKey.toString() to allow debugging hashcode/equals problems gitlab #312 Change-Id: I1a719581210c4587336df4e371f6dacb38c949c8 --- diff --git a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/internal/GECache.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/internal/GECache.java index 014a5c0d5..bdc0e16a0 100644 --- a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/internal/GECache.java +++ b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/internal/GECache.java @@ -24,38 +24,38 @@ import org.simantics.browsing.ui.NodeContext.CacheKey; public class GECache implements IGECache { - final Map entries = new THashMap(); - final Map> treeReferences = new THashMap>(); + final Map entries = new THashMap<>(); + final Map> treeReferences = new THashMap<>(); final private static class GECacheKey { private NodeContext context; private CacheKey key; + private int hash; GECacheKey(NodeContext context, CacheKey key) { - this.context = context; - this.key = key; - if (context == null || key == null) - throw new IllegalArgumentException("Null context or key is not accepted"); + setValues(context, key); } GECacheKey(GECacheKey other) { - this.context = other.context; - this.key = other.key; - if (context == null || key == null) - throw new IllegalArgumentException("Null context or key is not accepted"); + setValues(other.context, other.key); } void setValues(NodeContext context, CacheKey key) { + if (context == null || key == null) + throw new IllegalArgumentException("Null context or key is not accepted"); this.context = context; this.key = key; - if (context == null || key == null) - throw new IllegalArgumentException("Null context or key is not accepted"); + this.hash = calcHash(); + } + + private int calcHash() { + return (31 * context.hashCode()) + key.hashCode(); } @Override public int hashCode() { - return context.hashCode() | key.hashCode(); + return hash; } @Override @@ -74,6 +74,11 @@ public class GECache implements IGECache { } + @Override + public String toString() { + return String.format("%s@%d [key=%s, context=%s]", getClass().getSimpleName(), System.identityHashCode(this), key, context); //$NON-NLS-1$ + } + }; /** @@ -116,9 +121,7 @@ public class GECache implements IGECache { public T get(NodeContext context, CacheKey key) { getKey.setValues(context, key); IGECacheEntry entry = entries.get(getKey); - if (entry == null) - return null; - return (T) entry.getValue(); + return entry != null ? (T) entry.getValue() : null; } @Override @@ -171,7 +174,7 @@ public class GECache implements IGECache { return references.get(context) > 0; } - private TObjectIntHashMap references = new TObjectIntHashMap(); + private TObjectIntHashMap references = new TObjectIntHashMap<>(); @Override synchronized public void incRef(NodeContext context) {