1 4 package com.tcclient.util; 5 6 import com.tc.object.SerializationUtil; 7 import com.tc.object.bytecode.ManagerUtil; 8 9 import java.lang.reflect.Array ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.Set ; 14 import java.util.Map.Entry; 15 16 19 public class MapEntrySetWrapper implements Set { 20 public final static String CLASS_SLASH = "com/tcclient/util/MapEntrySetWrapper"; 21 22 protected final Set realEntrySet; 23 protected final Map map; 24 25 public MapEntrySetWrapper(Map map, Set realEntrySet) { 26 this.realEntrySet = realEntrySet; 27 this.map = map; 28 } 29 30 public final boolean add(Object o) { 31 return realEntrySet.add(o); 32 } 33 34 public final boolean addAll(Collection c) { 35 return realEntrySet.addAll(c); 36 } 37 38 public final void clear() { 39 realEntrySet.clear(); 40 } 41 42 public final boolean contains(Object o) { 43 return realEntrySet.contains(o); 44 } 45 46 public final boolean containsAll(Collection c) { 47 return realEntrySet.containsAll(c); 48 } 49 50 public final boolean equals(Object o) { 51 return realEntrySet.equals(o); 52 } 53 54 public final int hashCode() { 55 return realEntrySet.hashCode(); 56 } 57 58 public final boolean isEmpty() { 59 return realEntrySet.isEmpty(); 60 } 61 62 public Iterator iterator() { 63 return new IteratorWrapper(map, realEntrySet.iterator()); 64 } 65 66 public boolean remove(Object o) { 67 ManagerUtil.checkWriteAccess(map); 68 boolean removed = realEntrySet.remove(o); 69 if (removed) { 70 ManagerUtil.logicalInvoke(map, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object [] { ((Map.Entry ) o).getKey() }); 71 } 72 return removed; 73 } 74 75 public final boolean removeAll(Collection c) { 76 boolean modified = false; 77 78 if (size() > c.size()) { 79 for (Iterator i = c.iterator(); i.hasNext();) 80 modified |= remove(i.next()); 81 } else { 82 for (Iterator i = iterator(); i.hasNext();) { 83 if (c.contains(i.next())) { 84 i.remove(); 85 modified = true; 86 } 87 } 88 } 89 return modified; 90 } 91 92 public final boolean retainAll(Collection c) { 93 boolean modified = false; 94 Iterator i = iterator(); 95 while (i.hasNext()) { 96 if (!c.contains(i.next())) { 97 i.remove(); 98 modified = true; 99 } 100 } 101 return modified; 102 103 } 104 105 public final int size() { 106 return realEntrySet.size(); 107 } 108 109 public final Object [] toArray() { 110 return realEntrySet.toArray(); 111 } 112 113 public final Object [] toArray(Object [] a) { 114 int size = size(); 115 if (a.length < size) a = (Object []) Array.newInstance(((Object ) (a)).getClass().getComponentType(), size); 116 117 int index = 0; 118 for (Iterator iterator = iterator(); iterator.hasNext();) { 119 ManagerUtil.objectArrayChanged(a, index++, iterator.next()); 120 } 121 122 if (a.length > size) { 123 a[size] = null; 124 } 125 return a; 126 } 127 128 private static class IteratorWrapper implements Iterator { 129 130 protected final Iterator realIterator; 131 protected final Map map; 132 protected Entry current; 133 134 IteratorWrapper(Map map, Iterator realIterator) { 135 this.map = map; 136 this.realIterator = realIterator; 137 } 138 139 public void remove() { 140 ManagerUtil.checkWriteAccess(map); 141 realIterator.remove(); 142 143 ManagerUtil.logicalInvoke(map, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object [] { current.getKey() }); 146 } 147 148 public final boolean hasNext() { 149 boolean rv = realIterator.hasNext(); 150 return rv; 151 } 152 153 public final Object next() { 154 current = new EntryWrapper(map, (Entry) realIterator.next()); 155 return current; 156 } 157 158 } 159 160 protected static class EntryWrapper implements Entry { 161 private final Object key; 162 private Object value; 163 private final Map map; 164 165 EntryWrapper(Map map, Entry entry) { 166 this.map = map; 167 this.key = entry.getKey(); 168 this.value = entry.getValue(); 169 } 170 171 public final boolean equals(Object o) { 172 if (!(o instanceof Entry)) { return false; } 173 Entry e2 = (Entry) o; 174 return (key == null ? e2.getKey() == null : key.equals(e2.getKey())) 175 && (value == null ? e2.getValue() == null : value.equals(e2.getValue())); 176 } 177 178 public final Object getKey() { 179 return key; 180 } 181 182 public final Object getValue() { 183 return value; 184 } 185 186 public final int hashCode() { 187 return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode()); 188 } 189 190 public final Object setValue(Object value) { 191 Object rv = map.put(this.key, value); 192 this.value = value; 193 return rv; 194 } 195 196 public String toString() { 197 return key + "=" + value; 198 } 199 } 200 201 } 202 | Popular Tags |