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.bo.logic.work;
017
018 import java.util.Arrays;
019 import java.util.HashSet;
020 import java.util.Iterator;
021 import java.util.List;
022 import java.util.Set;
023
024 import org.qedeq.kernel.se.base.list.Element;
025 import org.qedeq.kernel.se.base.list.ElementList;
026
027
028 /**
029 * This class represents a set of {@link Element}s.
030 *
031 * @version $Revision: 1.1 $
032 */
033 public class EqualFormulaSet {
034
035 /** Here are the formulas stored. */
036 private final Set formulas;
037
038 /** Error message: NullPointer as element is not allowed. */
039 private static final String NULLPOINTER_AS_WORD_IS_NOT_ALLOWED
040 = "NullPointer as element is not allowed";
041
042 /** Error message: NullPointer as set is not allowed. */
043 private static final String NULLPOINTER_AS_SET_IS_NOT_ALLOWED
044 = "NullPointer as set is not allowed";
045
046
047 /**
048 * Constructs an empty element set.
049 *
050 */
051 public EqualFormulaSet() {
052 this.formulas = new HashSet();
053 }
054
055 /**
056 * Constructs an element set.
057 *
058 * @param formulas the elements to put into the set
059 * @throws IllegalArgumentException if <code>elements</code> was a
060 * NullPointer
061 */
062 public EqualFormulaSet(final EqualFormula[] formulas) {
063 if (formulas == null) {
064 throw new IllegalArgumentException(
065 "NullPointer as element array is not allowed");
066 }
067 this.formulas = new HashSet(Arrays.asList(formulas));
068 }
069
070 /**
071 * Constructs an equal formulas set.
072 *
073 * @param set Contains the elements to put into the set.
074 * @throws IllegalArgumentException <code>set</code> was a
075 * NullPointer.
076 */
077 EqualFormulaSet(final EqualFormulaSet set) {
078 if (set == null) {
079 throw new IllegalArgumentException(
080 NULLPOINTER_AS_SET_IS_NOT_ALLOWED);
081 }
082 this.formulas = new HashSet(set.formulas);
083 }
084
085 /**
086 * Constructs an element set from all operands of an element.
087 * The element must not be a symbol.
088 *
089 * @param element Contains the elements to put into the set
090 * (without the operator).
091 * @throws IllegalArgumentException <code>element</code> was a
092 * NullPointer or was an atom.
093 */
094 public EqualFormulaSet(final ElementList element) {
095 if (element == null) {
096 throw new IllegalArgumentException(NULLPOINTER_AS_WORD_IS_NOT_ALLOWED);
097 }
098 if (element.isAtom()) {
099 throw new IllegalArgumentException(
100 "atom as element is not allowed");
101 }
102 List list = element.getElements();
103 this.formulas = new HashSet();
104 for (int i = 0; i < list.size(); i++) {
105 this.formulas.add(new EqualFormula((Element) list.get(i)));
106 }
107 }
108
109 /**
110 * Is element in set?
111 *
112 * @param formula Element to check for.
113 * @return Is <code>formula</code> in this set?
114 * @throws IllegalArgumentException <code>element</code> was a
115 * NullPointer.
116 */
117 public final boolean contains(final EqualFormula formula) {
118 if (formula == null) {
119 throw new IllegalArgumentException(NULLPOINTER_AS_WORD_IS_NOT_ALLOWED);
120 }
121 return this.formulas.contains(formula);
122 }
123
124 /**
125 * Is set empty?
126 *
127 * @return Is this set empty?
128 */
129 public final boolean isEmpty() {
130 return formulas.isEmpty();
131 }
132
133 /**
134 * Is <code>set</code> a subset of this set?
135 *
136 * @param set set to check for.
137 * @return is <code>set</code> a subset of this set?
138 * @throws IllegalArgumentException <code>set</code> was a
139 * NullPointer
140 */
141 public final boolean isSubset(final EqualFormulaSet set) {
142 if (set == null) {
143 throw new IllegalArgumentException(NULLPOINTER_AS_SET_IS_NOT_ALLOWED);
144 }
145 return this.formulas.containsAll(set.formulas);
146 }
147
148 /**
149 * Add a formula to set. This object is after the method the
150 * union of this set with <code>element</code>
151 *
152 * @param formula FormulaOrTerm to put into the set.
153 * @return <code>this</code>.
154 * @throws IllegalArgumentException <code>formula</code> was a
155 * NullPointer.
156 */
157 public final EqualFormulaSet add(final EqualFormula formula) {
158 if (formula == null) {
159 throw new IllegalArgumentException(NULLPOINTER_AS_WORD_IS_NOT_ALLOWED);
160 }
161 formulas.add(formula);
162 return this;
163 }
164
165 /**
166 * After this method this object is the union of the two sets.
167 *
168 * @param set Add all formulas that are here in.
169 * @return <code>this</code>.
170 * @throws IllegalArgumentException <code>set</code> was a NullPointer.
171 */
172 public final EqualFormulaSet union(final EqualFormulaSet set) {
173 if (set == null) {
174 throw new IllegalArgumentException(NULLPOINTER_AS_SET_IS_NOT_ALLOWED);
175 }
176 formulas.addAll(set.formulas);
177 return this;
178 }
179
180 /**
181 * Remove an element from set.
182 *
183 * @param formula FormulaOrTerm to remove from the set
184 * @return <code>this</code>.
185 * @throws IllegalArgumentException <code>formula</code> was a NullPointer.
186 */
187 public final EqualFormulaSet remove(final EqualFormula formula) {
188 if (formula == null) {
189 throw new IllegalArgumentException(NULLPOINTER_AS_WORD_IS_NOT_ALLOWED);
190 }
191 formulas.remove(formula);
192 return this;
193 }
194
195 /**
196 * Remove elements from another {@link EqualFormulaSet} 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 Was <code>this</code> set changed?
203 * @throws IllegalArgumentException <code>set</code> was a
204 * NullPointer
205 */
206 public final EqualFormulaSet minus(final EqualFormulaSet set) {
207 if (set == null) {
208 throw new IllegalArgumentException(NULLPOINTER_AS_SET_IS_NOT_ALLOWED);
209 }
210 this.formulas.removeAll(set.formulas);
211 return this;
212 }
213
214 /**
215 * Build the intersection.
216 *
217 * @param set Check for these elements.
218 * @return Has <code>this</code> set changed?
219 * @throws IllegalArgumentException <code>set</code> was a
220 * NullPointer
221 */
222 public final EqualFormulaSet intersection(final EqualFormulaSet set) {
223 if (set == null) {
224 throw new IllegalArgumentException(NULLPOINTER_AS_SET_IS_NOT_ALLOWED);
225 }
226 this.formulas.retainAll(set.formulas);
227 return this;
228 }
229
230 /**
231 * Build a new intersection.
232 *
233 * @param set check for these elements
234 * @return was <code>this</code> set changed?
235 * @throws IllegalArgumentException if the set was a
236 * NullPointer
237 */
238 public final EqualFormulaSet newIntersection(final EqualFormulaSet set) {
239 if (set == null) {
240 throw new IllegalArgumentException(
241 NULLPOINTER_AS_SET_IS_NOT_ALLOWED);
242 }
243 final EqualFormulaSet result = new EqualFormulaSet(this);
244 result.formulas.retainAll(set.formulas);
245 return result;
246 }
247
248 public final boolean equals(final Object obj) {
249 if (obj == null) {
250 return false;
251 }
252 if (obj.getClass() == EqualFormulaSet.class) {
253 return this.formulas.equals(((EqualFormulaSet) obj).formulas);
254 }
255 return false;
256 }
257
258 public final int hashCode() {
259 return formulas.hashCode();
260 }
261
262 public final String toString() {
263 final StringBuffer result = new StringBuffer();
264 result.append("{");
265 final Iterator iterator = formulas.iterator();
266 while (iterator.hasNext()) {
267 result.append(iterator.next());
268 if (iterator.hasNext()) {
269 result.append(", ");
270 }
271 }
272 result.append("}");
273 return result.toString();
274 }
275
276 }
|