Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mai 10 2007 03:16:40 CEST
file stats: LOC: 223   Methods: 18
NCLOC: 163   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultElementList.java 33,3% 44,1% 61,1% 42,5%
coverage coverage
 1    /* $Id: DefaultElementList.java,v 1.1 2007/05/10 00:37:52 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, 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.ArrayList;
 21    import java.util.Arrays;
 22    import java.util.List;
 23   
 24    import org.qedeq.kernel.base.list.Atom;
 25    import org.qedeq.kernel.base.list.Element;
 26    import org.qedeq.kernel.base.list.ElementList;
 27   
 28   
 29    /**
 30    * Every Operator must inherit from this class. Its main function is to
 31    * provide the standard implementation of {@link #equals}
 32    * and {@link #hashCode}.
 33    *
 34    * @version $Revision: 1.1 $
 35    * @author Michael Meyling
 36    */
 37    public final class DefaultElementList implements ElementList {
 38   
 39    /** Operator string, e.g. "AND". */
 40    private final String operator;
 41   
 42    /** Here is are the elements stored. */
 43    private final List elements;
 44   
 45   
 46    /**
 47    * Constructs a element list.
 48    *
 49    * @param operator Operator name.
 50    * @param elements the elements to make a list of
 51    * @throws IllegalArgumentException Element or operator was a NullPointer.
 52    */
 53  22801 public DefaultElementList(final String operator, final Element[] elements) {
 54  22801 if (operator == null) {
 55  0 throw new IllegalArgumentException(
 56    "NullPointer as operator is not allowed");
 57    }
 58  22801 if (elements == null) {
 59  0 throw new IllegalArgumentException(
 60    "NullPointer as element array is not allowed");
 61    }
 62  22801 this.operator = operator;
 63  22801 this.elements = new ArrayList(Arrays.asList(elements));
 64    }
 65   
 66  94563 public final boolean isAtom() {
 67  94563 return false;
 68    }
 69   
 70  0 public final Atom getAtom() {
 71  0 throw new ClassCastException("this is no " + Atom.class.getName()
 72    + ", but a " + this.getClass().getName());
 73    }
 74   
 75  280296 public final boolean isList() {
 76  280296 return true;
 77    }
 78   
 79  399041 public final ElementList getList() {
 80  399041 return this;
 81    }
 82   
 83  371487 public final String getOperator() {
 84  371487 return this.operator;
 85    }
 86   
 87  1071508 public final int size() {
 88  1071508 return this.elements.size();
 89    }
 90   
 91  828600 public final Element getElement(final int i) {
 92  828600 if (i >= 0 && i < elements.size()) {
 93  828600 return (Element) elements.get(i);
 94    }
 95  0 if (size() == 0) {
 96  0 throw new IllegalArgumentException(
 97    "there are no elements, therefore no element number "
 98    + i);
 99    }
 100  0 throw new IllegalArgumentException(
 101    "there is no element number " + i
 102    + " the maximum element number is " + size());
 103    }
 104   
 105  0 public final List getElements() {
 106  0 return this.elements;
 107    }
 108   
 109  4584 public final boolean equals(final Object object) {
 110  4584 if (object == null) {
 111  0 return false;
 112    }
 113  4584 if (object.getClass() == this.getClass()) {
 114  4584 final ElementList element = (ElementList) object;
 115  4584 if (getOperator().equals(element.getOperator())
 116    && size() == element.size()) {
 117  4584 for (int i = 0; i < size(); i++) {
 118  4584 if (!getElement(i).equals(element.getElement(i))) {
 119  0 return false;
 120    }
 121    }
 122  4584 return true;
 123    }
 124    }
 125  0 return false;
 126    }
 127   
 128  0 public final Element copy() {
 129  0 final Element[] copied = new Element[size()];
 130  0 for (int i = 0; i < size(); i++) {
 131  0 copied[i] = getElement(i).copy();
 132    }
 133  0 return new DefaultElementList(getOperator(), copied);
 134    }
 135   
 136  0 public final Element replace(final Element search,
 137    final Element replacement) {
 138  0 if (this.equals(search)) {
 139  0 return replacement.copy();
 140    }
 141  0 final Element[] replaced = new Element[size()];
 142  0 for (int i = 0; i < size(); i++) {
 143  0 replaced[i] = getElement(i).replace(search, replacement);
 144    }
 145  0 return new DefaultElementList(getOperator(), replaced);
 146    }
 147   
 148  39088 public final void add(final Element element) {
 149  39088 if (element == null) {
 150  0 throw new IllegalArgumentException(
 151    "NullPointer couldn't be added");
 152    }
 153  39088 this.elements.add(element);
 154    }
 155   
 156  0 public final void insert(final int position, final Element element) {
 157  0 if (element == null) {
 158  0 throw new IllegalArgumentException(
 159    "NullPointer couldn't be inserted");
 160    }
 161  0 if (position >= 0 && position <= this.elements.size()) {
 162  0 this.elements.add(position, element);
 163    } else {
 164  0 throw new IllegalArgumentException(
 165    "allowed set is {0"
 166  0 + (this.elements.size() > 0 ? ", .. "
 167    + this.elements.size() : "")
 168    + "}, and " + position + " is not in this set");
 169    }
 170    }
 171   
 172  0 public final void replace(final int position, final Element element) {
 173  0 if (element == null) {
 174  0 throw new IllegalArgumentException(
 175    "NullPointer couldn't be set");
 176    }
 177  0 if (position >= 0 && position < this.elements.size()) {
 178  0 this.elements.set(position, element);
 179    }
 180  0 if (size() == 0) {
 181  0 throw new IllegalArgumentException(
 182    "there are no elements, therefore no element number "
 183    + position + " could be replaced");
 184    }
 185  0 throw new IllegalArgumentException(
 186    "there is no element number " + position
 187    + " the maximum element number is " + size());
 188    }
 189   
 190  0 public final void remove(final int i) {
 191  0 if (i >= 0 && i < elements.size()) {
 192  0 elements.remove(i);
 193    }
 194  0 if (size() == 0) {
 195  0 throw new IllegalArgumentException(
 196    "there are no elements, therefore no element number "
 197    + i + " could be removed");
 198    }
 199  0 throw new IllegalArgumentException(
 200    "there is no element number " + i
 201    + " the maximum element number is " + size());
 202    }
 203   
 204  69523 public final int hashCode() {
 205  69523 return toString().hashCode();
 206    }
 207   
 208  70403 public final String toString() {
 209  70403 if (size() > 0) {
 210  70387 final StringBuffer buffer = new StringBuffer(getOperator() + " ( ");
 211  70387 for (int i = 0; i < size(); i++) {
 212  70880 if (i != 0) {
 213  493 buffer.append(", ");
 214    }
 215  70880 buffer.append(getElement(i));
 216    }
 217  70387 buffer.append(")");
 218  70387 return buffer.toString();
 219    }
 220  16 return getOperator();
 221    }
 222   
 223    }