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.SerializationUtil;
007: import com.tc.object.bytecode.ManagerUtil;
008: import com.tc.object.lockmanager.api.LockLevel;
009:
010: import java.util.Collection;
011: import java.util.Hashtable;
012: import java.util.Iterator;
013: import java.util.Set;
014:
015: public class HashtableKeySetWrapper implements Set {
016:
017: private final Set realKeySet;
018: private final Hashtable hashtable;
019:
020: public HashtableKeySetWrapper(Hashtable hashtable, Set realKeySet) {
021: this .hashtable = hashtable;
022: this .realKeySet = realKeySet;
023: }
024:
025: public final boolean add(Object o) {
026: // will throw an exception
027: return realKeySet.add(o);
028: }
029:
030: public final boolean addAll(Collection c) {
031: // will throw an exception
032: return realKeySet.addAll(c);
033: }
034:
035: public final void clear() {
036: // calls through to Hashtable.clear()
037: realKeySet.clear();
038: }
039:
040: public final boolean contains(Object o) {
041: return realKeySet.contains(o);
042: }
043:
044: public final boolean containsAll(Collection c) {
045: return realKeySet.containsAll(c);
046: }
047:
048: public final boolean equals(Object o) {
049: return realKeySet.equals(o);
050: }
051:
052: public final int hashCode() {
053: return realKeySet.hashCode();
054: }
055:
056: public final boolean isEmpty() {
057: return realKeySet.isEmpty();
058: }
059:
060: public final Iterator iterator() {
061: return new IteratorWrapper(hashtable, realKeySet.iterator());
062: }
063:
064: public final boolean remove(Object o) {
065: // calls through to Hashtable.remove()
066: return realKeySet.remove(o);
067: }
068:
069: public final boolean removeAll(Collection c) {
070: // ends up calling remove() on this, or remove() on this.iterator()
071: return realKeySet.removeAll(c);
072: }
073:
074: public final boolean retainAll(Collection c) {
075: boolean modified = false;
076: Iterator i = iterator();
077: while (i.hasNext()) {
078: if (!c.contains(i.next())) {
079: i.remove();
080: modified = true;
081: }
082: }
083: return modified;
084: }
085:
086: public final int size() {
087: return realKeySet.size();
088: }
089:
090: public final Object[] toArray() {
091: return realKeySet.toArray();
092: }
093:
094: public final Object[] toArray(Object[] a) {
095: return realKeySet.toArray(a);
096: }
097:
098: public static class IteratorWrapper implements Iterator {
099:
100: private final Iterator realIterator;
101: private final Hashtable hashtable;
102: private Object last;
103:
104: public IteratorWrapper(Hashtable hashtable,
105: Iterator realIterator) {
106: this .hashtable = hashtable;
107: this .realIterator = realIterator;
108: }
109:
110: public final void remove() {
111: ManagerUtil.monitorEnter(hashtable, LockLevel.WRITE);
112: try {
113: realIterator.remove();
114: // Do the real remove first. If no exception thrown, then proceed with the DSO stuff
115: ManagerUtil.logicalInvoke(hashtable,
116: SerializationUtil.REMOVE_KEY_SIGNATURE,
117: new Object[] { last });
118: } finally {
119: ManagerUtil.monitorExit(hashtable);
120: }
121: }
122:
123: public final boolean hasNext() {
124: return realIterator.hasNext();
125: }
126:
127: public final Object next() {
128: last = realIterator.next();
129: return last;
130: }
131: }
132:
133: }
|