/******************************************************************************* * 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 extends BinaryQueryHash { /** * Creates a new THashMap instance with the default * capacity and load factor. */ public BinaryQueryHashMap() { super(); } /** * initialize the value array of the map. * * @param initialCapacity an int value * @return an int 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 Object value * @param value an Object value * @return the previous value associated with key, * or null if none was found. */ public BinaryQuery put(final long id, final BinaryQuery value) { BinaryQuery 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 int 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 key * * @param key an Object value * @return the value of key or null if no such mapping exists. */ public BinaryQuery 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 Object value * @return an Object 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 values() { ArrayList result = new ArrayList(); for (int i = _set.length; i-- > 0;) { if(_set[i] != null && _set[i] != REMOVED) { result.add(_set[i]); } } return result; } final public ArrayList values(final int r1) { ArrayList result = new ArrayList(); for (int i = _set.length; i-- > 0;) { if(_set[i] != null && _set[i] != REMOVED) { BinaryQuery e = _set[i]; if(e.r1() == r1) result.add((T)e); } } return result; } } // THashMap