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