comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/set/UnionSet.d @ 78:0a55d2d5a946

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