Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
../../../../../img/srcFileCovDistChart5.png 77% of files have more coverage
68   221   46   3.78
48   163   0.68   18
18     2.56  
1    
 
  DefaultElementList       Line # 35 68 46 41% 0.41044775
 
  (40)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, 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.dto.list;
17   
18    import java.util.ArrayList;
19    import java.util.Arrays;
20    import java.util.List;
21   
22    import org.qedeq.kernel.base.list.Atom;
23    import org.qedeq.kernel.base.list.Element;
24    import org.qedeq.kernel.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    * @version $Revision: 1.2 $
33    * @author Michael Meyling
34    */
 
35    public final class DefaultElementList implements ElementList {
36   
37    /** Operator string, e.g. "AND". */
38    private final String operator;
39   
40    /** Here is are the elements stored. */
41    private final List elements;
42   
43   
44    /**
45    * Constructs a element list.
46    *
47    * @param operator Operator name.
48    * @param elements the elements to make a list of
49    * @throws IllegalArgumentException Element or operator was a NullPointer.
50    */
 
51  39825 toggle public DefaultElementList(final String operator, final Element[] elements) {
52  39825 if (operator == null) {
53  0 throw new IllegalArgumentException(
54    "NullPointer as operator is not allowed");
55    }
56  39825 if (elements == null) {
57  0 throw new IllegalArgumentException(
58    "NullPointer as element array is not allowed");
59    }
60  39825 this.operator = operator;
61  39825 this.elements = new ArrayList(Arrays.asList(elements));
62    }
63   
 
64  124067 toggle public final boolean isAtom() {
65  124067 return false;
66    }
67   
 
68  0 toggle public final Atom getAtom() {
69  0 throw new ClassCastException("this is no " + Atom.class.getName()
70    + ", but a " + this.getClass().getName());
71    }
72   
 
73  402330 toggle public final boolean isList() {
74  402330 return true;
75    }
76   
 
77  558283 toggle public final ElementList getList() {
78  558283 return this;
79    }
80   
 
81  487966 toggle public final String getOperator() {
82  487966 return this.operator;
83    }
84   
 
85  1474547 toggle public final int size() {
86  1474547 return this.elements.size();
87    }
88   
 
89  1108595 toggle public final Element getElement(final int i) {
90  1108595 if (i >= 0 && i < elements.size()) {
91  1108595 return (Element) elements.get(i);
92    }
93  0 if (size() == 0) {
94  0 throw new IllegalArgumentException(
95    "there are no elements, therefore no element number "
96    + i);
97    }
98  0 throw new IllegalArgumentException(
99    "there is no element number " + i
100    + " the maximum element number is " + size());
101    }
102   
 
103  0 toggle public final List getElements() {
104  0 return this.elements;
105    }
106   
 
107  7582 toggle public final boolean equals(final Object object) {
108  7582 if (object == null) {
109  0 return false;
110    }
111  7582 if (object.getClass() == this.getClass()) {
112  7582 final ElementList element = (ElementList) object;
113  7582 if (getOperator().equals(element.getOperator())
114    && size() == element.size()) {
115  15164 for (int i = 0; i < size(); i++) {
116  7582 if (!getElement(i).equals(element.getElement(i))) {
117  0 return false;
118    }
119    }
120  7582 return true;
121    }
122    }
123  0 return false;
124    }
125   
 
126  0 toggle public final Element copy() {
127  0 final Element[] copied = new Element[size()];
128  0 for (int i = 0; i < size(); i++) {
129  0 copied[i] = getElement(i).copy();
130    }
131  0 return new DefaultElementList(getOperator(), copied);
132    }
133   
 
134  0 toggle public final Element replace(final Element search,
135    final Element replacement) {
136  0 if (this.equals(search)) {
137  0 return replacement.copy();
138    }
139  0 final Element[] replaced = new Element[size()];
140  0 for (int i = 0; i < size(); i++) {
141  0 replaced[i] = getElement(i).replace(search, replacement);
142    }
143  0 return new DefaultElementList(getOperator(), replaced);
144    }
145   
 
146  66435 toggle public final void add(final Element element) {
147  66435 if (element == null) {
148  0 throw new IllegalArgumentException(
149    "NullPointer couldn't be added");
150    }
151  66435 this.elements.add(element);
152    }
153   
 
154  0 toggle public final void insert(final int position, final Element element) {
155  0 if (element == null) {
156  0 throw new IllegalArgumentException(
157    "NullPointer couldn't be inserted");
158    }
159  0 if (position >= 0 && position <= this.elements.size()) {
160  0 this.elements.add(position, element);
161    } else {
162  0 throw new IllegalArgumentException(
163    "allowed set is {0"
164  0 + (this.elements.size() > 0 ? ", .. "
165    + this.elements.size() : "")
166    + "}, and " + position + " is not in this set");
167    }
168    }
169   
 
170  0 toggle public final void replace(final int position, final Element element) {
171  0 if (element == null) {
172  0 throw new IllegalArgumentException(
173    "NullPointer couldn't be set");
174    }
175  0 if (position >= 0 && position < this.elements.size()) {
176  0 this.elements.set(position, element);
177    }
178  0 if (size() == 0) {
179  0 throw new IllegalArgumentException(
180    "there are no elements, therefore no element number "
181    + position + " could be replaced");
182    }
183  0 throw new IllegalArgumentException(
184    "there is no element number " + position
185    + " the maximum element number is " + size());
186    }
187   
 
188  0 toggle public final void remove(final int i) {
189  0 if (i >= 0 && i < elements.size()) {
190  0 elements.remove(i);
191    }
192  0 if (size() == 0) {
193  0 throw new IllegalArgumentException(
194    "there are no elements, therefore no element number "
195    + i + " could be removed");
196    }
197  0 throw new IllegalArgumentException(
198    "there is no element number " + i
199    + " the maximum element number is " + size());
200    }
201   
 
202  97759 toggle public final int hashCode() {
203  97759 return toString().hashCode();
204    }
205   
 
206  97846 toggle public final String toString() {
207  97846 if (size() > 0) {
208  97846 final StringBuffer buffer = new StringBuffer(getOperator() + " ( ");
209  195728 for (int i = 0; i < size(); i++) {
210  97882 if (i != 0) {
211  36 buffer.append(", ");
212    }
213  97882 buffer.append(getElement(i));
214    }
215  97846 buffer.append(")");
216  97846 return buffer.toString();
217    }
218  0 return getOperator();
219    }
220   
221    }