1 16 package org.mortbay.util; 17 18 import java.util.Arrays ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 25 35 public class MultiMap extends HashMap implements Cloneable 36 { 37 38 40 public MultiMap() 41 {} 42 43 44 47 public MultiMap(int size) 48 { 49 super(size); 50 } 51 52 53 56 public MultiMap(Map map) 57 { 58 super((map.size()*3)/2); 59 putAll(map); 60 } 61 62 63 68 public List getValues(Object name) 69 { 70 return LazyList.getList(super.get(name),true); 71 } 72 73 74 81 public Object getValue(Object name,int i) 82 { 83 Object l=super.get(name); 84 if (i==0 && LazyList.size(l)==0) 85 return null; 86 return LazyList.get(l,i); 87 } 88 89 90 91 98 public String getString(Object name) 99 { 100 Object l=super.get(name); 101 switch(LazyList.size(l)) 102 { 103 case 0: 104 return null; 105 case 1: 106 Object o=LazyList.get(l,0); 107 return o==null?null:o.toString(); 108 default: 109 StringBuffer values=new StringBuffer (128); 110 synchronized(values) 111 { 112 for (int i=0; i<LazyList.size(l); i++) 113 { 114 Object e=LazyList.get(l,i); 115 if (e!=null) 116 { 117 if (values.length()>0) 118 values.append(','); 119 values.append(e.toString()); 120 } 121 } 122 return values.toString(); 123 } 124 } 125 } 126 127 128 public Object get(Object name) 129 { 130 Object l=super.get(name); 131 switch(LazyList.size(l)) 132 { 133 case 0: 134 return null; 135 case 1: 136 Object o=LazyList.get(l,0); 137 return o; 138 default: 139 return LazyList.getList(l,true); 140 } 141 } 142 143 144 149 public Object put(Object name, Object value) 150 { 151 return super.put(name,LazyList.add(null,value)); 152 } 153 154 155 160 public Object putValues(Object name, List values) 161 { 162 return super.put(name,values); 163 } 164 165 166 171 public Object putValues(Object name, String [] values) 172 { 173 Object list=null; 174 for (int i=0;i<values.length;i++) 175 list=LazyList.add(list,values[i]); 176 return put(name,list); 177 } 178 179 180 181 187 public void add(Object name, Object value) 188 { 189 Object lo = super.get(name); 190 Object ln = LazyList.add(lo,value); 191 if (lo!=ln) 192 super.put(name,ln); 193 } 194 195 196 202 public void addValues(Object name, List values) 203 { 204 Object lo = super.get(name); 205 Object ln = LazyList.addCollection(lo,values); 206 if (lo!=ln) 207 super.put(name,ln); 208 } 209 210 211 217 public void addValues(Object name, String [] values) 218 { 219 Object lo = super.get(name); 220 Object ln = LazyList.addCollection(lo,Arrays.asList(values)); 221 if (lo!=ln) 222 super.put(name,ln); 223 } 224 225 226 231 public boolean removeValue(Object name,Object value) 232 { 233 Object lo = super.get(name); 234 Object ln=lo; 235 int s=LazyList.size(lo); 236 if (s>0) 237 { 238 ln=LazyList.remove(lo,value); 239 if (ln==null) 240 super.remove(name); 241 else 242 super.put(name, ln); 243 } 244 return LazyList.size(ln)!=s; 245 } 246 247 248 251 public void putAll(Map m) 252 { 253 Iterator i = m.entrySet().iterator(); 254 boolean multi=m instanceof MultiMap; 255 while(i.hasNext()) 256 { 257 Map.Entry entry = 258 (Map.Entry )i.next(); 259 if (multi) 260 super.put(entry.getKey(),LazyList.clone(entry.getValue())); 261 else 262 put(entry.getKey(),entry.getValue()); 263 } 264 } 265 266 267 270 public Map toStringArrayMap() 271 { 272 HashMap map = new HashMap (size()*3/2); 273 274 Iterator i = super.entrySet().iterator(); 275 while(i.hasNext()) 276 { 277 Map.Entry entry = (Map.Entry )i.next(); 278 Object l = entry.getValue(); 279 map.put(entry.getKey(),LazyList.toStringArray(l)); 280 } 281 return map; 282 } 283 284 285 public Object clone() 286 { 287 MultiMap mm = (MultiMap) super.clone(); 288 289 Iterator iter = mm.entrySet().iterator(); 290 while (iter.hasNext()) 291 { 292 Map.Entry entry = (Map.Entry )iter.next(); 293 entry.setValue(LazyList.clone(entry.getValue())); 294 } 295 296 return mm; 297 } 298 } 299 | Popular Tags |