KickJava   Java API By Example, From Geeks To Geeks.

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


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.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
14 public class THashMapCollectionWrapper implements Set JavaDoc {
15
16   private final Collection JavaDoc realValues;
17   private final Map JavaDoc map;
18
19   public THashMapCollectionWrapper(Map JavaDoc map, Collection JavaDoc realValues) {
20     this.map = map;
21     this.realValues = realValues;
22   }
23
24   public boolean add(Object JavaDoc o) {
25     return realValues.add(o);
26   }
27
28   public boolean addAll(Collection JavaDoc c) {
29     return realValues.addAll(c);
30   }
31
32   public void clear() {
33     realValues.clear();
34   }
35
36   public boolean contains(Object JavaDoc o) {
37     return realValues.contains(o);
38   }
39
40   public boolean containsAll(Collection JavaDoc c) {
41     return realValues.containsAll(c);
42   }
43
44   public boolean equals(Object JavaDoc o) {
45     return realValues.equals(o);
46   }
47
48   public int hashCode() {
49     return realValues.hashCode();
50   }
51
52   public boolean isEmpty() {
53     return realValues.isEmpty();
54   }
55
56   public Iterator JavaDoc iterator() {
57     return new IteratorWrapper(map, realValues.iterator());
58   }
59
60   public boolean remove(Object JavaDoc o) {
61     ManagerUtil.checkWriteAccess(map);
62     return realValues.remove(o);
63   }
64
65   public boolean removeAll(Collection JavaDoc c) {
66     return realValues.removeAll(c);
67   }
68
69   public boolean retainAll(Collection JavaDoc c) {
70     return realValues.retainAll(c);
71   }
72
73   public int size() {
74     return realValues.size();
75   }
76
77   public Object JavaDoc[] toArray() {
78     return realValues.toArray();
79   }
80
81   public Object JavaDoc[] toArray(Object JavaDoc[] a) {
82     int size = size();
83     if (a.length < size) a = (Object JavaDoc[]) Array.newInstance(((Object JavaDoc) (a)).getClass().getComponentType(), size);
84
85     int index = 0;
86     for (Iterator JavaDoc iterator = iterator(); iterator.hasNext();) {
87       ManagerUtil.objectArrayChanged(a, index++, iterator.next());
88     }
89
90     if (a.length > size) {
91       a[size] = null;
92     }
93     return a;
94   }
95
96   private static class IteratorWrapper implements Iterator JavaDoc {
97     private final Iterator JavaDoc realIterator;
98     private final Map JavaDoc map;
99     private Object JavaDoc lastValue;
100
101     public IteratorWrapper(Map JavaDoc map, Iterator JavaDoc realIterator) {
102       this.map = map;
103       this.realIterator = realIterator;
104     }
105
106     public boolean hasNext() {
107       return realIterator.hasNext();
108     }
109
110     public Object JavaDoc next() {
111       lastValue = realIterator.next();
112       return lastValue;
113     }
114
115     public void remove() {
116       ManagerUtil.checkWriteAccess(map);
117       realIterator.remove();
118     }
119   }
120
121 }
Popular Tags