01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tcclient.util;
05:
06: import com.tc.object.SerializationUtil;
07: import com.tc.object.bytecode.ManagerUtil;
08: import com.tc.object.lockmanager.api.LockLevel;
09:
10: import java.util.Iterator;
11: import java.util.Map;
12: import java.util.Set;
13: import java.util.Map.Entry;
14:
15: /**
16: * A wrapper for Map.entrySet() that keeps DSO informed of changes
17: */
18: public class HashtableEntrySetWrapper extends MapEntrySetWrapper {
19: public final static String CLASS = "com/tcclient/util/HashtableEntrySetWrapper";
20:
21: public HashtableEntrySetWrapper(Map map, Set realEntrySet) {
22: super (map, realEntrySet);
23: }
24:
25: public final Iterator iterator() {
26: return new HashtableIteratorWrapper(map, realEntrySet
27: .iterator());
28: }
29:
30: public final boolean remove(Object o) {
31: boolean removed = false;
32: ManagerUtil.monitorEnter(map, LockLevel.WRITE);
33: try {
34: ManagerUtil.checkWriteAccess(map);
35: removed = realEntrySet.remove(o);
36: if (removed) {
37: ManagerUtil.logicalInvoke(map,
38: SerializationUtil.REMOVE_KEY_SIGNATURE,
39: new Object[] { ((Map.Entry) o).getKey() });
40: }
41: } finally {
42: ManagerUtil.monitorExit(map);
43: }
44: return removed;
45: }
46:
47: private static class HashtableIteratorWrapper implements Iterator {
48:
49: protected final Iterator realIterator;
50: protected final Map map;
51: protected Entry current;
52:
53: HashtableIteratorWrapper(Map map, Iterator realIterator) {
54: this .map = map;
55: this .realIterator = realIterator;
56: }
57:
58: public final void remove() {
59: ManagerUtil.monitorEnter(map, LockLevel.WRITE);
60: try {
61: realIterator.remove();
62:
63: // important to do this after the real remove() since an exception can be thrown (never
64: // started, at end, concurrent mod, etc)
65: ManagerUtil.logicalInvoke(map,
66: SerializationUtil.REMOVE_KEY_SIGNATURE,
67: new Object[] { current.getKey() });
68: } finally {
69: ManagerUtil.monitorExit(map);
70: }
71: }
72:
73: public final boolean hasNext() {
74: boolean rv = realIterator.hasNext();
75: return rv;
76: }
77:
78: public final Object next() {
79: current = new EntryWrapper(map, (Entry) realIterator.next());
80: return current;
81: }
82:
83: }
84:
85: }
|