KickJava   Java API By Example, From Geeks To Geeks.

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


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.bytecode.ManagerUtil;
7
8 import java.lang.reflect.Array JavaDoc;
9 import java.util.Collection JavaDoc;
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 ConcurrentHashMapEntrySetWrapper implements Set JavaDoc {
19   public final static String JavaDoc CLASS_SLASH = "com/tc/util/ConcurrentHashMapEntrySetWrapper";
20
21   protected final Set JavaDoc realEntrySet;
22   protected final Map JavaDoc map;
23
24   public ConcurrentHashMapEntrySetWrapper(Map JavaDoc map, Set JavaDoc realEntrySet) {
25     this.realEntrySet = realEntrySet;
26     this.map = map;
27   }
28
29   public final boolean add(Object JavaDoc o) {
30     return realEntrySet.add(o);
31   }
32
33   public final boolean addAll(Collection JavaDoc c) {
34     return realEntrySet.addAll(c);
35   }
36
37   public final void clear() {
38     realEntrySet.clear();
39   }
40
41   public final boolean contains(Object JavaDoc o) {
42     return realEntrySet.contains(o);
43   }
44
45   public final boolean containsAll(Collection JavaDoc c) {
46     return realEntrySet.containsAll(c);
47   }
48
49   public final boolean equals(Object JavaDoc o) {
50     return realEntrySet.equals(o);
51   }
52
53   public final int hashCode() {
54     return realEntrySet.hashCode();
55   }
56
57   public final boolean isEmpty() {
58     return realEntrySet.isEmpty();
59   }
60
61   public Iterator JavaDoc iterator() {
62     return new IteratorWrapper(map, realEntrySet.iterator());
63   }
64
65   public boolean remove(Object JavaDoc o) {
66     return realEntrySet.remove(o);
67   }
68
69   public final boolean removeAll(Collection JavaDoc c) {
70     return realEntrySet.removeAll(c);
71   }
72
73   public final boolean retainAll(Collection JavaDoc c) {
74     return realEntrySet.retainAll(c);
75   }
76
77   public final int size() {
78     return realEntrySet.size();
79   }
80
81   public final Object JavaDoc[] toArray() {
82     return realEntrySet.toArray();
83   }
84
85   public final Object JavaDoc[] toArray(Object JavaDoc[] a) {
86     int size = size();
87     if (a.length < size) a = (Object JavaDoc[]) Array.newInstance(((Object JavaDoc) (a)).getClass().getComponentType(), size);
88
89     int index = 0;
90     for (Iterator JavaDoc iterator = iterator(); iterator.hasNext();) {
91       ManagerUtil.objectArrayChanged(a, index++, iterator.next());
92     }
93
94     if (a.length > size) {
95       a[size] = null;
96     }
97     return a;
98   }
99
100   private static class IteratorWrapper implements Iterator JavaDoc {
101
102     protected final Iterator JavaDoc realIterator;
103     protected final Map JavaDoc map;
104
105     IteratorWrapper(Map JavaDoc map, Iterator JavaDoc realIterator) {
106       this.map = map;
107       this.realIterator = realIterator;
108     }
109
110     public void remove() {
111       realIterator.remove();
112     }
113
114     public final boolean hasNext() {
115       boolean rv = realIterator.hasNext();
116       return rv;
117     }
118
119     public final Object JavaDoc next() {
120       return new EntryWrapper(map, (Entry) realIterator.next());
121     }
122
123   }
124
125   protected static class EntryWrapper implements Entry {
126     private final Object JavaDoc key;
127     private Object JavaDoc value;
128     private final Map JavaDoc map;
129
130     EntryWrapper(Map JavaDoc map, Entry entry) {
131       this.map = map;
132       this.key = entry.getKey();
133       this.value = entry.getValue();
134     }
135
136     public final boolean equals(Object JavaDoc o) {
137       if (!(o instanceof Entry)) { return false; }
138       Entry e2 = (Entry) o;
139       return (key == null ? e2.getKey() == null : key.equals(e2.getKey()))
140              && (value == null ? e2.getValue() == null : value.equals(e2.getValue()));
141     }
142
143     public final Object JavaDoc getKey() {
144       return key;
145     }
146
147     public final Object JavaDoc getValue() {
148       return value;
149     }
150
151     public final int hashCode() {
152       return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
153     }
154
155     public final Object JavaDoc setValue(Object JavaDoc value) {
156       Object JavaDoc rv = map.put(this.key, value);
157       this.value = value;
158       return rv;
159     }
160
161     public String JavaDoc toString() {
162       return key + "=" + value;
163     }
164   }
165
166 }
167
Popular Tags