1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.common.internal;
\r
14 import gnu.trove.map.hash.THashMap;
\r
15 import gnu.trove.map.hash.TObjectIntHashMap;
\r
16 import gnu.trove.set.hash.THashSet;
\r
18 import java.util.Collections;
\r
19 import java.util.Map;
\r
20 import java.util.Set;
\r
22 import org.simantics.browsing.ui.NodeContext;
\r
23 import org.simantics.browsing.ui.NodeContext.CacheKey;
\r
25 public class GECache implements IGECache {
\r
27 final Map<GECacheKey, IGECacheEntry> entries = new THashMap<GECacheKey, IGECacheEntry>();
\r
28 final Map<GECacheKey, Set<UIElementReference>> treeReferences = new THashMap<GECacheKey, Set<UIElementReference>>();
\r
30 final private static class GECacheKey {
\r
32 private NodeContext context;
\r
33 private CacheKey<?> key;
\r
35 GECacheKey(NodeContext context, CacheKey<?> key) {
\r
36 this.context = context;
\r
38 if (context == null || key == null)
\r
39 throw new IllegalArgumentException("Null context or key is not accepted");
\r
42 GECacheKey(GECacheKey other) {
\r
43 this.context = other.context;
\r
44 this.key = other.key;
\r
45 if (context == null || key == null)
\r
46 throw new IllegalArgumentException("Null context or key is not accepted");
\r
49 void setValues(NodeContext context, CacheKey<?> key) {
\r
50 this.context = context;
\r
52 if (context == null || key == null)
\r
53 throw new IllegalArgumentException("Null context or key is not accepted");
\r
57 public int hashCode() {
\r
58 return context.hashCode() | key.hashCode();
\r
62 public boolean equals(Object object) {
\r
66 else if (object == null)
\r
68 // else if (getClass() != object.getClass())
\r
71 GECacheKey i = (GECacheKey)object;
\r
73 return key.equals(i.key) && context.equals(i.context);
\r
80 * This single instance is used for all get operations from the cache. This
\r
81 * should work since the GE cache is meant to be single-threaded within the
\r
82 * current UI thread, what ever that thread is. For put operations which
\r
83 * store the key, this is not used.
\r
85 NodeContext getNC = new NodeContext() {
\r
86 @SuppressWarnings("rawtypes")
\r
88 public Object getAdapter(Class adapter) {
\r
93 public <T> T getConstant(ConstantKey<T> key) {
\r
98 public Set<ConstantKey<?>> getKeys() {
\r
99 return Collections.emptySet();
\r
102 CacheKey<?> getCK = new CacheKey<Object>() {
\r
104 public Object processorIdenfitier() {
\r
108 GECacheKey getKey = new GECacheKey(getNC, getCK);
\r
110 public <T> IGECacheEntry put(NodeContext context, CacheKey<T> key, T value) {
\r
111 IGECacheEntry entry = new GECacheEntry(context, key, value);
\r
112 entries.put(new GECacheKey(context, key), entry);
\r
116 @SuppressWarnings("unchecked")
\r
117 public <T> T get(NodeContext context, CacheKey<T> key) {
\r
118 getKey.setValues(context, key);
\r
119 IGECacheEntry entry = entries.get(getKey);
\r
122 return (T) entry.getValue();
\r
126 public <T> IGECacheEntry getEntry(NodeContext context, CacheKey<T> key) {
\r
127 assert(context != null);
\r
128 assert(key != null);
\r
129 getKey.setValues(context, key);
\r
130 return entries.get(getKey);
\r
134 public <T> void remove(NodeContext context, CacheKey<T> key) {
\r
135 getKey.setValues(context, key);
\r
136 entries.remove(getKey);
\r
140 public <T> Set<UIElementReference> getTreeReference(NodeContext context, CacheKey<T> key) {
\r
141 assert(context != null);
\r
142 assert(key != null);
\r
143 getKey.setValues(context, key);
\r
144 return treeReferences.get(getKey);
\r
148 public <T> void putTreeReference(NodeContext context, CacheKey<T> key, UIElementReference reference) {
\r
149 assert(context != null);
\r
150 assert(key != null);
\r
151 getKey.setValues(context, key);
\r
152 Set<UIElementReference> refs = treeReferences.get(getKey);
\r
153 if (refs != null) {
\r
154 refs.add(reference);
\r
156 refs = new THashSet<UIElementReference>(4);
\r
157 refs.add(reference);
\r
158 treeReferences.put(new GECacheKey(getKey), refs);
\r
163 public <T> Set<UIElementReference> removeTreeReference(NodeContext context, CacheKey<T> key) {
\r
164 assert(context != null);
\r
165 assert(key != null);
\r
166 getKey.setValues(context, key);
\r
167 return treeReferences.remove(getKey);
\r
171 public boolean isShown(NodeContext context) {
\r
172 return references.get(context) > 0;
\r
175 private TObjectIntHashMap<NodeContext> references = new TObjectIntHashMap<NodeContext>();
\r
178 public void incRef(NodeContext context) {
\r
179 int exist = references.get(context);
\r
180 references.put(context, exist+1);
\r
184 public void decRef(NodeContext context) {
\r
185 int exist = references.get(context);
\r
186 references.put(context, exist-1);
\r
188 references.remove(context);
\r
192 public void dispose() {
\r
193 references.clear();
\r
195 treeReferences.clear();
\r