Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
69   326   36   3.63
34   144   0.52   19
19     1.89  
1    
 
  ElementSet       Line # 32 69 36 100% 1.0
 
  (82)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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.se.dto.list;
17   
18    import java.util.Arrays;
19    import java.util.HashSet;
20    import java.util.Iterator;
21    import java.util.Set;
22   
23    import org.qedeq.kernel.se.base.list.Element;
24    import org.qedeq.kernel.se.base.list.ElementList;
25   
26   
27    /**
28    * This class represents a set of {@link org.qedeq.kernel.se.base.list.Element}s.
29    *
30    * @author Michael Meyling
31    */
 
32    public class ElementSet {
33   
34   
35    /** Here are the elements stored. */
36    private final Set elements;
37   
38   
39    /**
40    * Constructs an empty element set.
41    */
 
42  2099691 toggle public ElementSet() {
43  2099691 this.elements = new HashSet();
44    }
45   
46   
47    /**
48    * Constructs an element set.
49    *
50    * @param elements the elements to put into the set
51    * @throws IllegalArgumentException if <code>elements</code> was a NullPointer
52    */
 
53  40 toggle public ElementSet(final Element[] elements) {
54  40 if (elements == null) {
55  1 throw new IllegalArgumentException(
56    "NullPointer as element array is not allowed");
57    }
58  39 this.elements = new HashSet(Arrays.asList(elements));
59    }
60   
61   
62    /**
63    * Constructs an element set.
64    *
65    * @param set contains the elements to put into the set
66    * @throws IllegalArgumentException if <code>set</code> was a
67    * NullPointer
68    */
 
69  369976 toggle public ElementSet(final ElementSet set) {
70  369976 if (set == null) {
71  1 throw new IllegalArgumentException(
72    "NullPointer as set is not allowed");
73    }
74  369975 this.elements = new HashSet(set.elements);
75    }
76   
77   
78    /**
79    * Constructs an element set from all operands of an element.
80    * The element must not be a symbol.
81    *
82    * @param element contains the elements to put into the set
83    * (without the operator)
84    * @throws IllegalArgumentException if <code>element</code> was a
85    * NullPointer or was an atom.
86    */
 
87  3 toggle public ElementSet(final ElementList element) {
88  3 if (element == null) {
89  1 throw new IllegalArgumentException(
90    "NullPointer as element is not allowed");
91    }
92  2 if (element.isAtom()) {
93  1 throw new IllegalArgumentException(
94    "text as element is not allowed");
95    }
96  1 this.elements = new HashSet(element.getElements());
97    }
98   
99   
100    /**
101    * Is element in set?
102    *
103    * @param element Element to check for.
104    * @return Is <code>element</code> in this set?
105    * @throws IllegalArgumentException if the element was a
106    * NullPointer
107    */
 
108  17338 toggle public final boolean contains(final Element element) {
109  17338 if (element == null) {
110  1 throw new IllegalArgumentException("NullPointer as element is not allowed");
111    }
112  17337 return this.elements.contains(element);
113    }
114   
115    /**
116    * Is this set empty?
117    *
118    * @return Is this set empty?
119    */
 
120  370832 toggle public final boolean isEmpty() {
121  370832 return elements.isEmpty();
122    }
123   
124   
125    /**
126    * Is <code>set</code> a superset of this set?
127    *
128    * @param set Set to check for.
129    * @return Is this set a subset of <code>set</code>?
130    * @throws IllegalArgumentException if the set was a NullPointer
131    */
 
132  6 toggle public final boolean isSubset(final ElementSet set) {
133  6 if (set == null) {
134  1 throw new IllegalArgumentException("NullPointer as set is not allowed");
135    }
136  5 return set.elements.containsAll(this.elements);
137    }
138   
139   
140    /**
141    * Add an element to set. This object is after the method the
142    * union of this set with {<code>element</code>}
143    *
144    * @param element element to put into the set
145    * @return Possibly changed <code>this</code>.
146    * @throws IllegalArgumentException if the element was a
147    * NullPointer
148    */
 
149  269085 toggle public final ElementSet add(final Element element) {
150  269085 if (element == null) {
151  1 throw new IllegalArgumentException("NullPointer as element is not allowed");
152    }
153  269084 elements.add(element);
154  269084 return this;
155    }
156   
157   
158    /**
159    * Add elements from another {@link ElementSet} to this set.
160    * After this method this object is the union of the two sets.
161    *
162    * @param set add all elements that are here
163    * @return Possibly changed <code>this</code>.
164    * @throws IllegalArgumentException if the set was a
165    * NullPointer
166    */
 
167  1784614 toggle public final ElementSet union(final ElementSet set) {
168  1784614 if (set == null) {
169  1 throw new IllegalArgumentException(
170    "NullPointer as set is not allowed");
171    }
172  1784613 elements.addAll(set.elements);
173  1784613 return this;
174    }
175   
176   
177    /**
178    * Remove an element from this set.
179    *
180    * @param element Element to remove from the set. Must not be <code>null</code>.
181    * @return Possibly changed <code>this</code>.
182    * @throws IllegalArgumentException if the element was a
183    * NullPointer
184    */
 
185  24340 toggle public final ElementSet remove(final Element element) {
186  24340 if (element == null) {
187  1 throw new IllegalArgumentException(
188    "NullPointer as element is not allowed");
189    }
190  24339 elements.remove(element);
191  24339 return this;
192    }
193   
194   
195    /**
196    * Remove elements from another {@link ElementSet} from this set.
197    * After this method this object is the asymmetric set difference of the
198    * two sets: <code>this</code> \ <code>set</code>.
199    *
200    * @param set Remove all elements that are in this set from
201    * <code>this</code>.
202    * @return Possibly changed <code>this</code>.
203    * @throws IllegalArgumentException if the set was a
204    * NullPointer
205    */
 
206  300 toggle public final ElementSet minus(final ElementSet set) {
207  300 if (set == null) {
208  1 throw new IllegalArgumentException(
209    "NullPointer as set is not allowed");
210    }
211  299 this.elements.removeAll(set.elements);
212  299 return this;
213    }
214   
215   
216    /**
217    * Build the intersection.
218    *
219    * @param set Check for these elements.
220    * @return Possibly changed <code>this</code>.
221    * @throws IllegalArgumentException if the set was a
222    * NullPointer
223    */
 
224  874 toggle public final ElementSet intersection(final ElementSet set) {
225  874 if (set == null) {
226  1 throw new IllegalArgumentException(
227    "NullPointer as set is not allowed");
228    }
229  873 this.elements.retainAll(set.elements);
230  873 return this;
231    }
232   
233   
234    /**
235    * Build a new intersection.
236    *
237    * @param set check for these elements
238    * @return New instance that contains all elements that were in <code>this</code>
239    * and <code>set</code>.
240    * @throws IllegalArgumentException if the set was a
241    * NullPointer
242    */
 
243  369967 toggle public final ElementSet newIntersection(final ElementSet set) {
244  369967 if (set == null) {
245  1 throw new IllegalArgumentException(
246    "NullPointer as set is not allowed");
247    }
248  369966 final ElementSet result = new ElementSet(this);
249  369966 result.elements.retainAll(set.elements);
250  369966 return result;
251    }
252   
253   
254    /**
255    * Return all elements that are only in one of both sets.
256    * This method returns a new instance that holds the symmetric set difference of the
257    * two sets. The original set is not modified.
258    *
259    * @param set Build the symmetric difference with this set.
260    * <code>this</code>
261    * @return Symmetric difference.
262    * @throws IllegalArgumentException if the set was a
263    * NullPointer
264    */
 
265  5 toggle public final ElementSet newDelta(final ElementSet set) {
266  5 if (set == null) {
267  1 throw new IllegalArgumentException(
268    "NullPointer as set is not allowed");
269    }
270  4 final ElementSet union = new ElementSet(this);
271  4 union.union(set);
272  4 final ElementSet intersection = new ElementSet(this);
273  4 intersection.intersection(set);
274  4 union.minus(intersection);
275  4 return union;
276    }
277   
278    /**
279    * Get number of elements.
280    *
281    * @return Number of elements in this set.
282    */
 
283  1998 toggle public final int size() {
284  1998 return this.elements.size();
285    }
286   
287    /**
288    * Returns an iterator over the elements in this set. The elements are
289    * returned in no particular order (unless this set is an instance of some
290    * class that provides a guarantee).
291    *
292    * @return Iterator over the elements in this set.
293    */
 
294  36094 toggle public Iterator iterator() {
295  36094 return this.elements.iterator();
296    }
297   
 
298  45 toggle public final boolean equals(final Object obj) {
299  45 if (obj == null) {
300  4 return false;
301    }
302  41 if (obj.getClass() == ElementSet.class) {
303  40 return this.elements.equals(((ElementSet) obj).elements);
304    }
305  1 return false;
306    }
307   
 
308  8 toggle public final int hashCode() {
309  8 return elements.hashCode();
310    }
311   
 
312  45 toggle public final String toString() {
313  45 final StringBuffer result = new StringBuffer();
314  45 result.append("{");
315  45 final Iterator iterator = elements.iterator();
316  91 while (iterator.hasNext()) {
317  46 result.append(iterator.next());
318  46 if (iterator.hasNext()) {
319  2 result.append(", ");
320    }
321    }
322  45 result.append("}");
323  45 return result.toString();
324    }
325   
326    }