ElementSet.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  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      * Is this set empty?
117      *
118      @return  Is this set empty?
119      */
120     public final boolean isEmpty() {
121         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     public final boolean isSubset(final ElementSet set) {
133         if (set == null) {
134             throw new IllegalArgumentException("NullPointer as set is not allowed");
135         }
136         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     public final ElementSet add(final Element element) {
150         if (element == null) {
151             throw new IllegalArgumentException("NullPointer as element is not allowed");
152         }
153         elements.add(element);
154         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     public final ElementSet union(final ElementSet set) {
168         if (set == null) {
169             throw new IllegalArgumentException(
170                 "NullPointer as set is not allowed");
171         }
172         elements.addAll(set.elements);
173         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     public final ElementSet remove(final Element element) {
186         if (element == null) {
187             throw new IllegalArgumentException(
188                 "NullPointer as element is not allowed");
189         }
190         elements.remove(element);
191         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     public final ElementSet minus(final ElementSet set) {
207         if (set == null) {
208             throw new IllegalArgumentException(
209                 "NullPointer as set is not allowed");
210         }
211         this.elements.removeAll(set.elements);
212         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     public final ElementSet intersection(final ElementSet set) {
225         if (set == null) {
226             throw new IllegalArgumentException(
227                 "NullPointer as set is not allowed");
228         }
229         this.elements.retainAll(set.elements);
230         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     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 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     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 }