KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.tc.util.SetIteratorWrapper;
9
10 import java.lang.reflect.Array JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Comparator JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.SortedSet JavaDoc;
15
16 /**
17  * A wrapper for TreeSet.subSet(), TreeSet.headSet(), and TreeSet.tailSet() that keeps DSO informed of changes.
18  */

19 public final class SortedViewSetWrapper implements SortedSet JavaDoc {
20   public final static String JavaDoc CLASS_SLASH = "com/tcclient/util/SortedViewSetWrapper";
21
22   // viewSet is transient and is re-constructed from oringalSet,
23
private transient SortedSet JavaDoc viewSet;
24   // fromKey, toKey, and head.
25
private SortedSet JavaDoc originalSet;
26
27   private Object JavaDoc fromKey;
28   private Object JavaDoc toKey;
29   private boolean head;
30
31   public SortedViewSetWrapper(SortedSet JavaDoc originalSet, SortedSet JavaDoc viewSet, Object JavaDoc fromKey, Object JavaDoc toKey) {
32     this.originalSet = originalSet;
33     this.viewSet = viewSet;
34     this.fromKey = fromKey;
35     this.toKey = toKey;
36   }
37
38   public SortedViewSetWrapper(SortedSet JavaDoc originalSet, SortedSet JavaDoc viewSet, Object JavaDoc key, boolean head) {
39     this.originalSet = originalSet;
40     this.viewSet = viewSet;
41     this.head = head;
42     if (head) {
43       this.toKey = key;
44     } else {
45       this.fromKey = key;
46     }
47   }
48
49   private SortedSet JavaDoc getViewSet() {
50     if (viewSet == null) {
51       if (head) {
52         viewSet = ((SortedViewSetWrapper) originalSet.headSet(toKey)).viewSet;
53       } else if (toKey == null) {
54         viewSet = ((SortedViewSetWrapper) originalSet.tailSet(fromKey)).viewSet;
55       } else {
56         viewSet = ((SortedViewSetWrapper) originalSet.subSet(fromKey, toKey)).viewSet;
57       }
58     }
59     return viewSet;
60   }
61
62   public final boolean add(Object JavaDoc o) {
63     ManagerUtil.checkWriteAccess(originalSet);
64     boolean flag = getViewSet().add(o);
65     if (flag) {
66       ManagerUtil.logicalInvoke(originalSet, SerializationUtil.ADD_SIGNATURE, new Object JavaDoc[] { o });
67     }
68
69     return flag;
70   }
71
72   public final boolean addAll(Collection JavaDoc c) {
73     ManagerUtil.checkWriteAccess(originalSet);
74     boolean flag = getViewSet().addAll(c);
75     if (flag) {
76       ManagerUtil.logicalInvoke(originalSet, SerializationUtil.ADD_ALL_SIGNATURE, new Object JavaDoc[] { c });
77     }
78
79     return flag;
80   }
81
82   public final void clear() {
83     ManagerUtil.checkWriteAccess(originalSet);
84     Object JavaDoc[] clearSet = getViewSet().toArray();
85     getViewSet().clear();
86     ManagerUtil.logicalInvoke(originalSet, SerializationUtil.REMOVE_ALL_SIGNATURE, clearSet);
87   }
88
89   public final boolean contains(Object JavaDoc o) {
90     return getViewSet().contains(o);
91   }
92
93   public final boolean containsAll(Collection JavaDoc c) {
94     return getViewSet().containsAll(c);
95   }
96
97   public final boolean equals(Object JavaDoc o) {
98     return getViewSet().equals(o);
99   }
100
101   public final int hashCode() {
102     return getViewSet().hashCode();
103   }
104
105   public final boolean isEmpty() {
106     return getViewSet().isEmpty();
107   }
108
109   public final Iterator JavaDoc iterator() {
110     return new SetIteratorWrapper(getViewSet().iterator(), originalSet);
111   }
112
113   public final boolean remove(Object JavaDoc o) {
114     ManagerUtil.checkWriteAccess(originalSet);
115     boolean removed = getViewSet().remove(o);
116     if (removed) {
117       ManagerUtil.logicalInvoke(originalSet, SerializationUtil.REMOVE_SIGNATURE, new Object JavaDoc[] { o });
118     }
119     return removed;
120   }
121
122   public final boolean removeAll(Collection JavaDoc c) {
123     boolean modified = false;
124
125     if (size() > c.size()) {
126       for (Iterator JavaDoc i = c.iterator(); i.hasNext();)
127         modified |= remove(i.next());
128     } else {
129       for (Iterator JavaDoc i = iterator(); i.hasNext();) {
130         if (c.contains(i.next())) {
131           i.remove();
132           modified = true;
133         }
134       }
135     }
136     return modified;
137   }
138
139   public final boolean retainAll(Collection JavaDoc c) {
140     boolean modified = false;
141     Iterator JavaDoc i = iterator();
142     while (i.hasNext()) {
143       if (!c.contains(i.next())) {
144         i.remove();
145         modified = true;
146       }
147     }
148     return modified;
149
150   }
151
152   public final int size() {
153     return getViewSet().size();
154   }
155
156   public final Object JavaDoc[] toArray() {
157     return getViewSet().toArray();
158   }
159
160   public final Object JavaDoc[] toArray(Object JavaDoc[] a) {
161     int size = size();
162     if (a.length < size) a = (Object JavaDoc[]) Array.newInstance(((Object JavaDoc) (a)).getClass().getComponentType(), size);
163
164     int index = 0;
165     for (Iterator JavaDoc iterator = iterator(); iterator.hasNext();) {
166       ManagerUtil.objectArrayChanged(a, index++, iterator.next());
167     }
168
169     if (a.length > size) {
170       a[size] = null;
171     }
172     return a;
173   }
174
175   public final Comparator JavaDoc comparator() {
176     return getViewSet().comparator();
177   }
178
179   public final SortedSet JavaDoc subSet(Object JavaDoc fromElement, Object JavaDoc toElement) {
180     return new SortedViewSetWrapper(originalSet,
181                                     ((SortedViewSetWrapper) viewSet.subSet(fromElement, toElement)).viewSet,
182                                     fromElement, toElement);
183   }
184
185   public final SortedSet JavaDoc headSet(Object JavaDoc toElement) {
186     return new SortedViewSetWrapper(originalSet, ((SortedViewSetWrapper) viewSet.headSet(toElement)).viewSet,
187                                     toElement, true);
188   }
189
190   public final SortedSet JavaDoc tailSet(Object JavaDoc fromElement) {
191     return new SortedViewSetWrapper(originalSet, ((SortedViewSetWrapper) viewSet.tailSet(fromElement)).viewSet,
192                                     fromElement, false);
193   }
194
195   public final Object JavaDoc first() {
196     return getViewSet().first();
197   }
198
199   public final Object JavaDoc last() {
200     return getViewSet().last();
201   }
202
203   public String JavaDoc toString() {
204     return viewSet.toString();
205   }
206 }
207
Popular Tags