KickJava   Java API By Example, From Geeks To Geeks.

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


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.TCObject;
8 import com.tc.object.bytecode.ManagerUtil;
9 import com.tc.object.lockmanager.api.LockLevel;
10
11 import java.util.Collection JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map.Entry;
15
16 public class HashtableValuesWrapper implements Collection JavaDoc {
17
18   private final Collection JavaDoc realValues;
19   private final Hashtable JavaDoc hashtable;
20
21   public HashtableValuesWrapper(Hashtable JavaDoc hashtable, Collection JavaDoc realValues) {
22     this.hashtable = hashtable;
23     this.realValues = realValues;
24   }
25
26   public final boolean add(Object JavaDoc o) {
27     return realValues.add(o);
28   }
29
30   public final boolean addAll(Collection JavaDoc c) {
31     return realValues.addAll(c);
32   }
33
34   public final void clear() {
35     realValues.clear();
36   }
37
38   public final boolean contains(Object JavaDoc o) {
39     return realValues.contains(o);
40   }
41
42   public final boolean containsAll(Collection JavaDoc c) {
43     return realValues.containsAll(c);
44   }
45
46   public final boolean equals(Object JavaDoc o) {
47     return realValues.equals(o);
48   }
49
50   public final int hashCode() {
51     return realValues.hashCode();
52   }
53
54   public final boolean isEmpty() {
55     return realValues.isEmpty();
56   }
57
58   public final Iterator JavaDoc iterator() {
59     return new IteratorWrapper(hashtable, realValues.iterator());
60   }
61
62   public final boolean remove(Object JavaDoc o) {
63     Iterator JavaDoc iter = iterator();
64     if (o == null) {
65       while (iter.hasNext()) {
66         if (iter.next() == null) {
67           iter.remove();
68           return true;
69         }
70       }
71     } else {
72       while (iter.hasNext()) {
73         if (o.equals(iter.next())) {
74           iter.remove();
75           return true;
76         }
77       }
78     }
79     return false;
80   }
81
82   public final boolean removeAll(Collection JavaDoc c) {
83     boolean modified = false;
84     Iterator JavaDoc iter = iterator();
85     while (iter.hasNext()) {
86       if (c.contains(iter.next())) {
87         iter.remove();
88         modified = true;
89       }
90     }
91     return modified;
92   }
93
94   public final boolean retainAll(Collection JavaDoc c) {
95     boolean modified = false;
96     Iterator JavaDoc i = iterator();
97     while (i.hasNext()) {
98       if (!c.contains(i.next())) {
99         i.remove();
100         modified = true;
101       }
102     }
103     return modified;
104   }
105
106   public final int size() {
107     return realValues.size();
108   }
109
110   public final Object JavaDoc[] toArray() {
111     return realValues.toArray();
112   }
113
114   public final Object JavaDoc[] toArray(Object JavaDoc[] a) {
115     return realValues.toArray(a);
116   }
117
118   private static class IteratorWrapper implements Iterator JavaDoc {
119
120     private final Iterator JavaDoc realIterator;
121     private final Hashtable JavaDoc hashtable;
122     private Object JavaDoc lastValue;
123
124     public IteratorWrapper(Hashtable JavaDoc hashtable, Iterator JavaDoc realIterator) {
125       this.hashtable = hashtable;
126       this.realIterator = realIterator;
127     }
128
129     public final boolean hasNext() {
130       return realIterator.hasNext();
131     }
132
133     public final Object JavaDoc next() {
134       lastValue = realIterator.next();
135       return lastValue;
136     }
137
138     public final void remove() {
139       // XXX: This linear scan of the hashtable to find the proper key is bad. The only way to it (I think) would be to
140
// instrument the actual hastable.values.iterator.remove code that has access to the key object right then and
141
// there
142
ManagerUtil.monitorEnter(hashtable, LockLevel.WRITE);
143
144       try {
145         TCObject tco = ManagerUtil.lookupExistingOrNull(hashtable);
146         Object JavaDoc key = null;
147         if (tco != null) {
148           // find the key object iff this is a managed hashtable
149
for (Iterator JavaDoc i = hashtable.entrySet().iterator(); i.hasNext();) {
150             Entry entry = (Entry) i.next();
151             if (entry.getValue().equals(lastValue)) {
152               key = entry.getKey();
153               break;
154             }
155           }
156
157           if (key == null) { throw new AssertionError JavaDoc("Did not find key for value " + lastValue); }
158         }
159
160         realIterator.remove();
161
162         // the real itearator can throw an exception. Do DSO work iff no exception thrown
163
if (tco != null) {
164           ManagerUtil.logicalInvoke(hashtable, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object JavaDoc[] { key });
165         }
166       } finally {
167         ManagerUtil.monitorExit(hashtable);
168       }
169     }
170   }
171
172 }
Popular Tags