View Javadoc

1   //
2   // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.6-01/24/2006 06:15 PM(kohsuke)-fcs 
3   // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
4   // Any modifications to this file will be lost upon recompilation of the source schema. 
5   // Generated on: 2012.10.03 at 04:27:47 AM CEST 
6   //
7   
8   package org.jdtaus.mojo.resource.model.impl.runtime;
9   
10  /**
11   * A set of {@link Object}s that uses the == (instead of equals)
12   * for the comparison.
13   * 
14   * @author
15   *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
16   */
17  final class IdentityHashSet {
18      /** The hash table data. */
19      private Object table[];
20  
21      /** The total number of mappings in the hash table. */
22      private int count;
23  
24      /**
25       * The table is rehashed when its size exceeds this threshold.  (The
26       * value of this field is (int)(capacity * loadFactor).)
27       */
28      private int threshold;
29  
30      /** The load factor for the hashtable. */
31      private static final float loadFactor = 0.3f;
32      private static final int initialCapacity = 191;
33  
34  
35      public IdentityHashSet() {
36          table = new Object[initialCapacity];
37          threshold = (int) (initialCapacity * loadFactor);
38      }
39  
40      public boolean contains(Object key) {
41          Object tab[] = table;
42          int index = (System.identityHashCode(key) & 0x7FFFFFFF) % tab.length;
43  
44          while (true) {
45              final Object e = tab[index];
46              if (e == null)
47                  return false;
48              if (e==key)
49                  return true;
50              index = (index + 1) % tab.length;
51          }
52      }
53  
54      /**
55       * rehash.
56       * 
57       * It is possible for one thread to call get method
58       * while another thread is performing rehash.
59       * Keep this in mind.
60       */
61      private void rehash() {
62          // create a new table first.
63          // meanwhile, other threads can safely access get method.
64          int oldCapacity = table.length;
65          Object oldMap[] = table;
66  
67          int newCapacity = oldCapacity * 2 + 1;
68          Object newMap[] = new Object[newCapacity];
69  
70          for (int i = oldCapacity; i-- > 0;)
71              if (oldMap[i] != null) {
72                  int index = (System.identityHashCode(oldMap[i]) & 0x7FFFFFFF) % newMap.length;
73                  while (newMap[index] != null)
74                      index = (index + 1) % newMap.length;
75                  newMap[index] = oldMap[i];
76              }
77  
78          // threshold is not accessed by get method.
79          threshold = (int) (newCapacity * loadFactor);
80          // switch!
81          table = newMap;
82      }
83  
84      public boolean add(Object newObj) {
85          if (count >= threshold)
86              rehash();
87  
88          Object tab[] = table;
89          int index = (System.identityHashCode(newObj) & 0x7FFFFFFF) % tab.length;
90  
91          Object existing;
92          
93          while ((existing=tab[index]) != null) {
94              if(existing==newObj)    return false;
95              index = (index + 1) % tab.length;
96          }
97          tab[index] = newObj;
98  
99          count++;
100         
101         return true;
102     }
103 }