KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > IdentityHashSet


1 package com.sun.tools.xjc.runtime;
2
3 /**
4  * A set of {@link Object}s that uses the == (instead of equals)
5  * for the comparison.
6  *
7  * @author
8  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
9  */

10 final class IdentityHashSet {
11     /** The hash table data. */
12     private Object JavaDoc table[];
13
14     /** The total number of mappings in the hash table. */
15     private int count;
16
17     /**
18      * The table is rehashed when its size exceeds this threshold. (The
19      * value of this field is (int)(capacity * loadFactor).)
20      */

21     private int threshold;
22
23     /** The load factor for the hashtable. */
24     private static final float loadFactor = 0.3f;
25     private static final int initialCapacity = 191;
26
27
28     public IdentityHashSet() {
29         table = new Object JavaDoc[initialCapacity];
30         threshold = (int) (initialCapacity * loadFactor);
31     }
32
33     public boolean contains(Object JavaDoc key) {
34         Object JavaDoc tab[] = table;
35         int index = (System.identityHashCode(key) & 0x7FFFFFFF) % tab.length;
36
37         while (true) {
38             final Object JavaDoc e = tab[index];
39             if (e == null)
40                 return false;
41             if (e==key)
42                 return true;
43             index = (index + 1) % tab.length;
44         }
45     }
46
47     /**
48      * rehash.
49      *
50      * It is possible for one thread to call get method
51      * while another thread is performing rehash.
52      * Keep this in mind.
53      */

54     private void rehash() {
55         // create a new table first.
56
// meanwhile, other threads can safely access get method.
57
int oldCapacity = table.length;
58         Object JavaDoc oldMap[] = table;
59
60         int newCapacity = oldCapacity * 2 + 1;
61         Object JavaDoc newMap[] = new Object JavaDoc[newCapacity];
62
63         for (int i = oldCapacity; i-- > 0;)
64             if (oldMap[i] != null) {
65                 int index = (System.identityHashCode(oldMap[i]) & 0x7FFFFFFF) % newMap.length;
66                 while (newMap[index] != null)
67                     index = (index + 1) % newMap.length;
68                 newMap[index] = oldMap[i];
69             }
70
71         // threshold is not accessed by get method.
72
threshold = (int) (newCapacity * loadFactor);
73         // switch!
74
table = newMap;
75     }
76
77     public boolean add(Object JavaDoc newObj) {
78         if (count >= threshold)
79             rehash();
80
81         Object JavaDoc tab[] = table;
82         int index = (System.identityHashCode(newObj) & 0x7FFFFFFF) % tab.length;
83
84         Object JavaDoc existing;
85         
86         while ((existing=tab[index]) != null) {
87             if(existing==newObj) return false;
88             index = (index + 1) % tab.length;
89         }
90         tab[index] = newObj;
91
92         count++;
93         
94         return true;
95     }
96 }
97
Popular Tags