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.db.impl.query;
\r
14 import java.lang.reflect.Array;
\r
15 import java.util.ArrayList;
\r
20 * An implementation of the Map interface which uses an open addressed
\r
21 * hash table to store its contents.
\r
23 * Created: Sun Nov 4 08:52:45 2001
\r
25 * @author Eric D. Friedman
\r
26 * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
\r
28 public class UnaryQueryHashMap<Procedure> extends UnaryQueryHash<Procedure> {
\r
31 * Creates a new <code>THashMap</code> instance with the default
\r
32 * capacity and load factor.
\r
34 public UnaryQueryHashMap() {
\r
39 * initialize the value array of the map.
\r
41 * @param initialCapacity an <code>int</code> value
\r
42 * @return an <code>int</code> value
\r
44 protected int setUp(int initialCapacity) {
\r
47 capacity = super.setUp(initialCapacity);
\r
48 // _values = (Object[]) new Object[capacity];
\r
53 * Inserts a key/value pair into the map.
\r
55 * @param key an <code>Object</code> value
\r
56 * @param value an <code>Object</code> value
\r
57 * @return the previous value associated with <tt>key</tt>,
\r
58 * or null if none was found.
\r
60 @SuppressWarnings("unchecked")
\r
61 public UnaryQuery put(final int id, final UnaryQuery value) {
\r
62 UnaryQuery previous = null;
\r
64 int index = insertionIndex(id);
\r
65 boolean isNewMapping = true;
\r
68 previous = _set[index];
\r
69 isNewMapping = false;
\r
71 oldKey = _set[index];
\r
72 _set[index] = value;
\r
74 postInsertHook(oldKey == null);
\r
81 * rehashes the map to the new capacity.
\r
83 * @param newCapacity an <code>int</code> value
\r
85 protected void rehash(int newCapacity) {
\r
87 int oldCapacity = _set.length;
\r
88 UnaryQuery<Procedure> oldKeys[] = _set;
\r
90 UnaryQuery<Procedure> newKeys[] = (UnaryQuery<Procedure>[])Array.newInstance(UnaryQuery.class, newCapacity);
\r
92 for (int i = oldCapacity; i-- > 0;) {
\r
93 if(oldKeys[i] != null && oldKeys[i] != REMOVED) {
\r
94 UnaryQuery<Procedure> o = oldKeys[i];
\r
95 int index = insertionIndex2(o.id, newKeys);
\r
97 throwObjectContractViolation(newKeys[(-index -1)], o);
\r
108 * retrieves the value for <tt>key</tt>
\r
110 * @param key an <code>Object</code> value
\r
111 * @return the value of <tt>key</tt> or null if no such mapping exists.
\r
113 final public UnaryQuery<Procedure> get(final int id) {
\r
114 // int index = index(id);
\r
115 // return index < 0 ? null : _set[index];
\r
120 * Deletes a key/value pair from the map.
\r
122 * @param key an <code>Object</code> value
\r
123 * @return an <code>Object</code> value
\r
125 public Object remove(final int id) {
\r
126 Object prev = null;
\r
127 int index = index(id);
\r
129 prev = _set[index];
\r
130 removeAt(index); // clear key,state; adjust size
\r
135 final public void values(int level, CacheCollectionResult result) {
\r
137 for (int i = _set.length; i-- > 0;) {
\r
138 CacheEntryBase entry = _set[i];
\r
139 if(entry != null && entry != REMOVED) {
\r
140 if(entry.getLevel() <= level)
\r
147 final public ArrayList<CacheEntry> values() {
\r
149 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
\r
151 for (int i = _set.length; i-- > 0;) {
\r
152 if(_set[i] != null && _set[i] != REMOVED) {
\r
153 result.add(_set[i]);
\r