1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.qedeq.kernel.dto.list; |
19 |
| |
20 |
| import java.util.Arrays; |
21 |
| import java.util.HashSet; |
22 |
| import java.util.Iterator; |
23 |
| import java.util.Set; |
24 |
| |
25 |
| import org.qedeq.kernel.base.list.Element; |
26 |
| import org.qedeq.kernel.base.list.ElementList; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public final class ElementSet { |
36 |
| |
37 |
| |
38 |
| |
39 |
| private final Set elements; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
33294
| public ElementSet() {
|
46 |
33294
| this.elements = new HashSet();
|
47 |
| } |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
0
| public ElementSet(final Element[] elements) {
|
57 |
0
| if (elements == null) {
|
58 |
0
| throw new IllegalArgumentException(
|
59 |
| "NullPointer as element array is not allowed"); |
60 |
| } |
61 |
0
| this.elements = new HashSet(Arrays.asList(elements));
|
62 |
| } |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
6231
| public ElementSet(final ElementSet set) {
|
73 |
6231
| if (set == null) {
|
74 |
0
| throw new IllegalArgumentException(
|
75 |
| "NullPointer as set is not allowed"); |
76 |
| } |
77 |
6231
| this.elements = new HashSet(set.elements);
|
78 |
| } |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
0
| public ElementSet(final ElementList element) {
|
91 |
0
| if (element == null) {
|
92 |
0
| throw new IllegalArgumentException(
|
93 |
| "NullPointer as element is not allowed"); |
94 |
| } |
95 |
0
| if (element.isAtom()) {
|
96 |
0
| throw new IllegalArgumentException(
|
97 |
| "text as element is not allowed"); |
98 |
| } |
99 |
0
| this.elements = new HashSet(element.getElements());
|
100 |
| } |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
394
| public final boolean contains(final Element element) {
|
112 |
394
| if (element == null) {
|
113 |
0
| throw new IllegalArgumentException("NullPointer as element is not allowed");
|
114 |
| } |
115 |
394
| return this.elements.contains(element);
|
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
6231
| public final boolean isEmpty() {
|
125 |
6231
| return elements.isEmpty();
|
126 |
| } |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
0
| public final boolean isSubset(final ElementSet set) {
|
137 |
0
| if (set == null) {
|
138 |
0
| throw new IllegalArgumentException("NullPointer as set is not allowed");
|
139 |
| } |
140 |
0
| return this.elements.containsAll(set.elements);
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
4856
| public final ElementSet add(final Element element) {
|
154 |
4856
| if (element == null) {
|
155 |
0
| throw new IllegalArgumentException("NullPointer as element is not allowed");
|
156 |
| } |
157 |
4856
| elements.add(element);
|
158 |
4856
| return this;
|
159 |
| } |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
28796
| public final ElementSet union(final ElementSet set) {
|
172 |
28796
| if (set == null) {
|
173 |
0
| throw new IllegalArgumentException(
|
174 |
| "NullPointer as set is not allowed"); |
175 |
| } |
176 |
28796
| elements.addAll(set.elements);
|
177 |
28796
| return this;
|
178 |
| } |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
302
| public final ElementSet remove(final Element element) {
|
190 |
302
| if (element == null) {
|
191 |
0
| throw new IllegalArgumentException(
|
192 |
| "NullPointer as element is not allowed"); |
193 |
| } |
194 |
302
| elements.remove(element);
|
195 |
302
| return this;
|
196 |
| } |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
0
| public final ElementSet minus(final ElementSet set) {
|
211 |
0
| if (set == null) {
|
212 |
0
| throw new IllegalArgumentException(
|
213 |
| "NullPointer as set is not allowed"); |
214 |
| } |
215 |
0
| this.elements.removeAll(set.elements);
|
216 |
0
| return this;
|
217 |
| } |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
0
| public final ElementSet intersection(final ElementSet set) {
|
229 |
0
| if (set == null) {
|
230 |
0
| throw new IllegalArgumentException(
|
231 |
| "NullPointer as set is not allowed"); |
232 |
| } |
233 |
0
| this.elements.retainAll(set.elements);
|
234 |
0
| return this;
|
235 |
| } |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
6231
| public final ElementSet newIntersection(final ElementSet set) {
|
247 |
6231
| if (set == null) {
|
248 |
0
| throw new IllegalArgumentException(
|
249 |
| "NullPointer as set is not allowed"); |
250 |
| } |
251 |
6231
| final ElementSet result = new ElementSet(this);
|
252 |
6231
| result.elements.retainAll(set.elements);
|
253 |
6231
| return result;
|
254 |
| } |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
0
| public final ElementSet newDelta(final ElementSet set) {
|
269 |
0
| if (set == null) {
|
270 |
0
| throw new IllegalArgumentException(
|
271 |
| "NullPointer as set is not allowed"); |
272 |
| } |
273 |
0
| final ElementSet union = new ElementSet(this);
|
274 |
0
| union.union(set);
|
275 |
0
| final ElementSet intersection = new ElementSet(this);
|
276 |
0
| intersection.intersection(set);
|
277 |
0
| union.minus(intersection);
|
278 |
0
| return union;
|
279 |
| } |
280 |
| |
281 |
0
| public final boolean equals(final Object obj) {
|
282 |
0
| if (obj == null) {
|
283 |
0
| return false;
|
284 |
| } |
285 |
0
| if (obj.getClass() == ElementSet.class) {
|
286 |
0
| return this.elements.equals(((ElementSet) obj).elements);
|
287 |
| } |
288 |
0
| return false;
|
289 |
| } |
290 |
| |
291 |
0
| public final int hashCode() {
|
292 |
0
| return elements.hashCode();
|
293 |
| } |
294 |
| |
295 |
32
| public final String toString() {
|
296 |
32
| final StringBuffer result = new StringBuffer();
|
297 |
32
| result.append("{");
|
298 |
32
| final Iterator iterator = elements.iterator();
|
299 |
32
| while (iterator.hasNext()) {
|
300 |
32
| result.append(iterator.next());
|
301 |
32
| if (iterator.hasNext()) {
|
302 |
0
| result.append(", ");
|
303 |
| } |
304 |
| } |
305 |
32
| result.append("}");
|
306 |
32
| return result.toString();
|
307 |
| } |
308 |
| |
309 |
| } |