1 /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2 *
3 * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>.
4 *
5 * "Hilbert II" is free software; you can redistribute
6 * it and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 package org.qedeq.kernel.bo.common;
17
18 import java.util.Arrays;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.Set;
22
23
24 /**
25 * This class represents a set of {@link QedeqBo}s.
26 *
27 * @author Michael Meyling
28 */
29 public class QedeqBoSet {
30
31
32 /** Here are the elements stored. */
33 private final Set elements;
34
35
36 /**
37 * Constructs an empty element set.
38 */
39 public QedeqBoSet() {
40 this.elements = new HashSet();
41 }
42
43
44 /**
45 * Constructs an element set.
46 *
47 * @param elements the elements to put into the set
48 * @throws IllegalArgumentException if <code>elements</code> was a NullPointer
49 */
50 public QedeqBoSet(final QedeqBo[] elements) {
51 if (elements == null) {
52 throw new IllegalArgumentException(
53 "NullPointer as element array is not allowed");
54 }
55 this.elements = new HashSet(Arrays.asList(elements));
56 }
57
58 /**
59 * Constructs an element set.
60 *
61 * @param element Put this element into the set
62 * @throws IllegalArgumentException if <code>element</code> was a NullPointer
63 */
64 public QedeqBoSet(final QedeqBo element) {
65 if (element == null) {
66 throw new IllegalArgumentException(
67 "NullPointer as element array is not allowed");
68 }
69 this.elements = new HashSet();
70 elements.add(element);
71 }
72
73 /**
74 * Constructs an element set.
75 *
76 * @param set contains the elements to put into the set
77 * @throws IllegalArgumentException if <code>set</code> was a
78 * NullPointer
79 */
80 public QedeqBoSet(final QedeqBoSet set) {
81 if (set == null) {
82 throw new IllegalArgumentException(
83 "NullPointer as set is not allowed");
84 }
85 this.elements = new HashSet(set.elements);
86 }
87
88
89 /**
90 * Is element in set?
91 *
92 * @param element QedeqBo to check for.
93 * @return Is <code>element</code> in this set?
94 * @throws IllegalArgumentException if the element was a
95 * NullPointer
96 */
97 public final boolean contains(final QedeqBo element) {
98 if (element == null) {
99 throw new IllegalArgumentException("NullPointer as element is not allowed");
100 }
101 return this.elements.contains(element);
102 }
103
104 /**
105 * Is this set empty?
106 *
107 * @return Is this set empty?
108 */
109 public final boolean isEmpty() {
110 return elements.isEmpty();
111 }
112
113
114 /**
115 * Add an element to set. This object is after the method the
116 * union of this set with {<code>element</code>}
117 *
118 * @param element element to put into the set
119 * @return Possibly changed <code>this</code>.
120 * @throws IllegalArgumentException if the element was a
121 * NullPointer
122 */
123 public final QedeqBoSet add(final QedeqBo element) {
124 if (element == null) {
125 throw new IllegalArgumentException("NullPointer as element is not allowed");
126 }
127 elements.add(element);
128 return this;
129 }
130
131
132 /**
133 * Add elements from another {@link QedeqBoSet} to this set.
134 * After this method this object is the union of the two sets.
135 *
136 * @param set add all elements that are here
137 * @return Possibly changed <code>this</code>.
138 * @throws IllegalArgumentException if the set was a
139 * NullPointer
140 */
141 public final QedeqBoSet add(final QedeqBoSet set) {
142 if (set == null) {
143 throw new IllegalArgumentException(
144 "NullPointer as set is not allowed");
145 }
146 elements.addAll(set.elements);
147 return this;
148 }
149
150
151 /**
152 * Remove an element from this set.
153 *
154 * @param element QedeqBo to remove from the set. Must not be <code>null</code>.
155 * @return Possibly changed <code>this</code>.
156 * @throws IllegalArgumentException if the element was a
157 * NullPointer
158 */
159 public final QedeqBoSet remove(final QedeqBo element) {
160 if (element == null) {
161 throw new IllegalArgumentException(
162 "NullPointer as element is not allowed");
163 }
164 elements.remove(element);
165 return this;
166 }
167
168 /**
169 * Remove elements from another {@link QedeqBoSet} from this set.
170 * After this method this object is the asymmetric set difference of the
171 * two sets: <code>this</code> \ <code>set</code>.
172 *
173 * @param set Remove all elements that are in this set from
174 * <code>this</code>.
175 * @return Possibly changed <code>this</code>.
176 * @throws IllegalArgumentException if the set was a
177 * NullPointer
178 */
179 public final QedeqBoSet remove(final QedeqBoSet set) {
180 if (set == null) {
181 throw new IllegalArgumentException(
182 "NullPointer as set is not allowed");
183 }
184 this.elements.removeAll(set.elements);
185 return this;
186 }
187
188 /**
189 * Build the intersection.
190 *
191 * @param set Check for these elements.
192 * @return Possibly changed <code>this</code>.
193 * @throws IllegalArgumentException if the set was a
194 * NullPointer
195 */
196 public final QedeqBoSet intersection(final QedeqBoSet set) {
197 if (set == null) {
198 throw new IllegalArgumentException(
199 "NullPointer as set is not allowed");
200 }
201 this.elements.retainAll(set.elements);
202 return this;
203 }
204
205
206 /**
207 * Get number of elements.
208 *
209 * @return Number of elements in this set.
210 */
211 public final int size() {
212 return this.elements.size();
213 }
214
215 /**
216 * Returns an iterator over the elements in this set. The elements are
217 * returned in no particular order (unless this set is an instance of some
218 * class that provides a guarantee).
219 *
220 * @return Iterator over the elements in this set.
221 */
222 public Iterator iterator() {
223 return this.elements.iterator();
224 }
225
226 public final boolean equals(final Object obj) {
227 if (obj instanceof QedeqBoSet) {
228 return this.elements.equals(((QedeqBoSet) obj).elements);
229 }
230 return false;
231 }
232
233 public final int hashCode() {
234 return elements.hashCode();
235 }
236
237 public final String toString() {
238 final StringBuffer result = new StringBuffer();
239 result.append("{");
240 final Iterator iterator = elements.iterator();
241 while (iterator.hasNext()) {
242 result.append(iterator.next());
243 if (iterator.hasNext()) {
244 result.append(", ");
245 }
246 }
247 result.append("}");
248 return result.toString();
249 }
250
251
252 public String asLongList() {
253 final StringBuffer result = new StringBuffer();
254 final Iterator iterator = elements.iterator();
255 while (iterator.hasNext()) {
256 result.append(((QedeqBo) iterator.next()).getUrl());
257 if (iterator.hasNext()) {
258 result.append(", ");
259 }
260 }
261 return result.toString();
262 }
263
264
265 public String asShortList() {
266 final StringBuffer result = new StringBuffer();
267 final Iterator iterator = elements.iterator();
268 while (iterator.hasNext()) {
269 result.append(((QedeqBo) iterator.next()).getName());
270 if (iterator.hasNext()) {
271 result.append(", ");
272 }
273 }
274 return result.toString();
275 }
276
277
278 }