1 package fri.util.collections; 2 3 import java.util.*; 4 import java.io.*; 5 6 14 15 public class AggregatingHashtable extends Hashtable 16 { 17 public AggregatingHashtable() { 18 super(); 19 } 20 21 public AggregatingHashtable(int initialCapacity) { 22 super(initialCapacity); 23 } 24 25 29 public Object put(Object key, Object value) { 30 List list = (List) super.get(key); 31 Object ret = null; 32 33 if (list == null) { 34 list = createAggregationList(); 35 super.put(key, list); 36 } 37 else { 38 if (shouldAdd(list, value) == false) 39 return list; 40 41 ret = list; 42 } 43 44 list.add(value); 45 return ret; 46 } 47 48 49 protected boolean shouldAdd(List list, Object value) { 50 return true; 51 } 52 53 54 protected List createAggregationList() { 55 return new ArrayList(); 56 } 57 58 59 public void replace(Object key, List newList) { 60 super.put(key, newList); 61 } 62 63 64 65 private void readObject(ObjectInputStream s) 67 throws IOException, ClassNotFoundException 68 { 69 s.defaultReadObject(); 70 71 for (Iterator it = entrySet().iterator(); it.hasNext(); ) { 72 Map.Entry entry = (Map.Entry) it.next(); 73 List list = (List) entry.getValue(); 74 Object o = list.size() == 1 ? list.get(0) : null; if (o instanceof List) { super.put(entry.getKey(), o); 77 } 78 } 79 } 80 81 } 82 | Popular Tags |