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