]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryHashMap.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / UnaryQueryHashMap.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 UnaryQueryHashMap<Procedure> extends UnaryQueryHash<Procedure> {
29
30     /**
31      * Creates a new <code>THashMap</code> instance with the default
32      * capacity and load factor.
33      */
34     public UnaryQueryHashMap() {
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     @SuppressWarnings("unchecked")
61         public UnaryQuery put(final int id, final UnaryQuery value) {
62         UnaryQuery previous = null;
63         Object oldKey;
64         int index = insertionIndex(id);
65         boolean isNewMapping = true;
66         if (index < 0) {
67             index = -index -1;
68             previous = _set[index];
69             isNewMapping = false;
70         }
71         oldKey = _set[index];
72         _set[index] = value;
73         if (isNewMapping) {
74             postInsertHook(oldKey == null);
75         }
76
77         return previous;
78     }
79
80     /**
81      * rehashes the map to the new capacity.
82      *
83      * @param newCapacity an <code>int</code> value
84      */
85         protected void rehash(int newCapacity) {
86         
87         int oldCapacity = _set.length;
88         UnaryQuery<Procedure> oldKeys[] = _set;
89
90         UnaryQuery<Procedure> newKeys[]  = (UnaryQuery<Procedure>[])Array.newInstance(UnaryQuery.class, newCapacity);
91
92         for (int i = oldCapacity; i-- > 0;) {
93             if(oldKeys[i] != null && oldKeys[i] != REMOVED) {
94                 UnaryQuery<Procedure> o = oldKeys[i];
95                 int index = insertionIndex2(o.id, newKeys);
96                 if (index < 0) {
97                     throwObjectContractViolation(newKeys[(-index -1)], o);
98                 }
99                 newKeys[index] = o;
100             }
101         }
102         
103         _set = newKeys;
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     final public UnaryQuery<Procedure> get(final int id) {
114 //        int index = index(id);
115 //        return index < 0 ? null : _set[index];
116         return index2(id);
117     }
118
119     /**
120      * Deletes a key/value pair from the map.
121      *
122      * @param key an <code>Object</code> value
123      * @return an <code>Object</code> value
124      */
125     public Object remove(final int id) {
126         Object prev = null;
127         int index = index(id);
128         if (index >= 0) {
129             prev = _set[index];
130             removeAt(index);    // clear key,state; adjust size
131         }
132         return prev;
133     }
134
135     final public void values(int level, CacheCollectionResult result) {
136
137         for (int i = _set.length; i-- > 0;) {
138                 CacheEntryBase entry = _set[i];
139             if(entry != null && entry != REMOVED) {
140                 if(entry.getLevel() <= level)
141                         result.add(entry);
142             }
143         }
144
145     }
146     
147     final public ArrayList<CacheEntry> values() {
148
149         ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
150         
151         for (int i = _set.length; i-- > 0;) {
152             if(_set[i] != null && _set[i] != REMOVED) {
153                 result.add(_set[i]);
154             }
155         }
156     
157         return result;
158         
159     }
160
161 } // THashMap