001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import com.tc.object.bytecode.ManagerUtil;
007:
008: import java.lang.reflect.Array;
009: import java.util.Collection;
010: import java.util.Iterator;
011: import java.util.Map;
012: import java.util.Set;
013:
014: public class THashMapCollectionWrapper implements Set {
015:
016: private final Collection realValues;
017: private final Map map;
018:
019: public THashMapCollectionWrapper(Map map, Collection realValues) {
020: this .map = map;
021: this .realValues = realValues;
022: }
023:
024: public boolean add(Object o) {
025: return realValues.add(o);
026: }
027:
028: public boolean addAll(Collection c) {
029: return realValues.addAll(c);
030: }
031:
032: public void clear() {
033: realValues.clear();
034: }
035:
036: public boolean contains(Object o) {
037: return realValues.contains(o);
038: }
039:
040: public boolean containsAll(Collection c) {
041: return realValues.containsAll(c);
042: }
043:
044: public boolean equals(Object o) {
045: return realValues.equals(o);
046: }
047:
048: public int hashCode() {
049: return realValues.hashCode();
050: }
051:
052: public boolean isEmpty() {
053: return realValues.isEmpty();
054: }
055:
056: public Iterator iterator() {
057: return new IteratorWrapper(map, realValues.iterator());
058: }
059:
060: public boolean remove(Object o) {
061: ManagerUtil.checkWriteAccess(map);
062: return realValues.remove(o);
063: }
064:
065: public boolean removeAll(Collection c) {
066: return realValues.removeAll(c);
067: }
068:
069: public boolean retainAll(Collection c) {
070: return realValues.retainAll(c);
071: }
072:
073: public int size() {
074: return realValues.size();
075: }
076:
077: public Object[] toArray() {
078: return realValues.toArray();
079: }
080:
081: public Object[] toArray(Object[] a) {
082: int size = size();
083: if (a.length < size)
084: a = (Object[]) Array.newInstance(((Object) (a)).getClass()
085: .getComponentType(), size);
086:
087: int index = 0;
088: for (Iterator iterator = iterator(); iterator.hasNext();) {
089: ManagerUtil.objectArrayChanged(a, index++, iterator.next());
090: }
091:
092: if (a.length > size) {
093: a[size] = null;
094: }
095: return a;
096: }
097:
098: private static class IteratorWrapper implements Iterator {
099: private final Iterator realIterator;
100: private final Map map;
101: private Object lastValue;
102:
103: public IteratorWrapper(Map map, Iterator realIterator) {
104: this .map = map;
105: this .realIterator = realIterator;
106: }
107:
108: public boolean hasNext() {
109: return realIterator.hasNext();
110: }
111:
112: public Object next() {
113: lastValue = realIterator.next();
114: return lastValue;
115: }
116:
117: public void remove() {
118: ManagerUtil.checkWriteAccess(map);
119: realIterator.remove();
120: }
121: }
122:
123: }
|