Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

HashPool.java

00001 package gov.nasa.arc.ase.util;
00002 
00003 import java.util.Hashtable;
00004 //#ifdef JDK11
00005 
00006 
00007 
00008 //#else JDK11
00009 import java.util.Set;
00010 import java.util.TreeSet;
00011 import java.util.Iterator;
00012 //#endif JDK11
00013 
00014 public class HashPool {
00015   public class PoolEntry implements Comparable {
00016     private Object obj;
00017     private int idx;
00018 
00019     public PoolEntry(Object o, int i) {
00020       obj = o;
00021       idx = i;
00022     }
00023 
00024     public Object getObject() {
00025       return obj;
00026     }
00027 
00028     public int getIndex() {
00029       return idx;
00030     }
00031 
00032     public int hashCode() {
00033       return idx;
00034     }
00035 
00036     public boolean equals(Object obj) {
00037       PoolEntry other = (PoolEntry)obj;
00038       return (other.idx == idx);
00039     }
00040 
00041     public String toString() {
00042       return idx + " => " + obj;
00043     }
00044 
00045     public int compareTo(Object o) {
00046       PoolEntry other = (PoolEntry)o;
00047       return (idx - other.idx);
00048     }
00049   }
00050 
00051   private Hashtable pool;
00052   private int size;
00053   private int length;
00054   private Object[] objects;
00055   private static final int DELTA = 10;
00056 
00057   public HashPool() {
00058     pool  = new Hashtable();
00059     objects = new Object[length = DELTA];
00060     objects[0] = null;
00061     size = 1;
00062   }  
00063   public Object get(Object o) {
00064     if(o == null) return null;
00065     PoolEntry e = (PoolEntry)pool.get(o);
00066     if (e != null) return e.obj;
00067 
00068     return put(o).obj;
00069   }  
00070   public PoolEntry getEntry(Object o) {
00071     if(o == null) return null;
00072     PoolEntry e = (PoolEntry)pool.get(o);
00073     if (e != null) return e;
00074 
00075     return put(o);
00076   }  
00077   public int getIndex(Object o) {
00078     if(o == null) return -1;
00079     PoolEntry e = (PoolEntry)pool.get(o);
00080     if (e != null) return e.idx;
00081 
00082     return put(o).idx;
00083   }  
00084   public Object getObject(int idx) {
00085     return objects[idx];
00086   }  
00087   public synchronized void print() {
00088     System.out.println("{");
00089 
00090 //#ifdef JDK11
00091 
00092 //#else JDK11
00093     Set s = new TreeSet(pool.values());
00094 //#endif JDK11
00095 
00096     for(Iterator i = s.iterator(); i.hasNext(); ) {
00097       Object entry = i.next();
00098       System.out.println("\t" + entry);
00099     }
00100     System.out.println("}");
00101   }  
00102   public synchronized PoolEntry put(Object o) {
00103     PoolEntry e = (PoolEntry)pool.get(o);
00104     if(e != null) return e;
00105 
00106     if(length < size+1) {
00107       Object[] newObjects = new Object[length+DELTA];
00108       System.arraycopy(objects, 0, newObjects, 0, length);
00109       objects = newObjects;
00110       length += DELTA;
00111     }
00112     objects[size] = o;
00113     pool.put(o, e = new PoolEntry(o, size++));
00114 
00115     return e;
00116   }  
00117   public synchronized int size() {
00118     return size;
00119   }  
00120 }

Generated at Thu Feb 7 06:46:09 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001