1 24 package org.ofbiz.base.util.collections; 25 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import javolution.lang.Reusable; 34 import javolution.realtime.ObjectFactory; 35 import javolution.util.FastList; 36 import javolution.util.FastMap; 37 import javolution.util.FastSet; 38 39 import org.ofbiz.base.util.Debug; 40 41 42 49 public class MapStack implements Map , Reusable { 50 51 public static final String module = MapStack.class.getName(); 52 53 protected static final ObjectFactory mapStackFactory = new ObjectFactory() { 54 protected Object create() { 55 return new MapStack(); 56 } 57 }; 58 59 public static MapStack create() { 60 MapStack newValue = (MapStack) mapStackFactory.object(); 61 newValue.push(); 63 return newValue; 64 } 65 66 public static MapStack create(Map baseMap) { 67 MapStack newValue = (MapStack) mapStackFactory.object(); 68 newValue.stackList.add(0, baseMap); 69 return newValue; 70 } 71 72 73 public static MapStack create(MapStack source) { 74 MapStack newValue = (MapStack) mapStackFactory.object(); 75 newValue.stackList.addAll(source.stackList); 76 return newValue; 77 } 78 79 protected MapStack() { 80 super(); 81 } 82 83 protected List stackList = FastList.newInstance(); 84 85 public void reset() { 86 stackList = FastList.newInstance(); 87 } 88 89 90 public void push() { 91 this.stackList.add(0, FastMap.newInstance()); 92 } 93 94 95 public void push(Map existingMap) { 96 if (existingMap == null) { 97 throw new IllegalArgumentException ("Error: cannot push null existing Map onto a MapStack"); 98 } 99 this.stackList.add(0, existingMap); 100 } 101 102 103 public void addToBottom(Map existingMap) { 104 if (existingMap == null) { 105 throw new IllegalArgumentException ("Error: cannot add null existing Map to bottom of a MapStack"); 106 } 107 this.stackList.add(existingMap); 108 } 109 110 111 public Map pop() { 112 if (this.stackList.size() > 1) { 114 return (Map ) stackList.remove(0); 115 } else { 116 return null; 117 } 118 } 119 120 126 public MapStack standAloneStack() { 127 MapStack standAlone = MapStack.create(this); 128 return standAlone; 129 } 130 131 137 public MapStack standAloneChildStack() { 138 MapStack standAloneChild = MapStack.create(this); 139 standAloneChild.push(); 140 return standAloneChild; 141 } 142 143 146 public int size() { 147 Set keys = this.keySet(); 150 return keys.size(); 151 } 152 153 156 public boolean isEmpty() { 157 Iterator stackIter = this.stackList.iterator(); 159 while (stackIter.hasNext()) { 160 Map curMap = (Map ) stackIter.next(); 161 if (!curMap.isEmpty()) { 162 return false; 163 } 164 } 165 return true; 166 } 167 168 171 public boolean containsKey(Object key) { 172 Iterator stackIter = this.stackList.iterator(); 174 while (stackIter.hasNext()) { 175 Map curMap = (Map ) stackIter.next(); 176 if (curMap.containsKey(key)) { 177 return true; 178 } 179 } 180 return false; 181 } 182 183 186 public boolean containsValue(Object value) { 187 Set resultKeySet = FastSet.newInstance(); 189 Iterator stackIter = this.stackList.iterator(); 190 while (stackIter.hasNext()) { 191 Map curMap = (Map ) stackIter.next(); 192 Iterator curEntrySetIter = curMap.entrySet().iterator(); 193 while (curEntrySetIter.hasNext()) { 194 Map.Entry curEntry = (Map.Entry ) curEntrySetIter.next(); 195 if (!resultKeySet.contains(curEntry.getKey())) { 196 resultKeySet.add(curEntry.getKey()); 197 if (value == null) { 198 if (curEntry.getValue() == null) { 199 return true; 200 } 201 } else { 202 if (value.equals(curEntry.getValue())) { 203 return true; 204 } 205 } 206 } 207 } 208 } 209 return false; 210 } 211 212 215 public Object get(Object key) { 216 if ("context".equals(key)) { 217 return this; 218 } 219 220 Iterator stackIter = this.stackList.iterator(); 222 while (stackIter.hasNext()) { 223 Map curMap = (Map ) stackIter.next(); 224 if (curMap.containsKey(key)) { 226 return curMap.get(key); 227 } 228 } 229 return null; 230 } 231 232 235 public Object put(Object key, Object value) { 236 if ("context".equals(key)) { 237 if (value == null || this != value) { 238 Debug.logWarning("WARNING: Putting a value in a MapStack with key [context] that is not this MapStack, will be hidden by the current MapStack self-reference: " + value, module); 239 } 240 } 241 242 Map currentMap = (Map ) this.stackList.get(0); 244 return currentMap.put(key, value); 245 } 246 247 250 public Object remove(Object key) { 251 Map currentMap = (Map ) this.stackList.get(0); 253 return currentMap.remove(key); 254 } 255 256 259 public void putAll(Map arg0) { 260 Map currentMap = (Map ) this.stackList.get(0); 262 currentMap.putAll(arg0); 263 } 264 265 268 public void clear() { 269 Map currentMap = (Map ) this.stackList.get(0); 271 currentMap.clear(); 272 } 273 274 277 public Set keySet() { 278 Set resultSet = FastSet.newInstance(); 280 Iterator stackIter = this.stackList.iterator(); 281 while (stackIter.hasNext()) { 282 Map curMap = (Map ) stackIter.next(); 283 resultSet.addAll(curMap.keySet()); 284 } 285 return Collections.unmodifiableSet(resultSet); 286 } 287 288 291 public Collection values() { 292 Set resultKeySet = FastSet.newInstance(); 294 List resultValues = FastList.newInstance(); 295 Iterator stackIter = this.stackList.iterator(); 296 while (stackIter.hasNext()) { 297 Map curMap = (Map ) stackIter.next(); 298 Iterator curEntrySetIter = curMap.entrySet().iterator(); 299 while (curEntrySetIter.hasNext()) { 300 Map.Entry curEntry = (Map.Entry ) curEntrySetIter.next(); 301 if (!resultKeySet.contains(curEntry.getKey())) { 302 resultKeySet.add(curEntry.getKey()); 303 resultValues.add(curEntry.getValue()); 304 } 305 } 306 } 307 return Collections.unmodifiableCollection(resultValues); 308 } 309 310 313 public Set entrySet() { 314 Set resultKeySet = FastSet.newInstance(); 316 Set resultEntrySet = FastSet.newInstance(); 317 Iterator stackIter = this.stackList.iterator(); 318 while (stackIter.hasNext()) { 319 Map curMap = (Map ) stackIter.next(); 320 Iterator curEntrySetIter = curMap.entrySet().iterator(); 321 while (curEntrySetIter.hasNext()) { 322 Map.Entry curEntry = (Map.Entry ) curEntrySetIter.next(); 323 if (!resultKeySet.contains(curEntry.getKey())) { 324 resultKeySet.add(curEntry.getKey()); 325 resultEntrySet.add(curEntry); 326 } 327 } 328 } 329 return Collections.unmodifiableSet(resultEntrySet); 330 } 331 332 public String toString() { 333 StringBuffer fullMapString = new StringBuffer (); 334 int curLevel = 0; 335 Iterator stackIter = this.stackList.iterator(); 336 while (stackIter.hasNext()) { 337 Map curMap = (Map ) stackIter.next(); 338 fullMapString.append("============================== Start stack level " + curLevel + "\n"); 339 Iterator curEntrySetIter = curMap.entrySet().iterator(); 340 while (curEntrySetIter.hasNext()) { 341 Map.Entry curEntry = (Map.Entry ) curEntrySetIter.next(); 342 343 fullMapString.append("==>["); 344 fullMapString.append(curEntry.getKey()); 345 fullMapString.append("]:"); 346 if (curEntry.getValue() instanceof MapStack) { 348 fullMapString.append("<Instance of MapStack, not printing to avoid infinite recursion>"); 349 } else { 350 fullMapString.append(curEntry.getValue()); 351 } 352 fullMapString.append("\n"); 353 } 354 fullMapString.append("============================== End stack level " + curLevel + "\n"); 355 curLevel++; 356 } 357 return fullMapString.toString(); 358 } 359 } 360 | Popular Tags |