1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.internal;
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.THashSet;
18 import java.util.Collections;
22 import org.simantics.browsing.ui.NodeContext;
23 import org.simantics.browsing.ui.NodeContext.CacheKey;
25 public class GECache implements IGECache {
27 final Map<GECacheKey, IGECacheEntry> entries = new THashMap<>();
28 final Map<GECacheKey, Set<UIElementReference>> treeReferences = new THashMap<>();
30 final private static class GECacheKey {
32 private NodeContext context;
33 private CacheKey<?> key;
36 GECacheKey(NodeContext context, CacheKey<?> key) {
37 setValues(context, key);
40 GECacheKey(GECacheKey other) {
41 setValues(other.context, other.key);
44 void setValues(NodeContext context, CacheKey<?> key) {
45 if (context == null || key == null)
46 throw new IllegalArgumentException("Null context or key is not accepted");
47 this.context = context;
49 this.hash = calcHash();
52 private int calcHash() {
53 return (31 * context.hashCode()) + key.hashCode();
57 public int hashCode() {
62 public boolean equals(Object object) {
66 else if (object == null)
68 // else if (getClass() != object.getClass())
71 GECacheKey i = (GECacheKey)object;
73 return key.equals(i.key) && context.equals(i.context);
78 public String toString() {
79 return String.format("%s@%d [key=%s, context=%s]", getClass().getSimpleName(), System.identityHashCode(this), key, context); //$NON-NLS-1$
85 * This single instance is used for all get operations from the cache. This
86 * should work since the GE cache is meant to be single-threaded within the
87 * current UI thread, what ever that thread is. For put operations which
88 * store the key, this is not used.
90 NodeContext getNC = new NodeContext() {
92 public <T> T getAdapter(Class<T> adapter) {
97 public <T> T getConstant(ConstantKey<T> key) {
102 public Set<ConstantKey<?>> getKeys() {
103 return Collections.emptySet();
106 CacheKey<?> getCK = new CacheKey<Object>() {
108 public Object processorIdenfitier() {
112 GECacheKey getKey = new GECacheKey(getNC, getCK);
114 public <T> IGECacheEntry put(NodeContext context, CacheKey<T> key, T value) {
115 IGECacheEntry entry = new GECacheEntry(context, key, value);
116 entries.put(new GECacheKey(context, key), entry);
120 @SuppressWarnings("unchecked")
121 public <T> T get(NodeContext context, CacheKey<T> key) {
122 getKey.setValues(context, key);
123 IGECacheEntry entry = entries.get(getKey);
124 return entry != null ? (T) entry.getValue() : null;
128 public <T> IGECacheEntry getEntry(NodeContext context, CacheKey<T> key) {
129 assert(context != null);
131 getKey.setValues(context, key);
132 return entries.get(getKey);
136 public <T> void remove(NodeContext context, CacheKey<T> key) {
137 getKey.setValues(context, key);
138 entries.remove(getKey);
142 public <T> Set<UIElementReference> getTreeReference(NodeContext context, CacheKey<T> key) {
143 assert(context != null);
145 getKey.setValues(context, key);
146 return treeReferences.get(getKey);
150 public <T> void putTreeReference(NodeContext context, CacheKey<T> key, UIElementReference reference) {
151 assert(context != null);
153 getKey.setValues(context, key);
154 Set<UIElementReference> refs = treeReferences.get(getKey);
158 refs = new THashSet<UIElementReference>(4);
160 treeReferences.put(new GECacheKey(getKey), refs);
165 public <T> Set<UIElementReference> removeTreeReference(NodeContext context, CacheKey<T> key) {
166 assert(context != null);
168 getKey.setValues(context, key);
169 return treeReferences.remove(getKey);
173 public boolean isShown(NodeContext context) {
174 return references.get(context) > 0;
177 private TObjectIntHashMap<NodeContext> references = new TObjectIntHashMap<>();
180 synchronized public void incRef(NodeContext context) {
181 int exist = references.get(context);
182 references.put(context, exist+1);
186 synchronized public void decRef(NodeContext context) {
187 int exist = references.get(context);
188 references.put(context, exist-1);
190 references.remove(context);
194 public void dispose() {
197 treeReferences.clear();