public class GECache implements IGECache {
- final Map<GECacheKey, IGECacheEntry> entries = new THashMap<GECacheKey, IGECacheEntry>();
- final Map<GECacheKey, Set<UIElementReference>> treeReferences = new THashMap<GECacheKey, Set<UIElementReference>>();
+ final Map<GECacheKey, IGECacheEntry> entries = new THashMap<>();
+ final Map<GECacheKey, Set<UIElementReference>> 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
}
+ @Override
+ public String toString() {
+ return String.format("%s@%d [key=%s, context=%s]", getClass().getSimpleName(), System.identityHashCode(this), key, context); //$NON-NLS-1$
+ }
+
};
/**
public <T> T get(NodeContext context, CacheKey<T> 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
return references.get(context) > 0;
}
- private TObjectIntHashMap<NodeContext> references = new TObjectIntHashMap<NodeContext>();
+ private TObjectIntHashMap<NodeContext> references = new TObjectIntHashMap<>();
@Override
synchronized public void incRef(NodeContext context) {