]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHashMap.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / BinaryQueryHashMap.java
index a7a4411b8a127b17355d0b1c0cdb596d26691f25..6556daf382a21878bcd6535f057282468c6a5b1f 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.db.impl.query;\r
-\r
-import java.lang.reflect.Array;\r
-import java.util.ArrayList;\r
-\r
-\r
-\r
-/**\r
- * An implementation of the Map interface which uses an open addressed\r
- * hash table to store its contents.\r
- *\r
- * Created: Sun Nov  4 08:52:45 2001\r
- *\r
- * @author Eric D. Friedman\r
- * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $\r
- */\r
-public class BinaryQueryHashMap<Procedure> extends BinaryQueryHash<Procedure> {\r
-\r
-    /**\r
-     * Creates a new <code>THashMap</code> instance with the default\r
-     * capacity and load factor.\r
-     */\r
-    public BinaryQueryHashMap() {\r
-        super();\r
-    }\r
-\r
-    /**\r
-     * initialize the value array of the map.\r
-     *\r
-     * @param initialCapacity an <code>int</code> value\r
-     * @return an <code>int</code> value\r
-     */\r
-    protected int setUp(int initialCapacity) {\r
-        int capacity;\r
-        \r
-        capacity = super.setUp(initialCapacity);\r
-//        _values = (Object[]) new Object[capacity];\r
-        return capacity;\r
-    }\r
-    \r
-    /**\r
-     * Inserts a key/value pair into the map.\r
-     *\r
-     * @param key an <code>Object</code> value\r
-     * @param value an <code>Object</code> value\r
-     * @return the previous value associated with <tt>key</tt>,\r
-     * or null if none was found.\r
-     */\r
-       public BinaryQuery<Procedure> put(final long id, final BinaryQuery<Procedure> value) {\r
-       BinaryQuery<Procedure> previous = null;\r
-        Object oldKey;\r
-        int index = insertionIndex(id);\r
-        boolean isNewMapping = true;\r
-        if (index < 0) {\r
-            index = -index -1;\r
-            previous = _set[index];\r
-            isNewMapping = false;\r
-            new Exception().printStackTrace();\r
-            throw new Error("Duplicate entry in BinaryQueryHashMap2 " + value);\r
-        }\r
-        oldKey = _set[index];\r
-        _set[index] = value;\r
-        if (isNewMapping) {\r
-            postInsertHook(oldKey == null);\r
-        }\r
-\r
-        return previous;\r
-        \r
-    }\r
-\r
-    /**\r
-     * rehashes the map to the new capacity.\r
-     *\r
-     * @param newCapacity an <code>int</code> value\r
-     */\r
-    @SuppressWarnings("unchecked")\r
-       protected void rehash(int newCapacity) {\r
-        \r
-        int oldCapacity = _set.length;\r
-        BinaryQuery oldKeys[] = _set;\r
-\r
-        _set = (BinaryQuery[])Array.newInstance(BinaryQuery.class, newCapacity);\r
-\r
-        for (int i = oldCapacity; i-- > 0;) {\r
-            if(oldKeys[i] != null && oldKeys[i] != REMOVED) {\r
-                BinaryQuery o = oldKeys[i];\r
-                int index = insertionIndex(o.id);\r
-                if (index < 0) {\r
-                    throwObjectContractViolation(_set[(-index -1)], o);\r
-                }\r
-                _set[index] = o;\r
-            }\r
-        }\r
-    }\r
-\r
-    /**\r
-     * retrieves the value for <tt>key</tt>\r
-     *\r
-     * @param key an <code>Object</code> value\r
-     * @return the value of <tt>key</tt> or null if no such mapping exists.\r
-     */\r
-    public BinaryQuery<Procedure> get(final long id) {\r
-        int index = index(id);\r
-        return index < 0 ? null : _set[index];\r
-    }\r
-\r
-    /**\r
-     * Deletes a key/value pair from the map.\r
-     *\r
-     * @param key an <code>Object</code> value\r
-     * @return an <code>Object</code> value\r
-     */\r
-    public Object remove(final long id) {\r
-        Object prev = null;\r
-        int index = index(id);\r
-        if (index >= 0) {\r
-            prev = _set[index];\r
-            removeAt(index);    // clear key,state; adjust size\r
-        }\r
-        return prev;\r
-    }\r
-    \r
-    final public void values(int level, CacheCollectionResult result) {\r
-\r
-        for (int i = _set.length; i-- > 0;) {\r
-               CacheEntryBase entry = _set[i];\r
-            if(entry != null && entry != REMOVED) {\r
-               if(entry.getLevel() <= level)\r
-                       result.add(entry);\r
-            }\r
-        }\r
-\r
-    }\r
-\r
-    final public ArrayList<CacheEntry> values() {\r
-\r
-       ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();\r
-       \r
-        for (int i = _set.length; i-- > 0;) {\r
-            if(_set[i] != null && _set[i] != REMOVED) {\r
-               result.add(_set[i]);\r
-            }\r
-        }\r
-    \r
-       return result;\r
-       \r
-    }\r
-\r
-    final public <T extends BinaryQuery> ArrayList<T> values(final int r1) {\r
-\r
-       ArrayList<T> result = new ArrayList<T>();\r
-       \r
-        for (int i = _set.length; i-- > 0;) {\r
-            if(_set[i] != null && _set[i] != REMOVED) {\r
-               BinaryQuery<Procedure> e = _set[i];\r
-               if(e.r1() == r1)\r
-                       result.add((T)e);\r
-            }\r
-        }\r
-    \r
-       return result;\r
-       \r
-    }\r
-    \r
-} // THashMap\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.db.impl.query;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+
+
+
+/**
+ * An implementation of the Map interface which uses an open addressed
+ * hash table to store its contents.
+ *
+ * Created: Sun Nov  4 08:52:45 2001
+ *
+ * @author Eric D. Friedman
+ * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
+ */
+public class BinaryQueryHashMap<Procedure> extends BinaryQueryHash<Procedure> {
+
+    /**
+     * Creates a new <code>THashMap</code> instance with the default
+     * capacity and load factor.
+     */
+    public BinaryQueryHashMap() {
+        super();
+    }
+
+    /**
+     * initialize the value array of the map.
+     *
+     * @param initialCapacity an <code>int</code> value
+     * @return an <code>int</code> value
+     */
+    protected int setUp(int initialCapacity) {
+        int capacity;
+        
+        capacity = super.setUp(initialCapacity);
+//        _values = (Object[]) new Object[capacity];
+        return capacity;
+    }
+    
+    /**
+     * Inserts a key/value pair into the map.
+     *
+     * @param key an <code>Object</code> value
+     * @param value an <code>Object</code> value
+     * @return the previous value associated with <tt>key</tt>,
+     * or null if none was found.
+     */
+       public BinaryQuery<Procedure> put(final long id, final BinaryQuery<Procedure> value) {
+       BinaryQuery<Procedure> previous = null;
+        Object oldKey;
+        int index = insertionIndex(id);
+        boolean isNewMapping = true;
+        if (index < 0) {
+            index = -index -1;
+            previous = _set[index];
+            isNewMapping = false;
+            new Exception().printStackTrace();
+            throw new Error("Duplicate entry in BinaryQueryHashMap2 " + value);
+        }
+        oldKey = _set[index];
+        _set[index] = value;
+        if (isNewMapping) {
+            postInsertHook(oldKey == null);
+        }
+
+        return previous;
+        
+    }
+
+    /**
+     * rehashes the map to the new capacity.
+     *
+     * @param newCapacity an <code>int</code> value
+     */
+    @SuppressWarnings("unchecked")
+       protected void rehash(int newCapacity) {
+        
+        int oldCapacity = _set.length;
+        BinaryQuery oldKeys[] = _set;
+
+        _set = (BinaryQuery[])Array.newInstance(BinaryQuery.class, newCapacity);
+
+        for (int i = oldCapacity; i-- > 0;) {
+            if(oldKeys[i] != null && oldKeys[i] != REMOVED) {
+                BinaryQuery o = oldKeys[i];
+                int index = insertionIndex(o.id);
+                if (index < 0) {
+                    throwObjectContractViolation(_set[(-index -1)], o);
+                }
+                _set[index] = o;
+            }
+        }
+    }
+
+    /**
+     * retrieves the value for <tt>key</tt>
+     *
+     * @param key an <code>Object</code> value
+     * @return the value of <tt>key</tt> or null if no such mapping exists.
+     */
+    public BinaryQuery<Procedure> get(final long id) {
+        int index = index(id);
+        return index < 0 ? null : _set[index];
+    }
+
+    /**
+     * Deletes a key/value pair from the map.
+     *
+     * @param key an <code>Object</code> value
+     * @return an <code>Object</code> value
+     */
+    public Object remove(final long id) {
+        Object prev = null;
+        int index = index(id);
+        if (index >= 0) {
+            prev = _set[index];
+            removeAt(index);    // clear key,state; adjust size
+        }
+        return prev;
+    }
+    
+    final public void values(int level, CacheCollectionResult result) {
+
+        for (int i = _set.length; i-- > 0;) {
+               CacheEntryBase entry = _set[i];
+            if(entry != null && entry != REMOVED) {
+               if(entry.getLevel() <= level)
+                       result.add(entry);
+            }
+        }
+
+    }
+
+    final public ArrayList<CacheEntry> values() {
+
+       ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
+       
+        for (int i = _set.length; i-- > 0;) {
+            if(_set[i] != null && _set[i] != REMOVED) {
+               result.add(_set[i]);
+            }
+        }
+    
+       return result;
+       
+    }
+
+    final public <T extends BinaryQuery> ArrayList<T> values(final int r1) {
+
+       ArrayList<T> result = new ArrayList<T>();
+       
+        for (int i = _set.length; i-- > 0;) {
+            if(_set[i] != null && _set[i] != REMOVED) {
+               BinaryQuery<Procedure> e = _set[i];
+               if(e.r1() == r1)
+                       result.add((T)e);
+            }
+        }
+    
+       return result;
+       
+    }
+    
+} // THashMap