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.SerializationUtil;
007: import com.tc.object.bytecode.ManagerUtil;
008: import com.tc.util.SetIteratorWrapper;
009:
010: import java.lang.reflect.Array;
011: import java.util.Collection;
012: import java.util.Comparator;
013: import java.util.Iterator;
014: import java.util.SortedSet;
015:
016: /**
017: * A wrapper for TreeSet.subSet(), TreeSet.headSet(), and TreeSet.tailSet() that keeps DSO informed of changes.
018: */
019: public final class SortedViewSetWrapper implements SortedSet {
020: public final static String CLASS_SLASH = "com/tcclient/util/SortedViewSetWrapper";
021:
022: // viewSet is transient and is re-constructed from oringalSet,
023: private transient SortedSet viewSet;
024: // fromKey, toKey, and head.
025: private SortedSet originalSet;
026:
027: private Object fromKey;
028: private Object toKey;
029: private boolean head;
030:
031: public SortedViewSetWrapper(SortedSet originalSet,
032: SortedSet viewSet, Object fromKey, Object toKey) {
033: this .originalSet = originalSet;
034: this .viewSet = viewSet;
035: this .fromKey = fromKey;
036: this .toKey = toKey;
037: }
038:
039: public SortedViewSetWrapper(SortedSet originalSet,
040: SortedSet viewSet, Object key, boolean head) {
041: this .originalSet = originalSet;
042: this .viewSet = viewSet;
043: this .head = head;
044: if (head) {
045: this .toKey = key;
046: } else {
047: this .fromKey = key;
048: }
049: }
050:
051: private SortedSet getViewSet() {
052: if (viewSet == null) {
053: if (head) {
054: viewSet = ((SortedViewSetWrapper) originalSet
055: .headSet(toKey)).viewSet;
056: } else if (toKey == null) {
057: viewSet = ((SortedViewSetWrapper) originalSet
058: .tailSet(fromKey)).viewSet;
059: } else {
060: viewSet = ((SortedViewSetWrapper) originalSet.subSet(
061: fromKey, toKey)).viewSet;
062: }
063: }
064: return viewSet;
065: }
066:
067: public final boolean add(Object o) {
068: ManagerUtil.checkWriteAccess(originalSet);
069: boolean flag = getViewSet().add(o);
070: if (flag) {
071: ManagerUtil
072: .logicalInvoke(originalSet,
073: SerializationUtil.ADD_SIGNATURE,
074: new Object[] { o });
075: }
076:
077: return flag;
078: }
079:
080: public final boolean addAll(Collection c) {
081: ManagerUtil.checkWriteAccess(originalSet);
082: boolean flag = getViewSet().addAll(c);
083: if (flag) {
084: ManagerUtil.logicalInvoke(originalSet,
085: SerializationUtil.ADD_ALL_SIGNATURE,
086: new Object[] { c });
087: }
088:
089: return flag;
090: }
091:
092: public final void clear() {
093: ManagerUtil.checkWriteAccess(originalSet);
094: Object[] clearSet = getViewSet().toArray();
095: getViewSet().clear();
096: ManagerUtil.logicalInvoke(originalSet,
097: SerializationUtil.REMOVE_ALL_SIGNATURE, clearSet);
098: }
099:
100: public final boolean contains(Object o) {
101: return getViewSet().contains(o);
102: }
103:
104: public final boolean containsAll(Collection c) {
105: return getViewSet().containsAll(c);
106: }
107:
108: public final boolean equals(Object o) {
109: return getViewSet().equals(o);
110: }
111:
112: public final int hashCode() {
113: return getViewSet().hashCode();
114: }
115:
116: public final boolean isEmpty() {
117: return getViewSet().isEmpty();
118: }
119:
120: public final Iterator iterator() {
121: return new SetIteratorWrapper(getViewSet().iterator(),
122: originalSet);
123: }
124:
125: public final boolean remove(Object o) {
126: ManagerUtil.checkWriteAccess(originalSet);
127: boolean removed = getViewSet().remove(o);
128: if (removed) {
129: ManagerUtil.logicalInvoke(originalSet,
130: SerializationUtil.REMOVE_SIGNATURE,
131: new Object[] { o });
132: }
133: return removed;
134: }
135:
136: public final boolean removeAll(Collection c) {
137: boolean modified = false;
138:
139: if (size() > c.size()) {
140: for (Iterator i = c.iterator(); i.hasNext();)
141: modified |= remove(i.next());
142: } else {
143: for (Iterator i = iterator(); i.hasNext();) {
144: if (c.contains(i.next())) {
145: i.remove();
146: modified = true;
147: }
148: }
149: }
150: return modified;
151: }
152:
153: public final boolean retainAll(Collection c) {
154: boolean modified = false;
155: Iterator i = iterator();
156: while (i.hasNext()) {
157: if (!c.contains(i.next())) {
158: i.remove();
159: modified = true;
160: }
161: }
162: return modified;
163:
164: }
165:
166: public final int size() {
167: return getViewSet().size();
168: }
169:
170: public final Object[] toArray() {
171: return getViewSet().toArray();
172: }
173:
174: public final Object[] toArray(Object[] a) {
175: int size = size();
176: if (a.length < size)
177: a = (Object[]) Array.newInstance(((Object) (a)).getClass()
178: .getComponentType(), size);
179:
180: int index = 0;
181: for (Iterator iterator = iterator(); iterator.hasNext();) {
182: ManagerUtil.objectArrayChanged(a, index++, iterator.next());
183: }
184:
185: if (a.length > size) {
186: a[size] = null;
187: }
188: return a;
189: }
190:
191: public final Comparator comparator() {
192: return getViewSet().comparator();
193: }
194:
195: public final SortedSet subSet(Object fromElement, Object toElement) {
196: return new SortedViewSetWrapper(originalSet,
197: ((SortedViewSetWrapper) viewSet.subSet(fromElement,
198: toElement)).viewSet, fromElement, toElement);
199: }
200:
201: public final SortedSet headSet(Object toElement) {
202: return new SortedViewSetWrapper(
203: originalSet,
204: ((SortedViewSetWrapper) viewSet.headSet(toElement)).viewSet,
205: toElement, true);
206: }
207:
208: public final SortedSet tailSet(Object fromElement) {
209: return new SortedViewSetWrapper(
210: originalSet,
211: ((SortedViewSetWrapper) viewSet.tailSet(fromElement)).viewSet,
212: fromElement, false);
213: }
214:
215: public final Object first() {
216: return getViewSet().first();
217: }
218:
219: public final Object last() {
220: return getViewSet().last();
221: }
222:
223: public String toString() {
224: return viewSet.toString();
225: }
226: }
|