1 4 5 9 10 package org.openlaszlo.utils; 11 12 import java.util.Collection ; 13 import java.util.Hashtable ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 21 public class MultiMap 22 implements Map 23 { 24 25 Hashtable mTable = new Hashtable (); 26 27 31 public Set keySet() 32 { 33 return mTable.keySet(); 34 } 35 36 41 public Object get(Object key) 42 { 43 return mTable.get(key); 44 } 45 46 51 public Object put(Object key, Object val) 52 { 53 Set valSet = (Set )mTable.get(key); 54 if (valSet == null) { 55 valSet = new HashSet (); 56 mTable.put(key, valSet); 57 } 58 valSet.add(val); 59 return valSet; 60 } 61 62 67 public Object remove(Object key, Object val) 68 { 69 Object ret = null; 70 Set valSet = (Set )mTable.get(key); 71 if (valSet != null) { 72 if (valSet.remove(val)) 73 ret = val; 74 if (valSet.isEmpty()) 75 mTable.remove(key); 76 } 77 return ret; 78 } 79 80 83 public Object remove(Object key) 84 { 85 return mTable.remove(key); 86 } 87 88 94 public Collection values() 95 { 96 return mTable.values(); 97 } 98 99 100 103 public int size() 104 { 105 return mTable.size(); 106 } 107 108 110 public boolean isEmpty() 111 { 112 return mTable.isEmpty(); 113 } 114 115 119 public boolean containsKey(Object key) 120 { 121 return mTable.containsKey(key); 122 } 123 124 133 public boolean containsValue(Object value) 134 { 135 Collection c = mTable.values(); 136 Iterator iter = c.iterator(); 137 while (iter.hasNext()) { 138 Set set = (Set )iter.next(); 139 if (set.contains(value)) 140 return true; 141 } 142 return false; 143 } 144 145 149 public Set removeValue(Object value) 150 { 151 Set keySet = new HashSet (); 152 Iterator iter = ((Set )mTable.entrySet()).iterator(); 153 while (iter.hasNext()) { 154 Map.Entry entry = (Map.Entry )iter.next(); 155 Set set = (Set )entry.getValue(); 156 if (set.contains(value)) { 157 keySet.add(entry.getKey()); 158 set.remove(value); 159 if (set.isEmpty()) 161 iter.remove(); 162 } 163 } 164 return (!keySet.isEmpty()?keySet:null); 165 } 166 167 168 169 public void putAll(Map m) 170 { 171 throw new UnsupportedOperationException (); 172 } 173 174 175 public void clear() 176 { 177 throw new UnsupportedOperationException (); 178 } 179 180 189 public Set entrySet() 190 { 191 return mTable.entrySet(); 192 } 193 194 } 195 | Popular Tags |