001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tcclient.util;
005:
006: import com.tc.object.bytecode.ManagerUtil;
007:
008: import java.lang.reflect.Array;
009: import java.util.Collection;
010: import java.util.Iterator;
011: import java.util.Map;
012: import java.util.Set;
013: import java.util.Map.Entry;
014:
015: /**
016: * A wrapper for Map.entrySet() that keeps DSO informed of changes
017: */
018: @SuppressWarnings("unchecked")
019: public class ConcurrentHashMapEntrySetWrapper implements Set {
020: public final static String CLASS_SLASH = "com/tc/util/ConcurrentHashMapEntrySetWrapper";
021:
022: protected final Set realEntrySet;
023: protected final Map map;
024:
025: public ConcurrentHashMapEntrySetWrapper(Map map, Set realEntrySet) {
026: this .realEntrySet = realEntrySet;
027: this .map = map;
028: }
029:
030: public Set getDelegateEntrySet() {
031: return realEntrySet;
032: }
033:
034: public final boolean add(Object o) {
035: return realEntrySet.add(o);
036: }
037:
038: public final boolean addAll(Collection c) {
039: return realEntrySet.addAll(c);
040: }
041:
042: public final void clear() {
043: realEntrySet.clear();
044: }
045:
046: public final boolean contains(Object o) {
047: return realEntrySet.contains(o);
048: }
049:
050: public final boolean containsAll(Collection c) {
051: return realEntrySet.containsAll(c);
052: }
053:
054: public final boolean equals(Object o) {
055: return realEntrySet.equals(o);
056: }
057:
058: public final int hashCode() {
059: return realEntrySet.hashCode();
060: }
061:
062: public final boolean isEmpty() {
063: return realEntrySet.isEmpty();
064: }
065:
066: public Iterator iterator() {
067: return new IteratorWrapper(map, realEntrySet.iterator());
068: }
069:
070: public boolean remove(Object o) {
071: return realEntrySet.remove(o);
072: }
073:
074: public final boolean removeAll(Collection c) {
075: return realEntrySet.removeAll(c);
076: }
077:
078: public final boolean retainAll(Collection c) {
079: return realEntrySet.retainAll(c);
080: }
081:
082: public final int size() {
083: return realEntrySet.size();
084: }
085:
086: public final Object[] toArray() {
087: return realEntrySet.toArray();
088: }
089:
090: public final Object[] toArray(Object[] a) {
091: int size = size();
092: if (a.length < size)
093: a = (Object[]) Array.newInstance(((Object) (a)).getClass()
094: .getComponentType(), size);
095:
096: int index = 0;
097: for (Iterator iterator = iterator(); iterator.hasNext();) {
098: ManagerUtil.objectArrayChanged(a, index++, iterator.next());
099: }
100:
101: if (a.length > size) {
102: a[size] = null;
103: }
104: return a;
105: }
106:
107: private static class IteratorWrapper implements Iterator {
108:
109: protected final Iterator realIterator;
110: protected final Map map;
111:
112: IteratorWrapper(Map map, Iterator realIterator) {
113: this .map = map;
114: this .realIterator = realIterator;
115: }
116:
117: public void remove() {
118: realIterator.remove();
119: }
120:
121: public final boolean hasNext() {
122: boolean rv = realIterator.hasNext();
123: return rv;
124: }
125:
126: public final Object next() {
127: return new EntryWrapper(map, (Entry) realIterator.next());
128: }
129: }
130:
131: public static class EntryWrapper implements Entry {
132: private final Object key;
133: private Object value;
134: private final Map map;
135:
136: public EntryWrapper(Map map, Entry entry) {
137: this .map = map;
138: this .key = entry.getKey();
139: this .value = entry.getValue();
140: }
141:
142: public final boolean equals(Object o) {
143: if (!(o instanceof Entry)) {
144: return false;
145: }
146: Entry e2 = (Entry) o;
147: return (key == null ? e2.getKey() == null : key.equals(e2
148: .getKey()))
149: && (value == null ? e2.getValue() == null : value
150: .equals(e2.getValue()));
151: }
152:
153: public final Object getKey() {
154: return key;
155: }
156:
157: public final Object getValue() {
158: return value;
159: }
160:
161: public final int hashCode() {
162: return (key == null ? 0 : key.hashCode())
163: ^ (value == null ? 0 : value.hashCode());
164: }
165:
166: public final Object setValue(Object value) {
167: Object rv = map.put(this .key, value);
168: this .value = value;
169: return rv;
170: }
171:
172: public String toString() {
173: return key + "=" + value;
174: }
175: }
176: }
|