KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcclient > util > HashtableEntrySetWrapper


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tcclient.util;
5
6 import com.tc.object.SerializationUtil;
7 import com.tc.object.bytecode.ManagerUtil;
8 import com.tc.object.lockmanager.api.LockLevel;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
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 JavaDoc CLASS = "com/tcclient/util/HashtableEntrySetWrapper";
20
21   public HashtableEntrySetWrapper(Map JavaDoc map, Set JavaDoc realEntrySet) {
22     super(map, realEntrySet);
23   }
24
25   public final Iterator JavaDoc iterator() {
26     return new HashtableIteratorWrapper(map, realEntrySet.iterator());
27   }
28
29   public final boolean remove(Object JavaDoc o) {
30     boolean removed = false;
31     ManagerUtil.monitorEnter(map, LockLevel.WRITE);
32     try {
33       ManagerUtil.checkWriteAccess(map);
34       removed = realEntrySet.remove(o);
35       if (removed) {
36         ManagerUtil.logicalInvoke(map, SerializationUtil.REMOVE_KEY_SIGNATURE,
37                                   new Object JavaDoc[] { ((Map.Entry JavaDoc) o).getKey() });
38       }
39     } finally {
40       ManagerUtil.monitorExit(map);
41     }
42     return removed;
43   }
44
45   private static class HashtableIteratorWrapper implements Iterator JavaDoc {
46
47     protected final Iterator JavaDoc realIterator;
48     protected final Map JavaDoc map;
49     protected Entry current;
50
51     HashtableIteratorWrapper(Map JavaDoc map, Iterator JavaDoc realIterator) {
52       this.map = map;
53       this.realIterator = realIterator;
54     }
55
56     public final void remove() {
57       ManagerUtil.monitorEnter(map, LockLevel.WRITE);
58       try {
59         realIterator.remove();
60
61         // important to do this after the real remove() since an exception can be thrown (never
62
// started, at end, concurrent mod, etc)
63
ManagerUtil.logicalInvoke(map, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object JavaDoc[] { current.getKey() });
64       } finally {
65         ManagerUtil.monitorExit(map);
66       }
67     }
68
69     public final boolean hasNext() {
70       boolean rv = realIterator.hasNext();
71       return rv;
72     }
73
74     public final Object JavaDoc next() {
75       current = new EntryWrapper(map, (Entry) realIterator.next());
76       return current;
77     }
78
79   }
80
81 }
82
Popular Tags