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