View Javadoc

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.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      public ElementSet() {
43          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      public ElementSet(final Element[] elements) {
54          if (elements == null) {
55              throw new IllegalArgumentException(
56                  "NullPointer as element array is not allowed");
57          }
58          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      public ElementSet(final ElementSet set) {
70          if (set == null) {
71              throw new IllegalArgumentException(
72                  "NullPointer as set is not allowed");
73          }
74          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      public ElementSet(final ElementList element) {
88          if (element == null) {
89              throw new IllegalArgumentException(
90                  "NullPointer as element is not allowed");
91          }
92          if (element.isAtom()) {
93              throw new IllegalArgumentException(
94                  "text as element is not allowed");
95          }
96          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     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(((ElementSet) obj).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 }