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