1 package org.apache.turbine.util; 2 3 18 19 import java.util.Collection ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 50 public class SequencedHashtable extends Hashtable 51 { 52 55 private static final int UNKNOWN_INDEX = -1; 56 57 61 private LinkedList keySequence; 62 63 66 public SequencedHashtable() 67 { 68 keySequence = new LinkedList (); 69 } 70 71 76 public SequencedHashtable(int size) 77 { 78 super(size); 79 keySequence = new LinkedList (); 80 } 81 82 85 public synchronized void clear() 86 { 87 super.clear(); 88 keySequence.clear(); 89 } 90 91 98 public synchronized Object clone() 99 { 100 SequencedHashtable seqHash = (SequencedHashtable) super.clone(); 101 seqHash.keySequence = (LinkedList ) keySequence.clone(); 102 return seqHash; 103 } 104 105 108 public Object get(int index) 109 { 110 return keySequence.get(index); 111 } 112 113 116 public Object getValue(int index) 117 { 118 return get(get(index)); 119 } 120 121 124 public int indexOf(Object key) 125 { 126 return keySequence.indexOf(key); 127 } 128 129 132 public Iterator iterator() 133 { 134 return keySequence.iterator(); 135 } 136 137 140 public int lastIndexOf(Object key) 141 { 142 return keySequence.lastIndexOf(key); 143 } 144 145 162 public List sequence() 163 { 164 return keySequence; 165 } 166 167 176 public synchronized Object put(Object key, Object value) 177 { 178 Object prevValue = super.put(key, value); 179 freshenSequence(key, prevValue); 180 return prevValue; 181 } 182 183 191 protected void freshenSequence(Object key, Object value) 192 { 193 if (value != null) 194 { 195 keySequence.remove(key); 197 } 198 keySequence.add(key); 199 } 200 201 206 public synchronized void putAll(Map t) 207 { 208 Set set = t.entrySet(); 209 for (Iterator iter = set.iterator(); iter.hasNext();) 210 { 211 Map.Entry e = (Map.Entry ) iter.next(); 212 put(e.getKey(), e.getValue()); 213 } 214 } 215 216 223 public Object remove(int index) 224 { 225 return remove(index, null); 226 } 227 228 235 public Object remove(Object key) 236 { 237 return remove(UNKNOWN_INDEX, key); 238 } 239 240 249 private final synchronized Object remove(int index, Object key) 250 { 251 if (index == UNKNOWN_INDEX) index = indexOf(key); 252 if (key == null) key = get(index); 253 if (index != UNKNOWN_INDEX) keySequence.remove(index); 254 return super.remove(key); 255 } 256 257 260 public Collection values() 261 { 262 return keySequence; 263 } 264 } 265 | Popular Tags |