Mercurial > projects > dwt2
comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/viewers/ViewerElementSet.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2008 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 215531) | |
10 * Matthew Hall - bug 124684 | |
11 ******************************************************************************/ | |
12 | |
13 module org.eclipse.jface.internal.databinding.viewers.ViewerElementSet; | |
14 | |
15 import java.lang.all; | |
16 | |
17 import java.lang.reflect.Array; | |
18 import java.util.Collection; | |
19 import java.util.HashSet; | |
20 import java.util.Iterator; | |
21 import java.util.Set; | |
22 | |
23 import org.eclipse.core.runtime.Assert; | |
24 import org.eclipse.jface.viewers.IElementComparer; | |
25 import org.eclipse.jface.viewers.StructuredViewer; | |
26 | |
27 /** | |
28 * A {@link Set} of elements in a {@link StructuredViewer}. Elements of the set | |
29 * are compared using an {@link IElementComparer} instead of | |
30 * {@link #equals(Object)}. | |
31 * <p> | |
32 * This class is <i>not</i> a strict implementation the {@link Set} interface. | |
33 * It intentionally violates the {@link Set} contract, which requires the use of | |
34 * {@link #equals(Object)} when comparing elements. This class is designed for | |
35 * use with {@link StructuredViewer} which uses {@link IElementComparer} for | |
36 * element comparisons. | |
37 * | |
38 * @since 1.2 | |
39 */ | |
40 public class ViewerElementSet : Set { | |
41 private final Set wrappedSet; | |
42 private final IElementComparer comparer; | |
43 | |
44 /** | |
45 * Constructs a ViewerElementSet using the given {@link IElementComparer}. | |
46 * | |
47 * @param comparer | |
48 * the {@link IElementComparer} used for comparing elements. | |
49 */ | |
50 public this(IElementComparer comparer) { | |
51 Assert.isNotNull(comparer); | |
52 this.wrappedSet = new HashSet(); | |
53 this.comparer = comparer; | |
54 } | |
55 | |
56 /** | |
57 * Constructs a ViewerElementSet containing all the elements in the | |
58 * specified collection. | |
59 * | |
60 * @param collection | |
61 * the collection whose elements are to be added to this set. | |
62 * @param comparer | |
63 * the {@link IElementComparer} used for comparing elements. | |
64 */ | |
65 public this(Collection collection, IElementComparer comparer) { | |
66 this(comparer); | |
67 addAll(collection); | |
68 } | |
69 | |
70 public bool add(Object o) { | |
71 return wrappedSet.add(new ViewerElementWrapper(o, comparer)); | |
72 } | |
73 | |
74 public bool addAll(Collection c) { | |
75 bool changed = false; | |
76 for (Iterator iterator = c.iterator(); iterator.hasNext();) | |
77 changed |= wrappedSet.add(new ViewerElementWrapper(iterator.next(), | |
78 comparer)); | |
79 return changed; | |
80 } | |
81 | |
82 public void clear() { | |
83 wrappedSet.clear(); | |
84 } | |
85 | |
86 public bool contains(Object o) { | |
87 return wrappedSet.contains(new ViewerElementWrapper(o, comparer)); | |
88 } | |
89 | |
90 public bool containsAll(Collection c) { | |
91 for (Iterator iterator = c.iterator(); iterator.hasNext();) | |
92 if (!wrappedSet.contains(new ViewerElementWrapper(iterator.next(), | |
93 comparer))) | |
94 return false; | |
95 return true; | |
96 } | |
97 | |
98 public bool isEmpty() { | |
99 return wrappedSet.isEmpty(); | |
100 } | |
101 | |
102 public Iterator iterator() { | |
103 final Iterator wrappedIterator = wrappedSet.iterator(); | |
104 return new class() Iterator { | |
105 public bool hasNext() { | |
106 return wrappedIterator.hasNext(); | |
107 } | |
108 | |
109 public Object next() { | |
110 return (cast(ViewerElementWrapper) wrappedIterator.next()).unwrap(); | |
111 } | |
112 | |
113 public void remove() { | |
114 wrappedIterator.remove(); | |
115 } | |
116 }; | |
117 } | |
118 | |
119 public bool remove(Object o) { | |
120 return wrappedSet.remove(new ViewerElementWrapper(o, comparer)); | |
121 } | |
122 | |
123 public bool removeAll(Collection c) { | |
124 bool changed = false; | |
125 for (Iterator iterator = c.iterator(); iterator.hasNext();) | |
126 changed |= remove(iterator.next()); | |
127 return changed; | |
128 } | |
129 | |
130 public bool retainAll(Collection c) { | |
131 // Have to do this the slow way to ensure correct comparisons. i.e. | |
132 // cannot delegate to c.contains(it) since we can't be sure will | |
133 // compare elements the way we want. | |
134 bool changed = false; | |
135 Object[] retainAll = c.toArray(); | |
136 outer: for (Iterator iterator = iterator(); iterator.hasNext();) { | |
137 Object element = iterator.next(); | |
138 for (int i = 0; i < retainAll.length; i++) { | |
139 if (comparer.equals(element, retainAll[i])) { | |
140 continue outer; | |
141 } | |
142 } | |
143 iterator.remove(); | |
144 changed = true; | |
145 } | |
146 return changed; | |
147 } | |
148 | |
149 public int size() { | |
150 return wrappedSet.size(); | |
151 } | |
152 | |
153 public Object[] toArray() { | |
154 return toArray(new Object[wrappedSet.size()]); | |
155 } | |
156 | |
157 public Object[] toArray(Object[] a) { | |
158 int size = wrappedSet.size(); | |
159 ViewerElementWrapper[] wrappedArray = cast(ViewerElementWrapper[]) wrappedSet | |
160 .toArray(new ViewerElementWrapper[size]); | |
161 Object[] result = a; | |
162 if (a.length < size) { | |
163 result = cast(Object[]) Array.newInstance(a.getClass() | |
164 .getComponentType(), size); | |
165 } | |
166 for (int i = 0; i < size; i++) | |
167 result[i] = wrappedArray[i].unwrap(); | |
168 return result; | |
169 } | |
170 | |
171 public bool equals(Object obj) { | |
172 if (obj is this) | |
173 return true; | |
174 if (!(null !is cast(Set)obj)) | |
175 return false; | |
176 Set that = cast(Set) obj; | |
177 return size() is that.size() && containsAll(that); | |
178 } | |
179 | |
180 public int hashCode() { | |
181 int hash = 0; | |
182 for (Iterator iterator = iterator(); iterator.hasNext();) { | |
183 Object element = iterator.next(); | |
184 hash += element is null ? 0 : element.hashCode(); | |
185 } | |
186 return hash; | |
187 } | |
188 | |
189 /** | |
190 * Returns a Set for holding viewer elements, using the given | |
191 * {@link IElementComparer} for comparisons. | |
192 * | |
193 * @param comparer | |
194 * the element comparer to use in element comparisons. If null, | |
195 * the returned set will compare elements according to the | |
196 * standard contract for {@link Set} interface contract. | |
197 * @return a Set for holding viewer elements, using the given | |
198 * {@link IElementComparer} for comparisons. | |
199 */ | |
200 public static Set withComparer(IElementComparer comparer) { | |
201 if (comparer is null) | |
202 return new HashSet(); | |
203 return new ViewerElementSet(comparer); | |
204 } | |
205 } |