1 4 5 package polyglot.util; 6 7 import java.util.Map ; 8 import java.util.AbstractMap ; 9 import java.util.HashMap ; 10 import java.util.Set ; 11 import java.util.Iterator ; 12 import java.util.AbstractSet ; 13 14 27 public class NestedMap extends AbstractMap implements Map { 28 32 public NestedMap(Map containing) { 33 this.superMap = containing == null ? NilMap.EMPTY_MAP : containing; 34 this.myMap = new HashMap (); 35 setView = new EntrySet(); 36 nShadowed = 0; 37 } 38 39 45 public Map getContainingMap() { 46 return superMap instanceof NilMap ? null : superMap; 47 } 48 49 53 public void release(Object key) { 54 myMap.remove(key); 55 } 56 57 60 public Map getInnerMap() { 61 return myMap; 62 } 63 64 68 public Set entrySet() { 69 return setView; 70 } 71 72 public int size() { 73 return superMap.size() + myMap.size() - nShadowed; 74 } 75 76 public boolean containsKey(Object key) { 77 return myMap.containsKey(key) || superMap.containsKey(key); 78 } 79 80 public Object get(Object key) { 81 if (myMap.containsKey(key)) 82 return myMap.get(key); 83 else 84 return superMap.get(key); 85 } 86 87 public Object put(Object key, Object value) { 88 if (myMap.containsKey(key)) { 89 return myMap.put(key,value); 90 } else { 91 Object oldV = superMap.get(key); 92 myMap.put(key,value); 93 nShadowed++; 94 return oldV; 95 } 96 } 97 98 public Object remove(Object key) { 99 throw new UnsupportedOperationException ("Remove from NestedMap"); 100 } 101 102 public void clear() { 103 throw new UnsupportedOperationException ("Clear in NestedMap"); 104 } 105 106 public final class KeySet extends AbstractSet { 107 public Iterator iterator() { 108 return new ConcatenatedIterator( 109 myMap.keySet().iterator(), 110 new FilteringIterator(superMap.keySet(), keyNotInMyMap)); 111 } 112 public int size() { 113 return NestedMap.this.size(); 114 } 115 public boolean contains(Object o) { 117 return NestedMap.this.containsKey(o); 118 } 119 public boolean remove(Object o) { 120 throw new UnsupportedOperationException ( 121 "Remove from NestedMap.keySet"); 122 } 123 } 124 125 private final class EntrySet extends AbstractSet { 126 public Iterator iterator() { 127 return new ConcatenatedIterator( 128 myMap.entrySet().iterator(), 129 new FilteringIterator(superMap.entrySet(), entryKeyNotInMyMap)); 130 } 131 public int size() { 132 return NestedMap.this.size(); 133 } 134 public boolean contains(Object o) { 136 if (! (o instanceof Map.Entry )) return false; 137 Map.Entry ent = (Map.Entry ) o; 138 Object entKey = ent.getKey(); 139 Object entVal = ent.getValue(); 140 if (entVal != null) { 141 Object val = NestedMap.this.get(entKey); 142 return (val != null) && val.equals(entVal); 143 } else { 144 return NestedMap.this.containsKey(entKey) && 145 (NestedMap.this.get(entKey) == null); 146 } 147 } 148 public boolean remove(Object o) { 149 throw new UnsupportedOperationException ( 150 "Remove from NestedMap.entrySet"); 151 } 152 } 153 154 private HashMap myMap; 155 private int nShadowed; 156 private Set setView; private Map superMap; 158 private Predicate entryKeyNotInMyMap = new Predicate() { 159 public boolean isTrue(Object o) { 160 Map.Entry ent = (Map.Entry ) o; 161 return ! myMap.containsKey(ent.getKey()); 162 } 163 }; 164 private Predicate keyNotInMyMap = new Predicate() { 165 public boolean isTrue(Object o) { 166 return ! myMap.containsKey(o); 167 } 168 }; 169 170 } 171 172 | Popular Tags |