Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
78   246   51   4.11
54   181   0.65   19
19     2.68  
1    
 
  DefaultElementList       Line # 34 78 51 100% 1.0
 
  (401)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.se.dto.list;
17   
18    import java.util.ArrayList;
19    import java.util.Arrays;
20    import java.util.List;
21   
22    import org.qedeq.kernel.se.base.list.Atom;
23    import org.qedeq.kernel.se.base.list.Element;
24    import org.qedeq.kernel.se.base.list.ElementList;
25   
26   
27    /**
28    * Every Operator must inherit from this class. Its main function is to
29    * provide the standard implementation of {@link #equals}
30    * and {@link #hashCode}.
31    *
32    * @author Michael Meyling
33    */
 
34    public final class DefaultElementList implements ElementList {
35   
36    /** Operator string, e.g. "AND". */
37    private final String operator;
38   
39    /** Here is are the elements stored. */
40    private final List elements;
41   
42   
43    /**
44    * Constructs an element list with no elements.
45    *
46    * @param operator Operator name.
47    * @throws IllegalArgumentException Element or operator was a NullPointer.
48    */
 
49  1326234 toggle public DefaultElementList(final String operator) {
50  1326234 if (operator == null) {
51  1 throw new IllegalArgumentException(
52    "NullPointer as operator is not allowed");
53    }
54  1326233 this.operator = operator;
55  1326233 this.elements = new ArrayList();
56    }
57   
58    /**
59    * Constructs an element list.
60    *
61    * @param operator Operator name.
62    * @param elements the elements to make a list of
63    * @throws IllegalArgumentException Element or operator was a NullPointer.
64    */
 
65  367303 toggle public DefaultElementList(final String operator, final Element[] elements) {
66  367303 if (operator == null) {
67  1 throw new IllegalArgumentException(
68    "NullPointer as operator is not allowed");
69    }
70  367302 if (elements == null) {
71  1 throw new IllegalArgumentException(
72    "NullPointer as element array is not allowed");
73    }
74  367301 this.operator = operator;
75  367301 this.elements = new ArrayList(Arrays.asList(elements));
76    }
77   
 
78  30393512 toggle public final boolean isAtom() {
79  30393512 return false;
80    }
81   
 
82  2 toggle public final Atom getAtom() {
83  2 throw new ClassCastException("this is no " + Atom.class.getName()
84    + ", but a " + this.getClass().getName());
85    }
86   
 
87  3970546 toggle public final boolean isList() {
88  3970546 return true;
89    }
90   
 
91  355994351 toggle public final ElementList getList() {
92  355994351 return this;
93    }
94   
 
95  2005906556 toggle public final String getOperator() {
96  2005906556 return this.operator;
97    }
98   
 
99  1739709374 toggle public final int size() {
100  1739709374 return this.elements.size();
101    }
102   
 
103  1426534039 toggle public final Element getElement(final int i) {
104  1426534039 if (i >= 0 && i < elements.size()) {
105  1426534034 return (Element) elements.get(i);
106    }
107  5 if (size() == 0) {
108  3 throw new IllegalArgumentException(
109    "there are no elements, therefore no element number "
110    + i);
111    }
112  2 throw new IllegalArgumentException(
113    "there is no element number " + i
114    + " the maximum element number is " + size());
115    }
116   
 
117  3 toggle public final List getElements() {
118  3 return this.elements;
119    }
120   
 
121  987489872 toggle public final boolean equals(final Object object) {
122    // first we check if we compare us with ourself
123  987489872 if (this == object) {
124  1898616 return true;
125    }
126  985591256 if (object == null) {
127  38 return false;
128    }
129  985591218 if (object.getClass() == this.getClass()) {
130  985591214 final ElementList element = (ElementList) object;
131  985591214 if (getOperator().equals(element.getOperator())
132    && size() == element.size()) {
133  597187334 for (int i = 0; i < size(); i++) {
134  572091445 if (!getElement(i).equals(element.getElement(i))) {
135  523215138 return false;
136    }
137    }
138  25095889 return true;
139    }
140    }
141  437280191 return false;
142    }
143   
 
144  417 toggle public final Element copy() {
145  417 final Element[] copied = new Element[size()];
146  869 for (int i = 0; i < size(); i++) {
147  452 copied[i] = getElement(i).copy();
148    }
149  417 return new DefaultElementList(getOperator(), copied);
150    }
151   
 
152  858 toggle public final Element replace(final Element search,
153    final Element replacement) {
154  858 if (this.equals(search)) {
155  214 return replacement.copy();
156    }
157  644 final Element[] replaced = new Element[size()];
158  1795 for (int i = 0; i < size(); i++) {
159  1151 replaced[i] = getElement(i).replace(search, replacement);
160    }
161  644 return new DefaultElementList(getOperator(), replaced);
162    }
163   
 
164  2784171 toggle public final void add(final Element element) {
165  2784171 if (element == null) {
166  1 throw new IllegalArgumentException(
167    "NullPointer couldn't be added");
168    }
169  2784170 this.elements.add(element);
170    }
171   
 
172  9 toggle public final void insert(final int position, final Element element) {
173  9 if (element == null) {
174  1 throw new IllegalArgumentException(
175    "NullPointer couldn't be inserted");
176    }
177  8 if (position >= 0 && position <= this.elements.size()) {
178  4 this.elements.add(position, element);
179    } else {
180  4 throw new IllegalArgumentException(
181    "allowed set is {0"
182  4 + (this.elements.size() > 0 ? ", .. "
183    + this.elements.size() : "")
184    + "}, and " + position + " is not in this set");
185    }
186    }
187   
 
188  8 toggle public final void replace(final int position, final Element element) {
189  8 if (element == null) {
190  1 throw new IllegalArgumentException(
191    "NullPointer is no element");
192    }
193  7 if (position >= 0 && position < this.elements.size()) {
194  1 this.elements.set(position, element);
195  1 return;
196    }
197  6 if (size() == 0) {
198  3 throw new IllegalArgumentException(
199    "there are no elements, therefore no element number "
200    + position + " could be replaced");
201    }
202  3 if (size() > 1) {
203  1 throw new IllegalArgumentException(
204    "there is no element number " + position
205    + ", you have to choose a number between 0 and " + (size() - 1));
206    }
207  2 throw new IllegalArgumentException(
208    "there is no element number " + position
209    + ", you can only ask for the element at position 0");
210    }
211   
 
212  8 toggle public final void remove(final int i) {
213  8 if (i >= 0 && i < elements.size()) {
214  5 elements.remove(i);
215  5 return;
216    }
217  3 if (size() == 0) {
218  1 throw new IllegalArgumentException(
219    "there are no elements, therefore no element number "
220    + i + " could be removed");
221    }
222  2 throw new IllegalArgumentException(
223    "there is no element number " + i
224    + " the maximum element number is " + size());
225    }
226   
 
227  1341939 toggle public final int hashCode() {
228  1341939 return toString().hashCode();
229    }
230   
 
231  1345987 toggle public final String toString() {
232  1345987 if (size() > 0) {
233  1345965 final StringBuffer buffer = new StringBuffer(getOperator() + " ( ");
234  2692794 for (int i = 0; i < size(); i++) {
235  1346829 if (i != 0) {
236  864 buffer.append(", ");
237    }
238  1346829 buffer.append(getElement(i));
239    }
240  1345965 buffer.append(")");
241  1345965 return buffer.toString();
242    }
243  22 return getOperator();
244    }
245   
246    }