]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import java.lang.reflect.Array;
15 import java.util.ArrayList;
16
17
18
19 /**
20  * An implementation of the Map interface which uses an open addressed
21  * hash table to store its contents.
22  *
23  * Created: Sun Nov  4 08:52:45 2001
24  *
25  * @author Eric D. Friedman
26  * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
27  */
28 public class BinaryQueryHashMap<Procedure> extends BinaryQueryHash<Procedure> {
29
30     /**
31      * Creates a new <code>THashMap</code> instance with the default
32      * capacity and load factor.
33      */
34     public BinaryQueryHashMap() {
35         super();
36     }
37
38     /**
39      * initialize the value array of the map.
40      *
41      * @param initialCapacity an <code>int</code> value
42      * @return an <code>int</code> value
43      */
44     protected int setUp(int initialCapacity) {
45         int capacity;
46         
47         capacity = super.setUp(initialCapacity);
48 //        _values = (Object[]) new Object[capacity];
49         return capacity;
50     }
51     
52     /**
53      * Inserts a key/value pair into the map.
54      *
55      * @param key an <code>Object</code> value
56      * @param value an <code>Object</code> value
57      * @return the previous value associated with <tt>key</tt>,
58      * or null if none was found.
59      */
60         public BinaryQuery<Procedure> put(final long id, final BinaryQuery<Procedure> value) {
61         BinaryQuery<Procedure> previous = null;
62         Object oldKey;
63         int index = insertionIndex(id);
64         boolean isNewMapping = true;
65         if (index < 0) {
66             index = -index -1;
67             previous = _set[index];
68             isNewMapping = false;
69             new Exception().printStackTrace();
70             throw new Error("Duplicate entry in BinaryQueryHashMap2 " + value);
71         }
72         oldKey = _set[index];
73         _set[index] = value;
74         if (isNewMapping) {
75             postInsertHook(oldKey == null);
76         }
77
78         return previous;
79         
80     }
81
82     /**
83      * rehashes the map to the new capacity.
84      *
85      * @param newCapacity an <code>int</code> value
86      */
87     @SuppressWarnings("unchecked")
88         protected void rehash(int newCapacity) {
89         
90         int oldCapacity = _set.length;
91         BinaryQuery oldKeys[] = _set;
92
93         _set = (BinaryQuery[])Array.newInstance(BinaryQuery.class, newCapacity);
94
95         for (int i = oldCapacity; i-- > 0;) {
96             if(oldKeys[i] != null && oldKeys[i] != REMOVED) {
97                 BinaryQuery o = oldKeys[i];
98                 int index = insertionIndex(o.id);
99                 if (index < 0) {
100                     throwObjectContractViolation(_set[(-index -1)], o);
101                 }
102                 _set[index] = o;
103             }
104         }
105     }
106
107     /**
108      * retrieves the value for <tt>key</tt>
109      *
110      * @param key an <code>Object</code> value
111      * @return the value of <tt>key</tt> or null if no such mapping exists.
112      */
113     public BinaryQuery<Procedure> get(final long id) {
114         int index = index(id);
115         return index < 0 ? null : _set[index];
116     }
117
118     /**
119      * Deletes a key/value pair from the map.
120      *
121      * @param key an <code>Object</code> value
122      * @return an <code>Object</code> value
123      */
124     public Object remove(final long id) {
125         Object prev = null;
126         int index = index(id);
127         if (index >= 0) {
128             prev = _set[index];
129             removeAt(index);    // clear key,state; adjust size
130         }
131         return prev;
132     }
133     
134     final public void values(int level, CacheCollectionResult result) {
135
136         for (int i = _set.length; i-- > 0;) {
137                 CacheEntryBase entry = _set[i];
138             if(entry != null && entry != REMOVED) {
139                 if(entry.getLevel() <= level)
140                         result.add(entry);
141             }
142         }
143
144     }
145
146     final public ArrayList<CacheEntry> values() {
147
148         ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
149         
150         for (int i = _set.length; i-- > 0;) {
151             if(_set[i] != null && _set[i] != REMOVED) {
152                 result.add(_set[i]);
153             }
154         }
155     
156         return result;
157         
158     }
159
160     final public <T extends BinaryQuery> ArrayList<T> values(final int r1) {
161
162         ArrayList<T> result = new ArrayList<T>();
163         
164         for (int i = _set.length; i-- > 0;) {
165             if(_set[i] != null && _set[i] != REMOVED) {
166                 BinaryQuery<Procedure> e = _set[i];
167                 if(e.r1() == r1)
168                         result.add((T)e);
169             }
170         }
171     
172         return result;
173         
174     }
175     
176 } // THashMap