Mercurial > projects > dwt2
comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/set/WritableSet.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 * Brad Reynolds - bug 147515 | |
11 * Matthew Hall - bug 221351 | |
12 *******************************************************************************/ | |
13 | |
14 module org.eclipse.core.databinding.observable.set.WritableSet; | |
15 | |
16 import java.lang.all; | |
17 | |
18 import java.util.Collection; | |
19 import java.util.Collections; | |
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 | |
27 /** | |
28 * Mutable (writable) implementation of {@link IObservableSet}. | |
29 * | |
30 * <p> | |
31 * This class is thread safe. All state accessing methods must be invoked from | |
32 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing | |
33 * listeners may be invoked from any thread. | |
34 * </p> | |
35 * | |
36 * @since 1.0 | |
37 */ | |
38 public class WritableSet : ObservableSet { | |
39 | |
40 /** | |
41 * Constructs a new empty instance in the default realm with a | |
42 * <code>null</code> element type. | |
43 * | |
44 */ | |
45 public this() { | |
46 this(Realm.getDefault()); | |
47 } | |
48 | |
49 /** | |
50 * Constructs a new instance in the default realm containing the | |
51 * elements of the given collection. Changes to the given collection after | |
52 * calling this method do not affect the contents of the created WritableSet. | |
53 * | |
54 * @param c | |
55 * @param elementType | |
56 * can be <code>null</code> | |
57 */ | |
58 public this(Collection c, Object elementType) { | |
59 this(Realm.getDefault(), new HashSet(c), elementType); | |
60 } | |
61 | |
62 /** | |
63 * Constructs a new empty instance in the given realm and a | |
64 * <code>null</code> element type. | |
65 * | |
66 * @param realm | |
67 */ | |
68 public this(Realm realm) { | |
69 this(realm, new HashSet(), null); | |
70 } | |
71 | |
72 /** | |
73 * Constructs a new instance in the default realm with the given element | |
74 * type, containing the elements of the given collection. Changes to the | |
75 * given collection after calling this method do not affect the contents of | |
76 * the created WritableSet. | |
77 * | |
78 * @param realm | |
79 * @param c | |
80 * @param elementType | |
81 * can be <code>null</code> | |
82 */ | |
83 public this(Realm realm, Collection c, Object elementType) { | |
84 super(realm, new HashSet(c), elementType); | |
85 this.elementType = elementType; | |
86 } | |
87 | |
88 public bool add(Object o) { | |
89 getterCalled(); | |
90 bool added = wrappedSet.add(o); | |
91 if (added) { | |
92 fireSetChange(Diffs.createSetDiff(Collections.singleton(o), Collections.EMPTY_SET)); | |
93 } | |
94 return added; | |
95 } | |
96 | |
97 public bool addAll(Collection c) { | |
98 getterCalled(); | |
99 Set additions = new HashSet(); | |
100 Iterator it = c.iterator(); | |
101 while (it.hasNext()) { | |
102 Object element = it.next(); | |
103 if (wrappedSet.add(element)) { | |
104 additions.add(element); | |
105 } | |
106 } | |
107 if (additions.size() > 0) { | |
108 fireSetChange(Diffs.createSetDiff(additions, Collections.EMPTY_SET)); | |
109 return true; | |
110 } | |
111 return false; | |
112 } | |
113 | |
114 public bool remove(Object o) { | |
115 getterCalled(); | |
116 bool removed = wrappedSet.remove(o); | |
117 if (removed) { | |
118 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, Collections | |
119 .singleton(o))); | |
120 } | |
121 return removed; | |
122 } | |
123 | |
124 public bool removeAll(Collection c) { | |
125 getterCalled(); | |
126 Set removes = new HashSet(); | |
127 Iterator it = c.iterator(); | |
128 while (it.hasNext()) { | |
129 Object element = it.next(); | |
130 if (wrappedSet.remove(element)) { | |
131 removes.add(element); | |
132 } | |
133 } | |
134 if (removes.size() > 0) { | |
135 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); | |
136 return true; | |
137 } | |
138 return false; | |
139 } | |
140 | |
141 public bool retainAll(Collection c) { | |
142 getterCalled(); | |
143 Set removes = new HashSet(); | |
144 Iterator it = wrappedSet.iterator(); | |
145 while (it.hasNext()) { | |
146 Object element = it.next(); | |
147 if (!c.contains(element)) { | |
148 it.remove(); | |
149 removes.add(element); | |
150 } | |
151 } | |
152 if (removes.size() > 0) { | |
153 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); | |
154 return true; | |
155 } | |
156 return false; | |
157 } | |
158 | |
159 public void clear() { | |
160 getterCalled(); | |
161 Set removes = new HashSet(wrappedSet); | |
162 wrappedSet.clear(); | |
163 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); | |
164 } | |
165 | |
166 /** | |
167 * @param elementType can be <code>null</code> | |
168 * @return new instance with the default realm | |
169 */ | |
170 public static WritableSet withElementType(Object elementType) { | |
171 return new WritableSet(Realm.getDefault(), new HashSet(), elementType); | |
172 } | |
173 } |