001: /**
002: *
003: */package org.drools.util;
004:
005: import org.drools.common.InternalFactHandle;
006: import org.drools.reteoo.FactHandleMemory;
007: import org.drools.reteoo.ReteTuple;
008:
009: public class FactHashTable extends AbstractHashTable implements
010: FactHandleMemory {
011: private static final long serialVersionUID = 400L;
012:
013: public FactHashTable() {
014: this (16, 0.75f);
015: }
016:
017: public FactHashTable(final int capacity, final float loadFactor) {
018: super (capacity, loadFactor);
019: }
020:
021: public Iterator iterator(final ReteTuple tuple) {
022: return iterator();
023: }
024:
025: public boolean add(final InternalFactHandle handle) {
026: return add(handle, true);
027: }
028:
029: public boolean add(final InternalFactHandle handle,
030: final boolean checkExists) {
031: final int hashCode = this .comparator.hashCodeOf(handle);
032: final int index = indexOf(hashCode, this .table.length);
033:
034: // scan the linked entries to see if it exists
035: if (checkExists) {
036: FactEntryImpl current = (FactEntryImpl) this .table[index];
037: while (current != null) {
038: if (hashCode == current.hashCode
039: && handle.getId() == current.handle.getId()) {
040: return false;
041: }
042: current = (FactHashTable.FactEntryImpl) current
043: .getNext();
044: }
045: }
046:
047: // We aren't checking the key exists, or it didn't find the key
048: final FactEntryImpl entry = new FactEntryImpl(handle, hashCode);
049: entry.next = this .table[index];
050: this .table[index] = entry;
051:
052: if (this .size++ >= this .threshold) {
053: resize(2 * this .table.length);
054: }
055: return true;
056: }
057:
058: public boolean contains(final InternalFactHandle handle) {
059: final int hashCode = this .comparator.hashCodeOf(handle);
060: final int index = indexOf(hashCode, this .table.length);
061:
062: FactEntryImpl current = (FactEntryImpl) this .table[index];
063: while (current != null) {
064: if (hashCode == current.hashCode
065: && handle.getId() == current.handle.getId()) {
066: return true;
067: }
068: current = (FactEntryImpl) current.getNext();
069: }
070: return false;
071: }
072:
073: public boolean remove(final InternalFactHandle handle) {
074: final int hashCode = this .comparator.hashCodeOf(handle);
075: final int index = indexOf(hashCode, this .table.length);
076:
077: FactEntryImpl previous = (FactEntryImpl) this .table[index];
078: FactEntryImpl current = previous;
079: while (current != null) {
080: final FactEntryImpl next = (FactEntryImpl) current
081: .getNext();
082: if (hashCode == current.hashCode
083: && handle.getId() == current.handle.getId()) {
084: if (previous == current) {
085: this .table[index] = next;
086: } else {
087: previous.setNext(next);
088: }
089: current.setNext(null);
090: this .size--;
091: return true;
092: }
093: previous = current;
094: current = next;
095: }
096: return false;
097: }
098:
099: public Entry getBucket(final Object object) {
100: final int hashCode = this .comparator.hashCodeOf(object);
101: final int index = indexOf(hashCode, this .table.length);
102:
103: return this .table[index];
104: }
105:
106: public boolean isIndexed() {
107: return false;
108: }
109: }
|