1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
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 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public final class ElementListImpl implements ElementList { |
38 |
| |
39 |
| |
40 |
| private final String operator; |
41 |
| |
42 |
| |
43 |
| private final List elements; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
10553
| public ElementListImpl(final String operator, final Element[] elements) {
|
54 |
10553
| if (operator == null) {
|
55 |
0
| throw new IllegalArgumentException(
|
56 |
| "NullPointer as operator is not allowed"); |
57 |
| } |
58 |
10553
| if (elements == null) {
|
59 |
0
| throw new IllegalArgumentException(
|
60 |
| "NullPointer as element array is not allowed"); |
61 |
| } |
62 |
10553
| this.operator = operator;
|
63 |
10553
| this.elements = new ArrayList(Arrays.asList(elements));
|
64 |
| } |
65 |
| |
66 |
3045
| public final boolean isAtom() {
|
67 |
3045
| 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 |
30291
| public final boolean isList() {
|
76 |
30291
| return true;
|
77 |
| } |
78 |
| |
79 |
59750
| public final ElementList getList() {
|
80 |
59750
| return this;
|
81 |
| } |
82 |
| |
83 |
107958
| public final String getOperator() {
|
84 |
107958
| return this.operator;
|
85 |
| } |
86 |
| |
87 |
237378
| public final int size() {
|
88 |
237378
| return this.elements.size();
|
89 |
| } |
90 |
| |
91 |
119896
| public final Element getElement(final int i) {
|
92 |
119896
| if (i >= 0 && i < elements.size()) {
|
93 |
119896
| 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 |
1755
| public final boolean equals(final Object object) {
|
110 |
1755
| if (object == null) {
|
111 |
0
| return false;
|
112 |
| } |
113 |
1755
| if (object.getClass() == this.getClass()) {
|
114 |
1755
| final ElementList element = (ElementList) object;
|
115 |
1755
| if (getOperator().equals(element.getOperator())
|
116 |
| && size() == element.size()) { |
117 |
1755
| for (int i = 0; i < size(); i++) {
|
118 |
1755
| if (!getElement(i).equals(element.getElement(i))) {
|
119 |
0
| return false;
|
120 |
| } |
121 |
| } |
122 |
1755
| return true;
|
123 |
| } |
124 |
| } |
125 |
0
| return false;
|
126 |
| } |
127 |
| |
128 |
4658
| public final Element copy() {
|
129 |
4658
| final Element[] copied = new Element[size()];
|
130 |
4658
| for (int i = 0; i < size(); i++) {
|
131 |
8085
| copied[i] = getElement(i).copy();
|
132 |
| } |
133 |
4658
| return new ElementListImpl(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 ElementListImpl(getOperator(), replaced);
|
146 |
| } |
147 |
| |
148 |
9965
| public final void add(final Element element) {
|
149 |
9965
| if (element == null) {
|
150 |
0
| throw new IllegalArgumentException(
|
151 |
| "NullPointer couldn't be added"); |
152 |
| } |
153 |
9965
| 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 |
26798
| public final int hashCode() {
|
205 |
26798
| return toString().hashCode();
|
206 |
| } |
207 |
| |
208 |
44020
| public final String toString() {
|
209 |
44020
| if (size() > 0) {
|
210 |
43988
| final StringBuffer buffer = new StringBuffer(getOperator() + " ( ");
|
211 |
43988
| for (int i = 0; i < size(); i++) {
|
212 |
54437
| if (i != 0) {
|
213 |
10449
| buffer.append(", ");
|
214 |
| } |
215 |
54437
| buffer.append(getElement(i));
|
216 |
| } |
217 |
43988
| buffer.append(")");
|
218 |
43988
| return buffer.toString();
|
219 |
| } |
220 |
32
| return getOperator();
|
221 |
| } |
222 |
| |
223 |
| } |