KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > HashtableKeySetWrapper


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.tc.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.Collection JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14
15 public class HashtableKeySetWrapper implements Set JavaDoc {
16
17   private final Set JavaDoc realKeySet;
18   private final Hashtable JavaDoc hashtable;
19
20   public HashtableKeySetWrapper(Hashtable JavaDoc hashtable, Set JavaDoc realKeySet) {
21     this.hashtable = hashtable;
22     this.realKeySet = realKeySet;
23   }
24
25   public final boolean add(Object JavaDoc o) {
26     // will throw an exception
27
return realKeySet.add(o);
28   }
29
30   public final boolean addAll(Collection JavaDoc c) {
31     // will throw an exception
32
return realKeySet.addAll(c);
33   }
34
35   public final void clear() {
36     // calls through to Hashtable.clear()
37
realKeySet.clear();
38   }
39
40   public final boolean contains(Object JavaDoc o) {
41     return realKeySet.contains(o);
42   }
43
44   public final boolean containsAll(Collection JavaDoc c) {
45     return realKeySet.containsAll(c);
46   }
47
48   public final boolean equals(Object JavaDoc o) {
49     return realKeySet.equals(o);
50   }
51
52   public final int hashCode() {
53     return realKeySet.hashCode();
54   }
55
56   public final boolean isEmpty() {
57     return realKeySet.isEmpty();
58   }
59
60   public final Iterator JavaDoc iterator() {
61     return new IteratorWrapper(hashtable, realKeySet.iterator());
62   }
63
64   public final boolean remove(Object JavaDoc o) {
65     // calls through to Hashtable.remove()
66
return realKeySet.remove(o);
67   }
68
69   public final boolean removeAll(Collection JavaDoc c) {
70     // ends up calling remove() on this, or remove() on this.iterator()
71
return realKeySet.removeAll(c);
72   }
73
74   public final boolean retainAll(Collection JavaDoc c) {
75     boolean modified = false;
76     Iterator JavaDoc i = iterator();
77     while (i.hasNext()) {
78       if (!c.contains(i.next())) {
79         i.remove();
80         modified = true;
81       }
82     }
83     return modified;
84   }
85
86   public final int size() {
87     return realKeySet.size();
88   }
89
90   public final Object JavaDoc[] toArray() {
91     return realKeySet.toArray();
92   }
93
94   public final Object JavaDoc[] toArray(Object JavaDoc[] a) {
95     return realKeySet.toArray(a);
96   }
97
98   public static class IteratorWrapper implements Iterator JavaDoc {
99
100     private final Iterator JavaDoc realIterator;
101     private final Hashtable JavaDoc hashtable;
102     private Object JavaDoc last;
103
104     public IteratorWrapper(Hashtable JavaDoc hashtable, Iterator JavaDoc realIterator) {
105       this.hashtable = hashtable;
106       this.realIterator = realIterator;
107     }
108
109     public final void remove() {
110       ManagerUtil.monitorEnter(hashtable, LockLevel.WRITE);
111       try {
112         realIterator.remove();
113         // Do the real remove first. If no exception thrown, then proceed with the DSO stuff
114
ManagerUtil.logicalInvoke(hashtable, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object JavaDoc[] { last });
115       } finally {
116         ManagerUtil.monitorExit(hashtable);
117       }
118     }
119
120     public final boolean hasNext() {
121       return realIterator.hasNext();
122     }
123
124     public final Object JavaDoc next() {
125       last = realIterator.next();
126       return last;
127     }
128   }
129
130 }
131
Popular Tags