1 package gov.nasa.jpf.util; 20 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 import java.util.TreeSet ; 25 26 27 36 public class HashPool { 37 private static final int DELTA = 10; 38 private Hashtable pool; 39 private int size; 40 private int length; 41 private Object [] objects; 42 43 public HashPool () { 44 pool = new Hashtable (); 45 objects = new Object [length = DELTA]; 46 objects[0] = null; 47 size = 1; 48 } 49 50 public PoolEntry getEntry (Object o) { 51 if (o == null) { 52 return null; 53 } 54 55 PoolEntry e = (PoolEntry) pool.get(o); 56 57 if (e != null) { 58 return e; 59 } 60 61 return put(o); 62 } 63 64 public int getIndex (Object o) { 65 if (o == null) { 66 return -1; 67 } 68 69 PoolEntry e = (PoolEntry) pool.get(o); 70 71 if (e != null) { 72 return e.idx; 73 } 74 75 return put(o).idx; 76 } 77 78 public Object getObject (int idx) { 79 return objects[idx]; 80 } 81 82 public Object get (Object o) { 83 if (o == null) { 84 return null; 85 } 86 87 PoolEntry e = (PoolEntry) pool.get(o); 88 89 if (e != null) { 90 return e.obj; 91 } 92 93 return put(o).obj; 94 } 95 96 public synchronized void print () { 97 System.out.println("{"); 98 99 Set s = new TreeSet (pool.values()); 100 101 for (Iterator i = s.iterator(); i.hasNext();) { 102 Object entry = i.next(); 103 System.out.println("\t" + entry); 104 } 105 106 System.out.println("}"); 107 } 108 109 public synchronized PoolEntry put (Object o) { 110 PoolEntry e = (PoolEntry) pool.get(o); 111 112 if (e != null) { 113 return e; 114 } 115 116 if (length < (size + 1)) { 117 Object [] newObjects = new Object [length + DELTA]; 118 System.arraycopy(objects, 0, newObjects, 0, length); 119 objects = newObjects; 120 length += DELTA; 121 } 122 123 objects[size] = o; 124 pool.put(o, e = new PoolEntry(o, size++)); 125 126 return e; 127 } 128 129 public synchronized int size () { 130 return size; 131 } 132 133 136 public class PoolEntry implements Comparable { 137 private Object obj; 138 private int idx; 139 140 public PoolEntry (Object o, int i) { 141 obj = o; 142 idx = i; 143 } 144 145 public int getIndex () { 146 return idx; 147 } 148 149 public Object getObject () { 150 return obj; 151 } 152 153 public int compareTo (Object o) { 154 PoolEntry other = (PoolEntry) o; 155 156 return (idx - other.idx); 157 } 158 159 public boolean equals (Object obj) { 160 PoolEntry other = (PoolEntry) obj; 161 162 return (other.idx == idx); 163 } 164 165 public int hashCode () { 166 return idx; 167 } 168 169 public String toString () { 170 return idx + " => " + obj; 171 } 172 } 173 } | Popular Tags |