1 4 package com.tc.util; 5 6 import com.tc.object.SerializationUtil; 7 import com.tc.object.TCObject; 8 import com.tc.object.bytecode.ManagerUtil; 9 import com.tc.object.lockmanager.api.LockLevel; 10 11 import java.util.Collection ; 12 import java.util.Hashtable ; 13 import java.util.Iterator ; 14 import java.util.Map.Entry; 15 16 public class HashtableValuesWrapper implements Collection { 17 18 private final Collection realValues; 19 private final Hashtable hashtable; 20 21 public HashtableValuesWrapper(Hashtable hashtable, Collection realValues) { 22 this.hashtable = hashtable; 23 this.realValues = realValues; 24 } 25 26 public final boolean add(Object o) { 27 return realValues.add(o); 28 } 29 30 public final boolean addAll(Collection c) { 31 return realValues.addAll(c); 32 } 33 34 public final void clear() { 35 realValues.clear(); 36 } 37 38 public final boolean contains(Object o) { 39 return realValues.contains(o); 40 } 41 42 public final boolean containsAll(Collection c) { 43 return realValues.containsAll(c); 44 } 45 46 public final boolean equals(Object o) { 47 return realValues.equals(o); 48 } 49 50 public final int hashCode() { 51 return realValues.hashCode(); 52 } 53 54 public final boolean isEmpty() { 55 return realValues.isEmpty(); 56 } 57 58 public final Iterator iterator() { 59 return new IteratorWrapper(hashtable, realValues.iterator()); 60 } 61 62 public final boolean remove(Object o) { 63 Iterator iter = iterator(); 64 if (o == null) { 65 while (iter.hasNext()) { 66 if (iter.next() == null) { 67 iter.remove(); 68 return true; 69 } 70 } 71 } else { 72 while (iter.hasNext()) { 73 if (o.equals(iter.next())) { 74 iter.remove(); 75 return true; 76 } 77 } 78 } 79 return false; 80 } 81 82 public final boolean removeAll(Collection c) { 83 boolean modified = false; 84 Iterator iter = iterator(); 85 while (iter.hasNext()) { 86 if (c.contains(iter.next())) { 87 iter.remove(); 88 modified = true; 89 } 90 } 91 return modified; 92 } 93 94 public final boolean retainAll(Collection c) { 95 boolean modified = false; 96 Iterator i = iterator(); 97 while (i.hasNext()) { 98 if (!c.contains(i.next())) { 99 i.remove(); 100 modified = true; 101 } 102 } 103 return modified; 104 } 105 106 public final int size() { 107 return realValues.size(); 108 } 109 110 public final Object [] toArray() { 111 return realValues.toArray(); 112 } 113 114 public final Object [] toArray(Object [] a) { 115 return realValues.toArray(a); 116 } 117 118 private static class IteratorWrapper implements Iterator { 119 120 private final Iterator realIterator; 121 private final Hashtable hashtable; 122 private Object lastValue; 123 124 public IteratorWrapper(Hashtable hashtable, Iterator realIterator) { 125 this.hashtable = hashtable; 126 this.realIterator = realIterator; 127 } 128 129 public final boolean hasNext() { 130 return realIterator.hasNext(); 131 } 132 133 public final Object next() { 134 lastValue = realIterator.next(); 135 return lastValue; 136 } 137 138 public final void remove() { 139 ManagerUtil.monitorEnter(hashtable, LockLevel.WRITE); 143 144 try { 145 TCObject tco = ManagerUtil.lookupExistingOrNull(hashtable); 146 Object key = null; 147 if (tco != null) { 148 for (Iterator i = hashtable.entrySet().iterator(); i.hasNext();) { 150 Entry entry = (Entry) i.next(); 151 if (entry.getValue().equals(lastValue)) { 152 key = entry.getKey(); 153 break; 154 } 155 } 156 157 if (key == null) { throw new AssertionError ("Did not find key for value " + lastValue); } 158 } 159 160 realIterator.remove(); 161 162 if (tco != null) { 164 ManagerUtil.logicalInvoke(hashtable, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object [] { key }); 165 } 166 } finally { 167 ManagerUtil.monitorExit(hashtable); 168 } 169 } 170 } 171 172 } | Popular Tags |