ElementSet.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.se.dto.list;
017 
018 import java.util.Arrays;
019 import java.util.HashSet;
020 import java.util.Iterator;
021 import java.util.Set;
022 
023 import org.qedeq.kernel.se.base.list.Element;
024 import org.qedeq.kernel.se.base.list.ElementList;
025 
026 
027 /**
028  * This class represents a set of {@link org.qedeq.kernel.se.base.list.Element}s.
029  *
030  @author  Michael Meyling
031  */
032 public class ElementSet {
033 
034 
035     /** Here are the elements stored. */
036     private final Set elements;
037 
038 
039     /**
040      * Constructs an empty element set.
041      */
042     public ElementSet() {
043         this.elements = new HashSet();
044     }
045 
046 
047     /**
048      * Constructs an element set.
049      *
050      @param  elements the elements to put into the set
051      @throws IllegalArgumentException if <code>elements</code> was a NullPointer
052      */
053     public ElementSet(final Element[] elements) {
054         if (elements == null) {
055             throw new IllegalArgumentException(
056                 "NullPointer as element array is not allowed");
057         }
058         this.elements = new HashSet(Arrays.asList(elements));
059     }
060 
061 
062     /**
063      * Constructs an element set.
064      *
065      @param  set  contains the elements to put into the set
066      @throws IllegalArgumentException if <code>set</code> was a
067      *         NullPointer
068      */
069     public ElementSet(final ElementSet set) {
070         if (set == null) {
071             throw new IllegalArgumentException(
072                 "NullPointer as set is not allowed");
073         }
074         this.elements = new HashSet(set.elements);
075     }
076 
077 
078     /**
079      * Constructs an element set from all operands of an element.
080      * The element must not be a symbol.
081      *
082      @param  element  contains the elements to put into the set
083      * (without the operator)
084      @throws IllegalArgumentException if <code>element</code> was a
085      *         NullPointer or was an atom.
086      */
087     public ElementSet(final ElementList element) {
088         if (element == null) {
089             throw new IllegalArgumentException(
090                 "NullPointer as element is not allowed");
091         }
092         if (element.isAtom()) {
093             throw new IllegalArgumentException(
094                 "text as element is not allowed");
095         }
096         this.elements = new HashSet(element.getElements());
097     }
098 
099 
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     public final boolean contains(final Element element) {
109         if (element == null) {
110             throw new IllegalArgumentException("NullPointer as element is not allowed");
111         }
112         return this.elements.contains(element);
113     }
114 
115 
116     /**
117      * Is set empty?
118      *
119      @return  is set empty?
120      */
121     public final boolean isEmpty() {
122         return elements.isEmpty();
123     }
124 
125 
126     /**
127      * Is <code>set</code> a subset of this set?
128      *
129      @param   set    set to check for.
130      @return  is <code>set</code> a subset of this set?
131      @throws  IllegalArgumentException if the set was a NullPointer
132      */
133     public final boolean isSubset(final ElementSet set) {
134         if (set == null) {
135             throw new IllegalArgumentException("NullPointer as set is not allowed");
136         }
137         return this.elements.containsAll(set.elements);
138     }
139 
140 
141     /**
142      * Add an element to set. This object is after the method the
143      * union of this set with {<code>element</code>}
144      *
145      @param   element    element to put into the set
146      @return  was <code>this</code> set changed?
147      @throws  IllegalArgumentException if the element was a
148      *          NullPointer
149      */
150     public final ElementSet add(final Element element) {
151         if (element == null) {
152             throw new IllegalArgumentException("NullPointer as element is not allowed");
153         }
154         elements.add(element);
155         return this;
156     }
157 
158 
159     /**
160      * Add elements from another {@link ElementSet} to this set.
161      * After this method this object is the union of the two sets.
162      *
163      @param   set    add all elements that are here
164      @return  was <code>this</code> set changed?
165      @throws  IllegalArgumentException if the set was a
166      *          NullPointer
167      */
168     public final ElementSet union(final ElementSet set) {
169         if (set == null) {
170             throw new IllegalArgumentException(
171                 "NullPointer as set is not allowed");
172         }
173         elements.addAll(set.elements);
174         return this;
175     }
176 
177 
178     /**
179      * Remove an element from set.
180      *
181      @param   element    Element to remove from the set
182      @return  was <code>this</code> set changed?
183      @throws  IllegalArgumentException if the element was a
184      *          NullPointer
185      */
186     public final ElementSet remove(final Element element) {
187         if (element == null) {
188             throw new IllegalArgumentException(
189                 "NullPointer as element is not allowed");
190         }
191         elements.remove(element);
192         return this;
193     }
194 
195 
196     /**
197      * Remove elements from another {@link ElementSet} from this set.
198      * After this method this object is the asymmetric set difference of the
199      * two sets: <code>this</code> <code>set</code>.
200      *
201      @param   set    remove all elements that are in this set from
202      *                 <code>this</code>
203      @return  was <code>this</code> set changed?
204      @throws  IllegalArgumentException if the set was a
205      *          NullPointer
206      */
207     public final ElementSet minus(final ElementSet set) {
208         if (set == null) {
209             throw new IllegalArgumentException(
210                 "NullPointer as set is not allowed");
211         }
212         this.elements.removeAll(set.elements);
213         return this;
214     }
215 
216 
217     /**
218      * Build the intersection.
219      *
220      @param   set    check for these elements
221      @return  was <code>this</code> set changed?
222      @throws  IllegalArgumentException if the set was a
223      *          NullPointer
224      */
225     public final ElementSet intersection(final ElementSet set) {
226         if (set == null) {
227             throw new IllegalArgumentException(
228                 "NullPointer as set is not allowed");
229         }
230         this.elements.retainAll(set.elements);
231         return this;
232     }
233 
234 
235     /**
236      * Build a new intersection.
237      *
238      @param   set    check for these elements
239      @return  was <code>this</code> set changed?
240      @throws  IllegalArgumentException if the set was a
241      *          NullPointer
242      */
243     public final ElementSet newIntersection(final ElementSet set) {
244         if (set == null) {
245             throw new IllegalArgumentException(
246                 "NullPointer as set is not allowed");
247         }
248         final ElementSet result = new ElementSet(this);
249         result.elements.retainAll(set.elements);
250         return result;
251     }
252 
253 
254     /**
255      * Return all elements that are only in one of both sets.
256      * This method returns the symmetric set difference of the
257      * two sets.
258      *
259      @param   set    remove all elements that are in this set from
260      *                 <code>this</code>
261      @return  was <code>this</code> set changed?
262      @throws  IllegalArgumentException if the set was a
263      *          NullPointer
264      */
265     public final ElementSet newDelta(final ElementSet set) {
266         if (set == null) {
267             throw new IllegalArgumentException(
268                 "NullPointer as set is not allowed");
269         }
270         final ElementSet union = new ElementSet(this);
271         union.union(set);
272         final ElementSet intersection = new ElementSet(this);
273         intersection.intersection(set);
274         union.minus(intersection);
275         return union;
276     }
277 
278     /**
279      * Get number of elements.
280      *
281      @return  Number of elements in this set.
282      */
283     public final int size() {
284         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     public Iterator iterator() {
295         return this.elements.iterator();
296     }
297 
298     public final boolean equals(final Object obj) {
299         if (obj == null) {
300             return false;
301         }
302         if (obj.getClass() == ElementSet.class) {
303             return this.elements.equals(((ElementSetobj).elements);
304         }
305         return false;
306     }
307 
308     public final int hashCode() {
309         return elements.hashCode();
310     }
311 
312     public final String toString() {
313         final StringBuffer result = new StringBuffer();
314         result.append("{");
315         final Iterator iterator = elements.iterator();
316         while (iterator.hasNext()) {
317             result.append(iterator.next());
318             if (iterator.hasNext()) {
319                 result.append(", ");
320             }
321         }
322         result.append("}");
323         return result.toString();
324     }
325 
326 }