78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Matthew Hall - bug 208332
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.databinding.observable.set.UnionSet;
|
81
|
14 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
15 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
16 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
17 import org.eclipse.core.databinding.observable.set.ObservableSet;
|
78
|
18
|
|
19 import java.lang.all;
|
|
20
|
|
21 import java.util.ArrayList;
|
|
22 import java.util.Collection;
|
|
23 import java.util.HashMap;
|
|
24 import java.util.HashSet;
|
|
25 import java.util.Iterator;
|
|
26 import java.util.Set;
|
|
27
|
|
28 import org.eclipse.core.databinding.observable.Diffs;
|
|
29 import org.eclipse.core.databinding.observable.Realm;
|
|
30 import org.eclipse.core.internal.databinding.observable.IStalenessConsumer;
|
|
31 import org.eclipse.core.internal.databinding.observable.StalenessTracker;
|
|
32
|
|
33 /**
|
|
34 * Represents a set consisting of the union of elements from one or more other
|
|
35 * sets. This object does not need to be explicitly disposed. If nobody is
|
|
36 * listening to the UnionSet, the set will remove its listeners.
|
|
37 *
|
|
38 * <p>
|
|
39 * This class is thread safe. All state accessing methods must be invoked from
|
|
40 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
41 * listeners may be invoked from any thread.
|
|
42 * </p>
|
|
43 *
|
|
44 * @since 1.0
|
|
45 */
|
|
46 public final class UnionSet : ObservableSet {
|
|
47
|
|
48 /**
|
|
49 * child sets
|
|
50 */
|
|
51 private IObservableSet[] childSets;
|
|
52
|
|
53 private bool stale = false;
|
|
54
|
|
55 /**
|
|
56 * Map of elements onto Integer reference counts. This map is constructed
|
|
57 * when the first listener is added to the union set. Null if nobody is
|
|
58 * listening to the UnionSet.
|
|
59 */
|
|
60 private HashMap refCounts = null;
|
|
61
|
|
62 private StalenessTracker stalenessTracker;
|
|
63
|
|
64 /**
|
|
65 * @param childSets
|
|
66 */
|
|
67 public this(IObservableSet[] childSets) {
|
|
68 super(childSets[0].getRealm(), null, childSets[0].getElementType());
|
|
69 System.arraycopy(childSets, 0, this.childSets = new IObservableSet[childSets.length], 0, childSets.length);
|
|
70 this.stalenessTracker = new StalenessTracker(childSets,
|
|
71 stalenessConsumer);
|
|
72 }
|
|
73
|
|
74 private ISetChangeListener childSetChangeListener = new class() ISetChangeListener {
|
|
75 public void handleSetChange(SetChangeEvent event) {
|
|
76 processAddsAndRemoves(event.diff.getAdditions(), event.diff.getRemovals());
|
|
77 }
|
|
78 };
|
|
79
|
|
80 private IStalenessConsumer stalenessConsumer = new class() IStalenessConsumer {
|
|
81 public void setStale(bool stale) {
|
|
82 bool oldStale = this.outer.stale;
|
|
83 this.outer.stale = stale;
|
|
84 if (stale && !oldStale) {
|
|
85 fireStale();
|
|
86 }
|
|
87 }
|
|
88 };
|
|
89
|
|
90 public bool isStale() {
|
|
91 getterCalled();
|
|
92 if (refCounts !is null) {
|
|
93 return stale;
|
|
94 }
|
|
95
|
|
96 for (int i = 0; i < childSets.length; i++) {
|
|
97 IObservableSet childSet = childSets[i];
|
|
98
|
|
99 if (childSet.isStale()) {
|
|
100 return true;
|
|
101 }
|
|
102 }
|
|
103 return false;
|
|
104 }
|
|
105
|
|
106 private void processAddsAndRemoves(Set adds, Set removes) {
|
|
107 Set addsToFire = new HashSet();
|
|
108 Set removesToFire = new HashSet();
|
|
109
|
|
110 for (Iterator iter = adds.iterator(); iter.hasNext();) {
|
|
111 Object added = iter.next();
|
|
112
|
|
113 Integer refCount = cast(Integer) refCounts.get(added);
|
|
114 if (refCount is null) {
|
|
115 refCounts.put(added, new Integer(1));
|
|
116 addsToFire.add(added);
|
|
117 } else {
|
|
118 int refs = refCount.intValue();
|
|
119 refCount = new Integer(refs + 1);
|
|
120 refCounts.put(added, refCount);
|
|
121 }
|
|
122 }
|
|
123
|
|
124 for (Iterator iter = removes.iterator(); iter.hasNext();) {
|
|
125 Object removed = iter.next();
|
|
126
|
|
127 Integer refCount = cast(Integer) refCounts.get(removed);
|
|
128 if (refCount !is null) {
|
|
129 int refs = refCount.intValue();
|
|
130 if (refs <= 1) {
|
|
131 removesToFire.add(removed);
|
|
132 refCounts.remove(removed);
|
|
133 } else {
|
|
134 refCount = new Integer(refCount.intValue() - 1);
|
|
135 refCounts.put(removed, refCount);
|
|
136 }
|
|
137 }
|
|
138 }
|
|
139
|
|
140 // just in case the removes overlapped with the adds
|
|
141 addsToFire.removeAll(removesToFire);
|
|
142
|
|
143 if (addsToFire.size() > 0 || removesToFire.size() > 0) {
|
|
144 fireSetChange(Diffs.createSetDiff(addsToFire, removesToFire));
|
|
145 }
|
|
146 }
|
|
147
|
|
148 protected void firstListenerAdded() {
|
|
149 super.firstListenerAdded();
|
|
150
|
|
151 refCounts = new HashMap();
|
|
152 for (int i = 0; i < childSets.length; i++) {
|
|
153 IObservableSet next = childSets[i];
|
|
154 next.addSetChangeListener(childSetChangeListener);
|
|
155 incrementRefCounts(next);
|
|
156 }
|
|
157 stalenessTracker = new StalenessTracker(childSets, stalenessConsumer);
|
|
158 setWrappedSet(refCounts.keySet());
|
|
159 }
|
|
160
|
|
161 protected void lastListenerRemoved() {
|
|
162 super.lastListenerRemoved();
|
|
163
|
|
164 for (int i = 0; i < childSets.length; i++) {
|
|
165 IObservableSet next = childSets[i];
|
|
166
|
|
167 next.removeSetChangeListener(childSetChangeListener);
|
|
168 stalenessTracker.removeObservable(next);
|
|
169 }
|
|
170 refCounts = null;
|
|
171 stalenessTracker = null;
|
|
172 setWrappedSet(null);
|
|
173 }
|
|
174
|
|
175 private ArrayList incrementRefCounts(Collection added) {
|
|
176 ArrayList adds = new ArrayList();
|
|
177
|
|
178 for (Iterator iter = added.iterator(); iter.hasNext();) {
|
|
179 Object next = iter.next();
|
|
180
|
|
181 Integer refCount = cast(Integer) refCounts.get(next);
|
|
182 if (refCount is null) {
|
|
183 adds.add(next);
|
|
184 refCount = new Integer(1);
|
|
185 refCounts.put(next, refCount);
|
|
186 } else {
|
|
187 refCount = new Integer(refCount.intValue() + 1);
|
|
188 refCounts.put(next, refCount);
|
|
189 }
|
|
190 }
|
|
191 return adds;
|
|
192 }
|
|
193
|
|
194 protected void getterCalled() {
|
|
195 super.getterCalled();
|
|
196 if (refCounts is null) {
|
|
197 // no listeners, recompute
|
|
198 setWrappedSet(computeElements());
|
|
199 }
|
|
200 }
|
|
201
|
|
202 private Set computeElements() {
|
|
203 // If there is no cached value, compute the union from scratch
|
|
204 if (refCounts is null) {
|
|
205 Set result = new HashSet();
|
|
206 for (int i = 0; i < childSets.length; i++) {
|
|
207 result.addAll(childSets[i]);
|
|
208 }
|
|
209 return result;
|
|
210 }
|
|
211
|
|
212 // Else there is a cached value. Return it.
|
|
213 return refCounts.keySet();
|
|
214 }
|
|
215
|
|
216 }
|