00001 package gov.nasa.arc.ase.util; 00002 00003 import java.util.*; 00004 00005 public class HashPool { 00006 public class PoolEntry { 00007 private PoolObject obj; 00008 private int idx; 00009 00010 public PoolEntry(PoolObject o, int i) { 00011 obj = o; 00012 idx = i; 00013 } 00014 00015 public PoolObject getObject() { 00016 return obj; 00017 } 00018 00019 public int getIndex() { 00020 return idx; 00021 } 00022 00023 public int hashCode() { 00024 return idx; 00025 } 00026 00027 public boolean equals(Object obj) { 00028 PoolEntry other = (PoolEntry)obj; 00029 return (other.idx == idx); 00030 } 00031 } 00032 00033 private Hashtable pool; 00034 private int size = 0; 00035 private PoolObject[] objects; 00036 00037 public HashPool() { 00038 pool = new Hashtable(); 00039 objects = new PoolObject[1]; 00040 objects[0] = null; 00041 } 00042 public PoolObject get(PoolObject o) { 00043 if(o == null) return null; 00044 PoolEntry e = (PoolEntry)pool.get(o); 00045 if (e != null) 00046 return e.obj; 00047 else { 00048 pool.put(o,new PoolEntry(o,size+1)); 00049 if(objects.length < size+2) { 00050 PoolObject[] old = objects; 00051 objects = new PoolObject[size+10]; 00052 System.arraycopy(old, 0, objects, 0, old.length); 00053 } 00054 objects[size+1] = o; 00055 } 00056 size++; 00057 00058 return o; 00059 } 00060 public PoolEntry getEntry(PoolObject o) { 00061 if(o == null) return null; 00062 PoolEntry e = (PoolEntry)pool.get(o); 00063 if (e == null) { 00064 pool.put(o,e = new PoolEntry(o,size+1)); 00065 if(objects.length < size+2) { 00066 PoolObject[] old = objects; 00067 objects = new PoolObject[size+10]; 00068 System.arraycopy(old, 0, objects, 0, old.length); 00069 } 00070 objects[size+1] = o; 00071 } else 00072 return e; 00073 size++; 00074 00075 return e; 00076 } 00077 public int getIndex(PoolObject o) { 00078 if(o == null) return -1; 00079 PoolEntry e = (PoolEntry)pool.get(o); 00080 if (e != null) 00081 return e.idx; 00082 else { 00083 pool.put(o,new PoolEntry(o,size+1)); 00084 if(objects.length < size+2) { 00085 PoolObject[] old = objects; 00086 objects = new PoolObject[size+10]; 00087 System.arraycopy(old, 0, objects, 0, old.length); 00088 } 00089 objects[size+1] = o; 00090 } 00091 size++; 00092 00093 return size; 00094 } 00095 public PoolObject getObject(int idx) { 00096 return objects[idx]; 00097 } 00098 public int size() { 00099 return size; 00100 } 00101 }