KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 import java.lang.reflect.Array JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.Map.Entry;
15
16 /**
17  * A wrapper for Map.entrySet() that keeps DSO informed of changes
18  */

19 public class MapEntrySetWrapper implements Set JavaDoc {
20   public final static String JavaDoc CLASS_SLASH = "com/tcclient/util/MapEntrySetWrapper";
21
22   protected final Set JavaDoc realEntrySet;
23   protected final Map JavaDoc map;
24
25   public MapEntrySetWrapper(Map JavaDoc map, Set JavaDoc realEntrySet) {
26     this.realEntrySet = realEntrySet;
27     this.map = map;
28   }
29
30   public final boolean add(Object JavaDoc o) {
31     return realEntrySet.add(o);
32   }
33
34   public final boolean addAll(Collection JavaDoc c) {
35     return realEntrySet.addAll(c);
36   }
37
38   public final void clear() {
39     realEntrySet.clear();
40   }
41
42   public final boolean contains(Object JavaDoc o) {
43     return realEntrySet.contains(o);
44   }
45
46   public final boolean containsAll(Collection JavaDoc c) {
47     return realEntrySet.containsAll(c);
48   }
49
50   public final boolean equals(Object JavaDoc o) {
51     return realEntrySet.equals(o);
52   }
53
54   public final int hashCode() {
55     return realEntrySet.hashCode();
56   }
57
58   public final boolean isEmpty() {
59     return realEntrySet.isEmpty();
60   }
61
62   public Iterator JavaDoc iterator() {
63       return new IteratorWrapper(map, realEntrySet.iterator());
64   }
65
66   public boolean remove(Object JavaDoc o) {
67     ManagerUtil.checkWriteAccess(map);
68     boolean removed = realEntrySet.remove(o);
69     if (removed) {
70       ManagerUtil.logicalInvoke(map, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object JavaDoc[] { ((Map.Entry JavaDoc) o).getKey() });
71     }
72     return removed;
73   }
74
75   public final boolean removeAll(Collection JavaDoc c) {
76     boolean modified = false;
77
78     if (size() > c.size()) {
79       for (Iterator JavaDoc i = c.iterator(); i.hasNext();)
80         modified |= remove(i.next());
81     } else {
82       for (Iterator JavaDoc i = iterator(); i.hasNext();) {
83         if (c.contains(i.next())) {
84           i.remove();
85           modified = true;
86         }
87       }
88     }
89     return modified;
90   }
91
92   public final boolean retainAll(Collection JavaDoc c) {
93     boolean modified = false;
94     Iterator JavaDoc i = iterator();
95     while (i.hasNext()) {
96       if (!c.contains(i.next())) {
97         i.remove();
98         modified = true;
99       }
100     }
101     return modified;
102
103   }
104
105   public final int size() {
106     return realEntrySet.size();
107   }
108
109   public final Object JavaDoc[] toArray() {
110     return realEntrySet.toArray();
111   }
112
113   public final Object JavaDoc[] toArray(Object JavaDoc[] a) {
114     int size = size();
115     if (a.length < size) a = (Object JavaDoc[]) Array.newInstance(((Object JavaDoc) (a)).getClass().getComponentType(), size);
116
117     int index = 0;
118     for (Iterator JavaDoc iterator = iterator(); iterator.hasNext();) {
119       ManagerUtil.objectArrayChanged(a, index++, iterator.next());
120     }
121
122     if (a.length > size) {
123       a[size] = null;
124     }
125     return a;
126   }
127
128   private static class IteratorWrapper implements Iterator JavaDoc {
129
130     protected final Iterator JavaDoc realIterator;
131     protected final Map JavaDoc map;
132     protected Entry current;
133
134     IteratorWrapper(Map JavaDoc map, Iterator JavaDoc realIterator) {
135       this.map = map;
136       this.realIterator = realIterator;
137     }
138
139     public void remove() {
140       ManagerUtil.checkWriteAccess(map);
141       realIterator.remove();
142
143       // important to do this after the real remove() since an exception can be thrown (never
144
// started, at end, concurrent mod, etc)
145
ManagerUtil.logicalInvoke(map, SerializationUtil.REMOVE_KEY_SIGNATURE, new Object JavaDoc[] { current.getKey() });
146     }
147
148     public final boolean hasNext() {
149       boolean rv = realIterator.hasNext();
150       return rv;
151     }
152
153     public final Object JavaDoc next() {
154       current = new EntryWrapper(map, (Entry) realIterator.next());
155       return current;
156     }
157
158   }
159
160   protected static class EntryWrapper implements Entry {
161     private final Object JavaDoc key;
162     private Object JavaDoc value;
163     private final Map JavaDoc map;
164
165     EntryWrapper(Map JavaDoc map, Entry entry) {
166       this.map = map;
167       this.key = entry.getKey();
168       this.value = entry.getValue();
169     }
170
171     public final boolean equals(Object JavaDoc o) {
172       if (!(o instanceof Entry)) { return false; }
173       Entry e2 = (Entry) o;
174       return (key == null ? e2.getKey() == null : key.equals(e2.getKey()))
175              && (value == null ? e2.getValue() == null : value.equals(e2.getValue()));
176     }
177
178     public final Object JavaDoc getKey() {
179       return key;
180     }
181
182     public final Object JavaDoc getValue() {
183       return value;
184     }
185
186     public final int hashCode() {
187       return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
188     }
189
190     public final Object JavaDoc setValue(Object JavaDoc value) {
191       Object JavaDoc rv = map.put(this.key, value);
192       this.value = value;
193       return rv;
194     }
195
196     public String JavaDoc toString() {
197       return key + "=" + value;
198     }
199   }
200
201 }
202
Popular Tags