Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../../img/srcFileCovDistChart5.png 74% of files have more coverage
68   223   46   3,78
48   163   0,68   18
18     2,56  
1    
 
  DefaultElementList       Line # 37 68 46 41% 0.41044775
 
  (27)
 
1    /* $Id: DefaultElementList.java,v 1.2 2008/03/27 05:16:26 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, 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.2 $
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  37011 toggle public DefaultElementList(final String operator, final Element[] elements) {
54  37011 if (operator == null) {
55  0 throw new IllegalArgumentException(
56    "NullPointer as operator is not allowed");
57    }
58  37011 if (elements == null) {
59  0 throw new IllegalArgumentException(
60    "NullPointer as element array is not allowed");
61    }
62  37011 this.operator = operator;
63  37011 this.elements = new ArrayList(Arrays.asList(elements));
64    }
65   
 
66  114174 toggle public final boolean isAtom() {
67  114174 return false;
68    }
69   
 
70  0 toggle public final Atom getAtom() {
71  0 throw new ClassCastException("this is no " + Atom.class.getName()
72    + ", but a " + this.getClass().getName());
73    }
74   
 
75  378533 toggle public final boolean isList() {
76  378533 return true;
77    }
78   
 
79  527505 toggle public final ElementList getList() {
80  527505 return this;
81    }
82   
 
83  457989 toggle public final String getOperator() {
84  457989 return this.operator;
85    }
86   
 
87  1380003 toggle public final int size() {
88  1380003 return this.elements.size();
89    }
90   
 
91  1041649 toggle public final Element getElement(final int i) {
92  1041649 if (i >= 0 && i < elements.size()) {
93  1041649 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 toggle public final List getElements() {
106  0 return this.elements;
107    }
108   
 
109  7041 toggle public final boolean equals(final Object object) {
110  7041 if (object == null) {
111  0 return false;
112    }
113  7041 if (object.getClass() == this.getClass()) {
114  7041 final ElementList element = (ElementList) object;
115  7041 if (getOperator().equals(element.getOperator())
116    && size() == element.size()) {
117  14082 for (int i = 0; i < size(); i++) {
118  7041 if (!getElement(i).equals(element.getElement(i))) {
119  0 return false;
120    }
121    }
122  7041 return true;
123    }
124    }
125  0 return false;
126    }
127   
 
128  0 toggle 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 toggle 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  61953 toggle public final void add(final Element element) {
149  61953 if (element == null) {
150  0 throw new IllegalArgumentException(
151    "NullPointer couldn't be added");
152    }
153  61953 this.elements.add(element);
154    }
155   
 
156  0 toggle 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 toggle 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 toggle 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  91373 toggle public final int hashCode() {
205  91373 return toString().hashCode();
206    }
207   
 
208  91433 toggle public final String toString() {
209  91433 if (size() > 0) {
210  91433 final StringBuffer buffer = new StringBuffer(getOperator() + " ( ");
211  182884 for (int i = 0; i < size(); i++) {
212  91451 if (i != 0) {
213  18 buffer.append(", ");
214    }
215  91451 buffer.append(getElement(i));
216    }
217  91433 buffer.append(")");
218  91433 return buffer.toString();
219    }
220  0 return getOperator();
221    }
222   
223    }