Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Jan 11 2007 09:03:50 CET
file stats: LOC: 309   Methods: 17
NCLOC: 138   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ElementSet.java 26,5% 40,3% 52,9% 38,1%
coverage coverage
 1    /* $Id: ElementSet.java,v 1.3 2006/10/20 20:23:08 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2006, Michael Meyling <mime@qedeq.org>.
 6    *
 7    * "Hilbert II" is free software; you can redistribute
 8    * it and/or modify it under the terms of the GNU General Public
 9    * License as published by the Free Software Foundation; either
 10    * version 2 of the License, or (at your option) any later version.
 11    *
 12    * This program is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15    * GNU General Public License for more details.
 16    */
 17   
 18    package org.qedeq.kernel.dto.list;
 19   
 20    import java.util.Arrays;
 21    import java.util.HashSet;
 22    import java.util.Iterator;
 23    import java.util.Set;
 24   
 25    import org.qedeq.kernel.base.list.Element;
 26    import org.qedeq.kernel.base.list.ElementList;
 27   
 28   
 29    /**
 30    * This class represents a set of {@link org.qedeq.kernel.base.list.Element}s.
 31    *
 32    * @version $Revision: 1.3 $
 33    * @author Michael Meyling
 34    */
 35    public final class ElementSet {
 36   
 37   
 38    /** Here are the elements stored. */
 39    private final Set elements;
 40   
 41   
 42    /**
 43    * Constructs an empty element set.
 44    */
 45  33294 public ElementSet() {
 46  33294 this.elements = new HashSet();
 47    }
 48   
 49   
 50    /**
 51    * Constructs an element set.
 52    *
 53    * @param elements the elements to put into the set
 54    * @throws IllegalArgumentException if <code>elements</code> was a NullPointer
 55    */
 56  0 public ElementSet(final Element[] elements) {
 57  0 if (elements == null) {
 58  0 throw new IllegalArgumentException(
 59    "NullPointer as element array is not allowed");
 60    }
 61  0 this.elements = new HashSet(Arrays.asList(elements));
 62    }
 63   
 64   
 65    /**
 66    * Constructs an element set.
 67    *
 68    * @param set contains the elements to put into the set
 69    * @throws IllegalArgumentException if <code>set</code> was a
 70    * NullPointer
 71    */
 72  6231 public ElementSet(final ElementSet set) {
 73  6231 if (set == null) {
 74  0 throw new IllegalArgumentException(
 75    "NullPointer as set is not allowed");
 76    }
 77  6231 this.elements = new HashSet(set.elements);
 78    }
 79   
 80   
 81    /**
 82    * Constructs an element set from all operands of an element.
 83    * The element must not be a symbol.
 84    *
 85    * @param element contains the elements to put into the set
 86    * (without the operator)
 87    * @throws IllegalArgumentException if <code>element</code> was a
 88    * NullPointer or was an atom.
 89    */
 90  0 public ElementSet(final ElementList element) {
 91  0 if (element == null) {
 92  0 throw new IllegalArgumentException(
 93    "NullPointer as element is not allowed");
 94    }
 95  0 if (element.isAtom()) {
 96  0 throw new IllegalArgumentException(
 97    "text as element is not allowed");
 98    }
 99  0 this.elements = new HashSet(element.getElements());
 100    }
 101   
 102   
 103    /**
 104    * Is element in set?
 105    *
 106    * @param element element to check for.
 107    * @return is <code>element</code> in this set?
 108    * @throws IllegalArgumentException if the element was a
 109    * NullPointer
 110    */
 111  394 public final boolean contains(final Element element) {
 112  394 if (element == null) {
 113  0 throw new IllegalArgumentException("NullPointer as element is not allowed");
 114    }
 115  394 return this.elements.contains(element);
 116    }
 117   
 118   
 119    /**
 120    * Is set empty?
 121    *
 122    * @return is set empty?
 123    */
 124  6231 public final boolean isEmpty() {
 125  6231 return elements.isEmpty();
 126    }
 127   
 128   
 129    /**
 130    * Is <code>set</code> a subset of this set?
 131    *
 132    * @param set set to check for.
 133    * @return is <code>set</code> a subset of this set?
 134    * @throws IllegalArgumentException if the set was a NullPointer
 135    */
 136  0 public final boolean isSubset(final ElementSet set) {
 137  0 if (set == null) {
 138  0 throw new IllegalArgumentException("NullPointer as set is not allowed");
 139    }
 140  0 return this.elements.containsAll(set.elements);
 141    }
 142   
 143   
 144    /**
 145    * Add an element to set. This object is after the method the
 146    * union of this set with {<code>element</code>}
 147    *
 148    * @param element element to put into the set
 149    * @return was <code>this</code> set changed?
 150    * @throws IllegalArgumentException if the element was a
 151    * NullPointer
 152    */
 153  4856 public final ElementSet add(final Element element) {
 154  4856 if (element == null) {
 155  0 throw new IllegalArgumentException("NullPointer as element is not allowed");
 156    }
 157  4856 elements.add(element);
 158  4856 return this;
 159    }
 160   
 161   
 162    /**
 163    * Add elements from another {@link ElementSet} to this set.
 164    * After this method this object is the union of the two sets.
 165    *
 166    * @param set add all elements that are here
 167    * @return was <code>this</code> set changed?
 168    * @throws IllegalArgumentException if the set was a
 169    * NullPointer
 170    */
 171  28796 public final ElementSet union(final ElementSet set) {
 172  28796 if (set == null) {
 173  0 throw new IllegalArgumentException(
 174    "NullPointer as set is not allowed");
 175    }
 176  28796 elements.addAll(set.elements);
 177  28796 return this;
 178    }
 179   
 180   
 181    /**
 182    * Remove an element from set.
 183    *
 184    * @param element Element to remove from the set
 185    * @return was <code>this</code> set changed?
 186    * @throws IllegalArgumentException if the element was a
 187    * NullPointer
 188    */
 189  302 public final ElementSet remove(final Element element) {
 190  302 if (element == null) {
 191  0 throw new IllegalArgumentException(
 192    "NullPointer as element is not allowed");
 193    }
 194  302 elements.remove(element);
 195  302 return this;
 196    }
 197   
 198   
 199    /**
 200    * Remove elements from another {@link ElementSet} from this set.
 201    * After this method this object is the asymmetric set difference of the
 202    * two sets: <code>this</code> \ <code>set</code>.
 203    *
 204    * @param set remove all elements that are in this set from
 205    * <code>this</code>
 206    * @return was <code>this</code> set changed?
 207    * @throws IllegalArgumentException if the set was a
 208    * NullPointer
 209    */
 210  0 public final ElementSet minus(final ElementSet set) {
 211  0 if (set == null) {
 212  0 throw new IllegalArgumentException(
 213    "NullPointer as set is not allowed");
 214    }
 215  0 this.elements.removeAll(set.elements);
 216  0 return this;
 217    }
 218   
 219   
 220    /**
 221    * Build the intersection.
 222    *
 223    * @param set check for these elements
 224    * @return was <code>this</code> set changed?
 225    * @throws IllegalArgumentException if the set was a
 226    * NullPointer
 227    */
 228  0 public final ElementSet intersection(final ElementSet set) {
 229  0 if (set == null) {
 230  0 throw new IllegalArgumentException(
 231    "NullPointer as set is not allowed");
 232    }
 233  0 this.elements.retainAll(set.elements);
 234  0 return this;
 235    }
 236   
 237   
 238    /**
 239    * Build a new intersection.
 240    *
 241    * @param set check for these elements
 242    * @return was <code>this</code> set changed?
 243    * @throws IllegalArgumentException if the set was a
 244    * NullPointer
 245    */
 246  6231 public final ElementSet newIntersection(final ElementSet set) {
 247  6231 if (set == null) {
 248  0 throw new IllegalArgumentException(
 249    "NullPointer as set is not allowed");
 250    }
 251  6231 final ElementSet result = new ElementSet(this);
 252  6231 result.elements.retainAll(set.elements);
 253  6231 return result;
 254    }
 255   
 256   
 257    /**
 258    * Return all elements that are only in one of both sets.
 259    * This method returns the symmetric set difference of the
 260    * two sets.
 261    *
 262    * @param set remove all elements that are in this set from
 263    * <code>this</code>
 264    * @return was <code>this</code> set changed?
 265    * @throws IllegalArgumentException if the set was a
 266    * NullPointer
 267    */
 268  0 public final ElementSet newDelta(final ElementSet set) {
 269  0 if (set == null) {
 270  0 throw new IllegalArgumentException(
 271    "NullPointer as set is not allowed");
 272    }
 273  0 final ElementSet union = new ElementSet(this);
 274  0 union.union(set);
 275  0 final ElementSet intersection = new ElementSet(this);
 276  0 intersection.intersection(set);
 277  0 union.minus(intersection);
 278  0 return union;
 279    }
 280   
 281  0 public final boolean equals(final Object obj) {
 282  0 if (obj == null) {
 283  0 return false;
 284    }
 285  0 if (obj.getClass() == ElementSet.class) {
 286  0 return this.elements.equals(((ElementSet) obj).elements);
 287    }
 288  0 return false;
 289    }
 290   
 291  0 public final int hashCode() {
 292  0 return elements.hashCode();
 293    }
 294   
 295  32 public final String toString() {
 296  32 final StringBuffer result = new StringBuffer();
 297  32 result.append("{");
 298  32 final Iterator iterator = elements.iterator();
 299  32 while (iterator.hasNext()) {
 300  32 result.append(iterator.next());
 301  32 if (iterator.hasNext()) {
 302  0 result.append(", ");
 303    }
 304    }
 305  32 result.append("}");
 306  32 return result.toString();
 307    }
 308   
 309    }