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<GECacheKey, IGECacheEntry>();
28 final Map<GECacheKey, Set<UIElementReference>> treeReferences = new THashMap<GECacheKey, Set<UIElementReference>>();
30 final private static class GECacheKey {
32 private NodeContext context;
33 private CacheKey<?> key;
35 GECacheKey(NodeContext context, CacheKey<?> key) {
36 this.context = context;
38 if (context == null || key == null)
39 throw new IllegalArgumentException("Null context or key is not accepted");
42 GECacheKey(GECacheKey other) {
43 this.context = other.context;
45 if (context == null || key == null)
46 throw new IllegalArgumentException("Null context or key is not accepted");
49 void setValues(NodeContext context, CacheKey<?> key) {
50 this.context = context;
52 if (context == null || key == null)
53 throw new IllegalArgumentException("Null context or key is not accepted");
57 public int hashCode() {
58 return context.hashCode() | key.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);
80 * This single instance is used for all get operations from the cache. This
81 * should work since the GE cache is meant to be single-threaded within the
82 * current UI thread, what ever that thread is. For put operations which
83 * store the key, this is not used.
85 NodeContext getNC = new NodeContext() {
87 public <T> T getAdapter(Class<T> adapter) {
92 public <T> T getConstant(ConstantKey<T> key) {
97 public Set<ConstantKey<?>> getKeys() {
98 return Collections.emptySet();
101 CacheKey<?> getCK = new CacheKey<Object>() {
103 public Object processorIdenfitier() {
107 GECacheKey getKey = new GECacheKey(getNC, getCK);
109 public <T> IGECacheEntry put(NodeContext context, CacheKey<T> key, T value) {
110 IGECacheEntry entry = new GECacheEntry(context, key, value);
111 entries.put(new GECacheKey(context, key), entry);
115 @SuppressWarnings("unchecked")
116 public <T> T get(NodeContext context, CacheKey<T> key) {
117 getKey.setValues(context, key);
118 IGECacheEntry entry = entries.get(getKey);
121 return (T) entry.getValue();
125 public <T> IGECacheEntry getEntry(NodeContext context, CacheKey<T> key) {
126 assert(context != null);
128 getKey.setValues(context, key);
129 return entries.get(getKey);
133 public <T> void remove(NodeContext context, CacheKey<T> key) {
134 getKey.setValues(context, key);
135 entries.remove(getKey);
139 public <T> Set<UIElementReference> getTreeReference(NodeContext context, CacheKey<T> key) {
140 assert(context != null);
142 getKey.setValues(context, key);
143 return treeReferences.get(getKey);
147 public <T> void putTreeReference(NodeContext context, CacheKey<T> key, UIElementReference reference) {
148 assert(context != null);
150 getKey.setValues(context, key);
151 Set<UIElementReference> refs = treeReferences.get(getKey);
155 refs = new THashSet<UIElementReference>(4);
157 treeReferences.put(new GECacheKey(getKey), refs);
162 public <T> Set<UIElementReference> removeTreeReference(NodeContext context, CacheKey<T> key) {
163 assert(context != null);
165 getKey.setValues(context, key);
166 return treeReferences.remove(getKey);
170 public boolean isShown(NodeContext context) {
171 return references.get(context) > 0;
174 private TObjectIntHashMap<NodeContext> references = new TObjectIntHashMap<NodeContext>();
177 public void incRef(NodeContext context) {
178 int exist = references.get(context);
179 references.put(context, exist+1);
183 public void decRef(NodeContext context) {
184 int exist = references.get(context);
185 references.put(context, exist-1);
187 references.remove(context);
191 public void dispose() {
194 treeReferences.clear();