View Javadoc

1   
2   package org.paneris.util;
3   import java.util.Enumeration;
4   import java.util.Hashtable;
5   import java.util.Vector;
6    
7   /**
8    * This class provides extended facilities to a hashtable
9    * paticularly, it will return keys and values in the order they were added,
10   * allowing it to be used as a vector.
11   */
12  
13  public class ExtendedHash extends Hashtable {
14      
15    private static final long serialVersionUID = 1L;
16      UniqueVector keys;
17      JoinVector values;
18  
19  /**
20   * Normal constructor.
21   */
22   public ExtendedHash() {
23     super();
24     keys = new UniqueVector();
25     values = new JoinVector();
26   }
27  
28  /**
29  constructor with capacity and loadfactor
30  **/
31      public ExtendedHash(int initialCapacity, float loadFactor) {
32          super(initialCapacity, loadFactor);
33          keys = new UniqueVector();
34          values = new JoinVector();
35      }
36  
37  
38  /**
39  constructor with capacity
40  **/
41      public ExtendedHash(int initialCapacity) {
42          super(initialCapacity);
43          keys = new UniqueVector();
44          values = new JoinVector();
45      }
46  
47  
48  /**
49  constructor - make a hashtable out of 2 vectors: a keys and b values
50  **/
51      public ExtendedHash(Vector a, Vector b) {
52          super();
53          keys = new UniqueVector();
54          values = new JoinVector();
55          Enumeration enumB = b.elements();
56          for (Enumeration enumA = a.elements() ; enumA.hasMoreElements() ;) {
57              Object oA = enumA.nextElement();
58              if (enumB.hasMoreElements()) {
59                  Object oB = enumB.nextElement();
60                  put(oA, oB);
61              } else {
62                  put(oA, "");
63              }
64          }
65      }
66  
67  /**
68  zap this Extended hashtable
69  **/
70      public synchronized void clear() {
71          super.clear();
72          super.rehash();
73          keys.removeAllElements();
74          values.removeAllElements();
75          keys.trimToSize(); 
76          values.trimToSize(); 
77      }
78  
79  /**
80  return the elements as a vector in the order they were added
81  **/
82      public synchronized Enumeration elements() {
83          return values.elements();
84      }
85  
86  /**
87  return the keys as a vector in the order they were added
88  **/
89      public synchronized Enumeration keys() {
90          return keys.elements();
91      }
92  
93  /**
94  add an item to the ExtendedHash
95  **/
96      public synchronized Object put(Object key, Object value) {
97          if (keys.addUniqueElement(key)) { 
98              values.addElement(value); 
99          }
100         return super.put(key, value);
101     }
102     
103 /**
104 remove an item from the ExtendedHash
105 **/
106     public synchronized Object remove(Object key) {
107         values.removeElementAt(keys.indexOf(key));
108         keys.removeElement(key);
109         return super.remove(key);
110     }
111    
112     
113 /**
114 join the keys together with the specified deliminator
115 **/
116     public String joinKeys(String deliminator) {
117         return keys.join(deliminator);
118     }
119 
120 }
121