001: // The UMLet source code is distributed under the terms of the GPL; see license.txt
002: package com.umlet.control.io;
003:
004: import java.util.*;
005:
006: /// A Hashtable that uses ints as the keys.
007: // <P>
008: // Use just like java.util.Hashtable, except that the keys must be ints.
009: // This is much faster than creating a new Integer for each access.
010: // <P>
011: // <A HREF="/resources/classes/Acme/IntHashtable.java">Fetch the software.</A><BR>
012: // <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
013: // <P>
014: // @see java.util.Hashtable
015:
016: public class IntHashtable extends Dictionary implements Cloneable {
017: /// The hash table data.
018: private IntHashtableEntry table[];
019:
020: /// The total number of entries in the hash table.
021: private int count;
022:
023: /// Rehashes the table when count exceeds this threshold.
024: private int threshold;
025:
026: /// The load factor for the hashtable.
027: private float loadFactor;
028:
029: /// Constructs a new, empty hashtable with the specified initial
030: // capacity and the specified load factor.
031: // @param initialCapacity the initial number of buckets
032: // @param loadFactor a number between 0.0 and 1.0, it defines
033: // the threshold for rehashing the hashtable into
034: // a bigger one.
035: // @exception IllegalArgumentException If the initial capacity
036: // is less than or equal to zero.
037: // @exception IllegalArgumentException If the load factor is
038: // less than or equal to zero.
039: public IntHashtable(int initialCapacity, float loadFactor) {
040: if (initialCapacity <= 0 || loadFactor <= 0.0)
041: throw new IllegalArgumentException();
042: this .loadFactor = loadFactor;
043: table = new IntHashtableEntry[initialCapacity];
044: threshold = (int) (initialCapacity * loadFactor);
045: }
046:
047: /// Constructs a new, empty hashtable with the specified initial
048: // capacity.
049: // @param initialCapacity the initial number of buckets
050: public IntHashtable(int initialCapacity) {
051: this (initialCapacity, 0.75f);
052: }
053:
054: /// Constructs a new, empty hashtable. A default capacity and load factor
055: // is used. Note that the hashtable will automatically grow when it gets
056: // full.
057: public IntHashtable() {
058: this (101, 0.75f);
059: }
060:
061: /// Returns the number of elements contained in the hashtable.
062: public int size() {
063: return count;
064: }
065:
066: /// Returns true if the hashtable contains no elements.
067: public boolean isEmpty() {
068: return count == 0;
069: }
070:
071: /// Returns an enumeration of the hashtable's keys.
072: // @see IntHashtable#elements
073: public synchronized Enumeration keys() {
074: return new IntHashtableEnumerator(table, true);
075: }
076:
077: /// Returns an enumeration of the elements. Use the Enumeration methods
078: // on the returned object to fetch the elements sequentially.
079: // @see IntHashtable#keys
080: public synchronized Enumeration elements() {
081: return new IntHashtableEnumerator(table, false);
082: }
083:
084: /// Returns true if the specified object is an element of the hashtable.
085: // This operation is more expensive than the containsKey() method.
086: // @param value the value that we are looking for
087: // @exception NullPointerException If the value being searched
088: // for is equal to null.
089: // @see IntHashtable#containsKey
090: public synchronized boolean contains(Object value) {
091: if (value == null)
092: throw new NullPointerException();
093: IntHashtableEntry tab[] = table;
094: for (int i = tab.length; i-- > 0;) {
095: for (IntHashtableEntry e = tab[i]; e != null; e = e.next) {
096: if (e.value.equals(value))
097: return true;
098: }
099: }
100: return false;
101: }
102:
103: /// Returns true if the collection contains an element for the key.
104: // @param key the key that we are looking for
105: // @see IntHashtable#contains
106: public synchronized boolean containsKey(int key) {
107: IntHashtableEntry tab[] = table;
108: int hash = key;
109: int index = (hash & 0x7FFFFFFF) % tab.length;
110: for (IntHashtableEntry e = tab[index]; e != null; e = e.next) {
111: if (e.hash == hash && e.key == key)
112: return true;
113: }
114: return false;
115: }
116:
117: /// Gets the object associated with the specified key in the
118: // hashtable.
119: // @param key the specified key
120: // @returns the element for the key or null if the key
121: // is not defined in the hash table.
122: // @see IntHashtable#put
123: public synchronized Object get(int key) {
124: IntHashtableEntry tab[] = table;
125: int hash = key;
126: int index = (hash & 0x7FFFFFFF) % tab.length;
127: for (IntHashtableEntry e = tab[index]; e != null; e = e.next) {
128: if (e.hash == hash && e.key == key)
129: return e.value;
130: }
131: return null;
132: }
133:
134: /// A get method that takes an Object, for compatibility with
135: // java.util.Dictionary. The Object must be an Integer.
136: public Object get(Object okey) {
137: if (!(okey instanceof Integer))
138: throw new InternalError("key is not an Integer");
139: Integer ikey = (Integer) okey;
140: int key = ikey.intValue();
141: return get(key);
142: }
143:
144: /// Rehashes the content of the table into a bigger table.
145: // This method is called automatically when the hashtable's
146: // size exceeds the threshold.
147: protected void rehash() {
148: int oldCapacity = table.length;
149: IntHashtableEntry oldTable[] = table;
150:
151: int newCapacity = oldCapacity * 2 + 1;
152: IntHashtableEntry newTable[] = new IntHashtableEntry[newCapacity];
153:
154: threshold = (int) (newCapacity * loadFactor);
155: table = newTable;
156:
157: for (int i = oldCapacity; i-- > 0;) {
158: for (IntHashtableEntry old = oldTable[i]; old != null;) {
159: IntHashtableEntry e = old;
160: old = old.next;
161:
162: int index = (e.hash & 0x7FFFFFFF) % newCapacity;
163: e.next = newTable[index];
164: newTable[index] = e;
165: }
166: }
167: }
168:
169: /// Puts the specified element into the hashtable, using the specified
170: // key. The element may be retrieved by doing a get() with the same key.
171: // The key and the element cannot be null.
172: // @param key the specified key in the hashtable
173: // @param value the specified element
174: // @exception NullPointerException If the value of the element
175: // is equal to null.
176: // @see IntHashtable#get
177: // @return the old value of the key, or null if it did not have one.
178: public synchronized Object put(int key, Object value) {
179: // Make sure the value is not null.
180: if (value == null)
181: throw new NullPointerException();
182:
183: // Makes sure the key is not already in the hashtable.
184: IntHashtableEntry tab[] = table;
185: int hash = key;
186: int index = (hash & 0x7FFFFFFF) % tab.length;
187: for (IntHashtableEntry e = tab[index]; e != null; e = e.next) {
188: if (e.hash == hash && e.key == key) {
189: Object old = e.value;
190: e.value = value;
191: return old;
192: }
193: }
194:
195: if (count >= threshold) {
196: // Rehash the table if the threshold is exceeded.
197: rehash();
198: return put(key, value);
199: }
200:
201: // Creates the new entry.
202: IntHashtableEntry e = new IntHashtableEntry();
203: e.hash = hash;
204: e.key = key;
205: e.value = value;
206: e.next = tab[index];
207: tab[index] = e;
208: ++count;
209: return null;
210: }
211:
212: /// A put method that takes an Object, for compatibility with
213: // java.util.Dictionary. The Object must be an Integer.
214: public Object put(Object okey, Object value) {
215: if (!(okey instanceof Integer))
216: throw new InternalError("key is not an Integer");
217: Integer ikey = (Integer) okey;
218: int key = ikey.intValue();
219: return put(key, value);
220: }
221:
222: /// Removes the element corresponding to the key. Does nothing if the
223: // key is not present.
224: // @param key the key that needs to be removed
225: // @return the value of key, or null if the key was not found.
226: public synchronized Object remove(int key) {
227: IntHashtableEntry tab[] = table;
228: int hash = key;
229: int index = (hash & 0x7FFFFFFF) % tab.length;
230: for (IntHashtableEntry e = tab[index], prev = null; e != null; prev = e, e = e.next) {
231: if (e.hash == hash && e.key == key) {
232: if (prev != null)
233: prev.next = e.next;
234: else
235: tab[index] = e.next;
236: --count;
237: return e.value;
238: }
239: }
240: return null;
241: }
242:
243: /// A remove method that takes an Object, for compatibility with
244: // java.util.Dictionary. The Object must be an Integer.
245: public Object remove(Object okey) {
246: if (!(okey instanceof Integer))
247: throw new InternalError("key is not an Integer");
248: Integer ikey = (Integer) okey;
249: int key = ikey.intValue();
250: return remove(key);
251: }
252:
253: /// Clears the hash table so that it has no more elements in it.
254: public synchronized void clear() {
255: IntHashtableEntry tab[] = table;
256: for (int index = tab.length; --index >= 0;)
257: tab[index] = null;
258: count = 0;
259: }
260:
261: /// Creates a clone of the hashtable. A shallow copy is made,
262: // the keys and elements themselves are NOT cloned. This is a
263: // relatively expensive operation.
264: public synchronized Object clone() {
265: try {
266: IntHashtable t = (IntHashtable) super .clone();
267: t.table = new IntHashtableEntry[table.length];
268: for (int i = table.length; i-- > 0;)
269: t.table[i] = (table[i] != null) ? (IntHashtableEntry) table[i]
270: .clone()
271: : null;
272: return t;
273: } catch (CloneNotSupportedException e) {
274: // This shouldn't happen, since we are Cloneable.
275: throw new InternalError();
276: }
277: }
278:
279: /// Converts to a rather lengthy String.
280: public synchronized String toString() {
281: int max = size() - 1;
282: StringBuffer buf = new StringBuffer();
283: Enumeration k = keys();
284: Enumeration e = elements();
285: buf.append("{");
286:
287: for (int i = 0; i <= max; ++i) {
288: String s1 = k.nextElement().toString();
289: String s2 = e.nextElement().toString();
290: buf.append(s1 + "=" + s2);
291: if (i < max)
292: buf.append(", ");
293: }
294: buf.append("}");
295: return buf.toString();
296: }
297: }
298:
299: class IntHashtableEntry {
300: int hash;
301: int key;
302: Object value;
303: IntHashtableEntry next;
304:
305: protected Object clone() {
306: IntHashtableEntry entry = new IntHashtableEntry();
307: entry.hash = hash;
308: entry.key = key;
309: entry.value = value;
310: entry.next = (next != null) ? (IntHashtableEntry) next.clone()
311: : null;
312: return entry;
313: }
314: }
315:
316: class IntHashtableEnumerator implements Enumeration {
317: boolean keys;
318: int index;
319: IntHashtableEntry table[];
320: IntHashtableEntry entry;
321:
322: IntHashtableEnumerator(IntHashtableEntry table[], boolean keys) {
323: this .table = table;
324: this .keys = keys;
325: this .index = table.length;
326: }
327:
328: public boolean hasMoreElements() {
329: if (entry != null)
330: return true;
331: while (index-- > 0)
332: if ((entry = table[index]) != null)
333: return true;
334: return false;
335: }
336:
337: public Object nextElement() {
338: if (entry == null)
339: while ((index-- > 0) && ((entry = table[index]) == null))
340: ;
341: if (entry != null) {
342: IntHashtableEntry e = entry;
343: entry = e.next;
344: return keys ? new Integer(e.key) : e.value;
345: }
346: throw new NoSuchElementException("IntHashtableEnumerator");
347: }
348: }
|