1 4 package com.tc.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 14 public class THashMapCollectionWrapper implements Set { 15 16 private final Collection realValues; 17 private final Map map; 18 19 public THashMapCollectionWrapper(Map map, Collection realValues) { 20 this.map = map; 21 this.realValues = realValues; 22 } 23 24 public boolean add(Object o) { 25 return realValues.add(o); 26 } 27 28 public boolean addAll(Collection c) { 29 return realValues.addAll(c); 30 } 31 32 public void clear() { 33 realValues.clear(); 34 } 35 36 public boolean contains(Object o) { 37 return realValues.contains(o); 38 } 39 40 public boolean containsAll(Collection c) { 41 return realValues.containsAll(c); 42 } 43 44 public boolean equals(Object o) { 45 return realValues.equals(o); 46 } 47 48 public int hashCode() { 49 return realValues.hashCode(); 50 } 51 52 public boolean isEmpty() { 53 return realValues.isEmpty(); 54 } 55 56 public Iterator iterator() { 57 return new IteratorWrapper(map, realValues.iterator()); 58 } 59 60 public boolean remove(Object o) { 61 ManagerUtil.checkWriteAccess(map); 62 return realValues.remove(o); 63 } 64 65 public boolean removeAll(Collection c) { 66 return realValues.removeAll(c); 67 } 68 69 public boolean retainAll(Collection c) { 70 return realValues.retainAll(c); 71 } 72 73 public int size() { 74 return realValues.size(); 75 } 76 77 public Object [] toArray() { 78 return realValues.toArray(); 79 } 80 81 public Object [] toArray(Object [] a) { 82 int size = size(); 83 if (a.length < size) a = (Object []) Array.newInstance(((Object ) (a)).getClass().getComponentType(), size); 84 85 int index = 0; 86 for (Iterator iterator = iterator(); iterator.hasNext();) { 87 ManagerUtil.objectArrayChanged(a, index++, iterator.next()); 88 } 89 90 if (a.length > size) { 91 a[size] = null; 92 } 93 return a; 94 } 95 96 private static class IteratorWrapper implements Iterator { 97 private final Iterator realIterator; 98 private final Map map; 99 private Object lastValue; 100 101 public IteratorWrapper(Map map, Iterator realIterator) { 102 this.map = map; 103 this.realIterator = realIterator; 104 } 105 106 public boolean hasNext() { 107 return realIterator.hasNext(); 108 } 109 110 public Object next() { 111 lastValue = realIterator.next(); 112 return lastValue; 113 } 114 115 public void remove() { 116 ManagerUtil.checkWriteAccess(map); 117 realIterator.remove(); 118 } 119 } 120 121 } | Popular Tags |