KernelQedeqBoSet.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.bo.module;
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.bo.common.QedeqBo;
024 import org.qedeq.kernel.bo.common.QedeqBoSet;
025 
026 
027 /**
028  * This class represents a set of {@link KernelQedeqBo}s.
029  *
030  @author  Michael Meyling
031  */
032 public class KernelQedeqBoSet implements QedeqBoSet {
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 KernelQedeqBoSet() {
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 KernelQedeqBoSet(final KernelQedeqBo[] 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      * Constructs an element set.
063      *
064      @param  element  Put this element into the set
065      @throws IllegalArgumentException if <code>element</code> was a NullPointer
066      */
067     public KernelQedeqBoSet(final KernelQedeqBo element) {
068         if (element == null) {
069             throw new IllegalArgumentException(
070                 "NullPointer as element array is not allowed");
071         }
072         this.elements = new HashSet();
073         elements.add(element);
074     }
075 
076     /**
077      * Constructs an element set.
078      *
079      @param  set  contains the elements to put into the set
080      @throws IllegalArgumentException if <code>set</code> was a
081      *         NullPointer
082      */
083     public KernelQedeqBoSet(final KernelQedeqBoSet set) {
084         if (set == null) {
085             throw new IllegalArgumentException(
086                 "NullPointer as set is not allowed");
087         }
088         this.elements = new HashSet(set.elements);
089     }
090 
091 
092     /**
093      * Is element in set?
094      *
095      @param   element    KernelQedeqBo to check for.
096      @return  Is <code>element</code> in this set?
097      @throws  IllegalArgumentException if the element was a
098      *          NullPointer
099      */
100     public final boolean contains(final KernelQedeqBo element) {
101         if (element == null) {
102             throw new IllegalArgumentException("NullPointer as element is not allowed");
103         }
104         return this.elements.contains(element);
105     }
106 
107     public boolean contains(final QedeqBo element) {
108         if (element == null) {
109             throw new IllegalArgumentException("NullPointer as element is not allowed");
110         }
111         return this.elements.contains(element);
112     }
113 
114     /**
115      * Is this set empty?
116      *
117      @return  Is this set empty?
118      */
119     public final boolean isEmpty() {
120         return elements.isEmpty();
121     }
122 
123 
124     /**
125      * Add an element to set. This object is after the method the
126      * union of this set with {<code>element</code>}
127      *
128      @param   element    element to put into the set
129      @return  Possibly changed <code>this</code>.
130      @throws  IllegalArgumentException if the element was a
131      *          NullPointer
132      */
133     public final KernelQedeqBoSet add(final KernelQedeqBo element) {
134         if (element == null) {
135             throw new IllegalArgumentException("NullPointer as element is not allowed");
136         }
137         elements.add(element);
138         return this;
139     }
140 
141 
142     /**
143      * Add elements from another {@link KernelQedeqBoSet} to this set.
144      * After this method this object is the union of the two sets.
145      *
146      @param   set    add all elements that are here
147      @return  Possibly changed <code>this</code>.
148      @throws  IllegalArgumentException if the set was a
149      *          NullPointer
150      */
151     public final KernelQedeqBoSet add(final KernelQedeqBoSet set) {
152         if (set == null) {
153             throw new IllegalArgumentException(
154                 "NullPointer as set is not allowed");
155         }
156         elements.addAll(set.elements);
157         return this;
158     }
159 
160 
161     /**
162      * Remove an element from this set.
163      *
164      @param   element    KernelQedeqBo to remove from the set. Must not be <code>null</code>.
165      @return  Possibly changed <code>this</code>.
166      @throws  IllegalArgumentException if the element was a
167      *          NullPointer
168      */
169     public final KernelQedeqBoSet remove(final KernelQedeqBo element) {
170         if (element == null) {
171             throw new IllegalArgumentException(
172                 "NullPointer as element is not allowed");
173         }
174         elements.remove(element);
175         return this;
176     }
177 
178 
179     /**
180      * Remove elements from another {@link KernelQedeqBoSet} from this set.
181      * After this method this object is the asymmetric set difference of the
182      * two sets: <code>this</code> <code>set</code>.
183      *
184      @param   set    Remove all elements that are in this set from
185      *                 <code>this</code>.
186      @return  Possibly changed <code>this</code>.
187      @throws  IllegalArgumentException if the set was a
188      *          NullPointer
189      */
190     public final KernelQedeqBoSet remove(final KernelQedeqBoSet set) {
191         if (set == null) {
192             throw new IllegalArgumentException(
193                 "NullPointer as set is not allowed");
194         }
195         this.elements.removeAll(set.elements);
196         return this;
197     }
198 
199 
200     /**
201      * Build the intersection.
202      *
203      @param   set    Check for these elements.
204      @return  Possibly changed <code>this</code>.
205      @throws  IllegalArgumentException if the set was a
206      *          NullPointer
207      */
208     public final KernelQedeqBoSet intersection(final KernelQedeqBoSet set) {
209         if (set == null) {
210             throw new IllegalArgumentException(
211                 "NullPointer as set is not allowed");
212         }
213         this.elements.retainAll(set.elements);
214         return this;
215     }
216 
217 
218     /**
219      * Get number of elements.
220      *
221      @return  Number of elements in this set.
222      */
223     public final int size() {
224         return this.elements.size();
225     }
226 
227     /**
228      * Returns an iterator over the elements in this set.  The elements are
229      * returned in no particular order (unless this set is an instance of some
230      * class that provides a guarantee).
231      *
232      @return  Iterator over the elements in this set.
233      */
234     public Iterator iterator() {
235         return this.elements.iterator();
236     }
237 
238     public final boolean equals(final Object obj) {
239         if (obj instanceof KernelQedeqBoSet) {
240             return this.elements.equals(((KernelQedeqBoSetobj).elements);
241         }
242         return false;
243     }
244 
245     public final int hashCode() {
246         return elements.hashCode();
247     }
248 
249     public final String toString() {
250         final StringBuffer result = new StringBuffer();
251         result.append("{");
252         final Iterator iterator = elements.iterator();
253         while (iterator.hasNext()) {
254             result.append(iterator.next());
255             if (iterator.hasNext()) {
256                 result.append(", ");
257             }
258         }
259         result.append("}");
260         return result.toString();
261     }
262 
263 
264 }