]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/internal/GECache.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / internal / GECache.java
index 600d16ff3cb2444252925877a085b37b5f6d055e..b5545df0065264423b0bf9d240679a94063d05aa 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.browsing.ui.common.internal;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-import gnu.trove.map.hash.TObjectIntHashMap;\r
-import gnu.trove.set.hash.THashSet;\r
-\r
-import java.util.Collections;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.NodeContext.CacheKey;\r
-\r
-public class GECache implements IGECache {\r
-\r
-    final Map<GECacheKey, IGECacheEntry> entries = new THashMap<GECacheKey, IGECacheEntry>();\r
-    final Map<GECacheKey, Set<UIElementReference>> treeReferences = new THashMap<GECacheKey, Set<UIElementReference>>();\r
-\r
-    final private static class GECacheKey {\r
-\r
-        private NodeContext context;\r
-        private CacheKey<?> key;\r
-\r
-        GECacheKey(NodeContext context, CacheKey<?> key) {\r
-            this.context = context;\r
-            this.key = key;\r
-            if (context == null || key == null) \r
-               throw new IllegalArgumentException("Null context or key is not accepted");\r
-        }\r
-\r
-        GECacheKey(GECacheKey other) {\r
-            this.context = other.context;\r
-            this.key = other.key;\r
-            if (context == null || key == null) \r
-               throw new IllegalArgumentException("Null context or key is not accepted");\r
-        }\r
-\r
-        void setValues(NodeContext context, CacheKey<?> key) {\r
-            this.context = context;\r
-            this.key = key;\r
-            if (context == null || key == null) \r
-               throw new IllegalArgumentException("Null context or key is not accepted");\r
-        }\r
-\r
-        @Override\r
-        public int hashCode() {\r
-            return context.hashCode() | key.hashCode();\r
-        }\r
-\r
-        @Override\r
-        public boolean equals(Object object) {\r
-\r
-            if (this == object)\r
-                return true;\r
-            else if (object == null)\r
-                return false;\r
-//            else if (getClass() != object.getClass())\r
-//                return false;\r
-\r
-            GECacheKey i = (GECacheKey)object;\r
-\r
-            return key.equals(i.key) && context.equals(i.context);\r
-\r
-        }\r
-\r
-    };\r
-\r
-    /**\r
-     * This single instance is used for all get operations from the cache. This\r
-     * should work since the GE cache is meant to be single-threaded within the\r
-     * current UI thread, what ever that thread is. For put operations which\r
-     * store the key, this is not used.\r
-     */\r
-    NodeContext getNC = new NodeContext() {\r
-               @Override\r
-       public <T> T getAdapter(Class<T> adapter) {\r
-               return null;\r
-       }\r
-       \r
-       @Override\r
-       public <T> T getConstant(ConstantKey<T> key) {\r
-               return null;\r
-       }\r
-       \r
-       @Override\r
-       public Set<ConstantKey<?>> getKeys() {\r
-               return Collections.emptySet();\r
-       }\r
-    };\r
-    CacheKey<?> getCK = new CacheKey<Object>() {\r
-       @Override\r
-       public Object processorIdenfitier() {\r
-               return this;\r
-       }\r
-       };\r
-    GECacheKey getKey = new GECacheKey(getNC, getCK);\r
-\r
-    public <T> IGECacheEntry put(NodeContext context, CacheKey<T> key, T value) {\r
-        IGECacheEntry entry = new GECacheEntry(context, key, value);\r
-        entries.put(new GECacheKey(context, key), entry);\r
-        return entry;\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-    public <T> T get(NodeContext context, CacheKey<T> key) {\r
-        getKey.setValues(context, key);\r
-        IGECacheEntry entry = entries.get(getKey);\r
-        if (entry == null)\r
-            return null;\r
-        return (T) entry.getValue();\r
-    }\r
-\r
-    @Override\r
-    public <T> IGECacheEntry getEntry(NodeContext context, CacheKey<T> key) {\r
-        assert(context != null);\r
-        assert(key != null);\r
-        getKey.setValues(context, key);\r
-        return entries.get(getKey);\r
-    }\r
-\r
-    @Override\r
-    public <T> void remove(NodeContext context, CacheKey<T> key) {\r
-        getKey.setValues(context, key);\r
-        entries.remove(getKey);\r
-    }\r
-\r
-    @Override\r
-    public <T> Set<UIElementReference> getTreeReference(NodeContext context, CacheKey<T> key) {\r
-        assert(context != null);\r
-        assert(key != null);\r
-        getKey.setValues(context, key);\r
-        return treeReferences.get(getKey);\r
-    }\r
-\r
-    @Override\r
-    public <T> void putTreeReference(NodeContext context, CacheKey<T> key, UIElementReference reference) {\r
-        assert(context != null);\r
-        assert(key != null);\r
-        getKey.setValues(context, key);\r
-        Set<UIElementReference> refs = treeReferences.get(getKey);\r
-        if (refs != null) {\r
-            refs.add(reference);\r
-        } else {\r
-            refs = new THashSet<UIElementReference>(4);\r
-            refs.add(reference);\r
-            treeReferences.put(new GECacheKey(getKey), refs);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public <T> Set<UIElementReference> removeTreeReference(NodeContext context, CacheKey<T> key) {\r
-        assert(context != null);\r
-        assert(key != null);\r
-        getKey.setValues(context, key);\r
-        return treeReferences.remove(getKey);\r
-    }\r
-    \r
-    @Override\r
-    public boolean isShown(NodeContext context) {\r
-       return references.get(context) > 0;\r
-    }\r
-\r
-    private TObjectIntHashMap<NodeContext> references = new TObjectIntHashMap<NodeContext>();\r
-    \r
-    @Override\r
-    public void incRef(NodeContext context) {\r
-       int exist = references.get(context);\r
-       references.put(context, exist+1);\r
-    }\r
-    \r
-    @Override\r
-    public void decRef(NodeContext context) {\r
-       int exist = references.get(context);\r
-       references.put(context, exist-1);\r
-       if(exist == 1) {\r
-               references.remove(context);\r
-       }\r
-    }\r
-    \r
-    public void dispose() {\r
-       references.clear();\r
-       entries.clear();\r
-       treeReferences.clear();\r
-    }\r
-    \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.common.internal;
+
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.map.hash.TObjectIntHashMap;
+import gnu.trove.set.hash.THashSet;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.NodeContext.CacheKey;
+
+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 private static class GECacheKey {
+
+        private NodeContext context;
+        private CacheKey<?> key;
+
+        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");
+        }
+
+        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");
+        }
+
+        void setValues(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");
+        }
+
+        @Override
+        public int hashCode() {
+            return context.hashCode() | key.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object object) {
+
+            if (this == object)
+                return true;
+            else if (object == null)
+                return false;
+//            else if (getClass() != object.getClass())
+//                return false;
+
+            GECacheKey i = (GECacheKey)object;
+
+            return key.equals(i.key) && context.equals(i.context);
+
+        }
+
+    };
+
+    /**
+     * This single instance is used for all get operations from the cache. This
+     * should work since the GE cache is meant to be single-threaded within the
+     * current UI thread, what ever that thread is. For put operations which
+     * store the key, this is not used.
+     */
+    NodeContext getNC = new NodeContext() {
+               @Override
+       public <T> T getAdapter(Class<T> adapter) {
+               return null;
+       }
+       
+       @Override
+       public <T> T getConstant(ConstantKey<T> key) {
+               return null;
+       }
+       
+       @Override
+       public Set<ConstantKey<?>> getKeys() {
+               return Collections.emptySet();
+       }
+    };
+    CacheKey<?> getCK = new CacheKey<Object>() {
+       @Override
+       public Object processorIdenfitier() {
+               return this;
+       }
+       };
+    GECacheKey getKey = new GECacheKey(getNC, getCK);
+
+    public <T> IGECacheEntry put(NodeContext context, CacheKey<T> key, T value) {
+        IGECacheEntry entry = new GECacheEntry(context, key, value);
+        entries.put(new GECacheKey(context, key), entry);
+        return entry;
+    }
+
+    @SuppressWarnings("unchecked")
+    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();
+    }
+
+    @Override
+    public <T> IGECacheEntry getEntry(NodeContext context, CacheKey<T> key) {
+        assert(context != null);
+        assert(key != null);
+        getKey.setValues(context, key);
+        return entries.get(getKey);
+    }
+
+    @Override
+    public <T> void remove(NodeContext context, CacheKey<T> key) {
+        getKey.setValues(context, key);
+        entries.remove(getKey);
+    }
+
+    @Override
+    public <T> Set<UIElementReference> getTreeReference(NodeContext context, CacheKey<T> key) {
+        assert(context != null);
+        assert(key != null);
+        getKey.setValues(context, key);
+        return treeReferences.get(getKey);
+    }
+
+    @Override
+    public <T> void putTreeReference(NodeContext context, CacheKey<T> key, UIElementReference reference) {
+        assert(context != null);
+        assert(key != null);
+        getKey.setValues(context, key);
+        Set<UIElementReference> refs = treeReferences.get(getKey);
+        if (refs != null) {
+            refs.add(reference);
+        } else {
+            refs = new THashSet<UIElementReference>(4);
+            refs.add(reference);
+            treeReferences.put(new GECacheKey(getKey), refs);
+        }
+    }
+
+    @Override
+    public <T> Set<UIElementReference> removeTreeReference(NodeContext context, CacheKey<T> key) {
+        assert(context != null);
+        assert(key != null);
+        getKey.setValues(context, key);
+        return treeReferences.remove(getKey);
+    }
+    
+    @Override
+    public boolean isShown(NodeContext context) {
+       return references.get(context) > 0;
+    }
+
+    private TObjectIntHashMap<NodeContext> references = new TObjectIntHashMap<NodeContext>();
+    
+    @Override
+    public void incRef(NodeContext context) {
+       int exist = references.get(context);
+       references.put(context, exist+1);
+    }
+    
+    @Override
+    public void decRef(NodeContext context) {
+       int exist = references.get(context);
+       references.put(context, exist-1);
+       if(exist == 1) {
+               references.remove(context);
+       }
+    }
+    
+    public void dispose() {
+       references.clear();
+       entries.clear();
+       treeReferences.clear();
+    }
+    
+}