|
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 DefaultElementList 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 |
74070
| public DefaultElementList(final String operator, final Element[] elements) {
|
|
54 |
74069
| if (operator == null) {
|
|
55 |
0
| throw new IllegalArgumentException(
|
|
56 |
| "NullPointer as operator is not allowed"); |
|
57 |
| } |
|
58 |
74068
| if (elements == null) {
|
|
59 |
0
| throw new IllegalArgumentException(
|
|
60 |
| "NullPointer as element array is not allowed"); |
|
61 |
| } |
|
62 |
74067
| this.operator = operator;
|
|
63 |
74068
| this.elements = new ArrayList(Arrays.asList(elements));
|
|
64 |
| } |
|
65 |
| |
|
66 |
114200
| public final boolean isAtom() {
|
|
67 |
114200
| 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 |
423822
| public final boolean isList() {
|
|
76 |
423822
| return true;
|
|
77 |
| } |
|
78 |
| |
|
79 |
553247
| public final ElementList getList() {
|
|
80 |
553247
| return this;
|
|
81 |
| } |
|
82 |
| |
|
83 |
520796
| public final String getOperator() {
|
|
84 |
520796
| return this.operator;
|
|
85 |
| } |
|
86 |
| |
|
87 |
1634792
| public final int size() {
|
|
88 |
1634781
| return this.elements.size();
|
|
89 |
| } |
|
90 |
| |
|
91 |
1199041
| public final Element getElement(final int i) {
|
|
92 |
1199037
| if (i >= 0 && i < elements.size()) {
|
|
93 |
1199037
| 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 |
6670
| public final boolean equals(final Object object) {
|
|
110 |
6670
| if (object == null) {
|
|
111 |
0
| return false;
|
|
112 |
| } |
|
113 |
6670
| if (object.getClass() == this.getClass()) {
|
|
114 |
6670
| final ElementList element = (ElementList) object;
|
|
115 |
6670
| if (getOperator().equals(element.getOperator())
|
|
116 |
| && size() == element.size()) { |
|
117 |
6670
| for (int i = 0; i < size(); i++) {
|
|
118 |
6670
| if (!getElement(i).equals(element.getElement(i))) {
|
|
119 |
0
| return false;
|
|
120 |
| } |
|
121 |
| } |
|
122 |
6670
| return true;
|
|
123 |
| } |
|
124 |
| } |
|
125 |
0
| return false;
|
|
126 |
| } |
|
127 |
| |
|
128 |
0
| 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
| 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 |
124754
| public final void add(final Element element) {
|
|
149 |
124756
| if (element == null) {
|
|
150 |
0
| throw new IllegalArgumentException(
|
|
151 |
| "NullPointer couldn't be added"); |
|
152 |
| } |
|
153 |
124756
| 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 |
88259
| public final int hashCode() {
|
|
205 |
88259
| return toString().hashCode();
|
|
206 |
| } |
|
207 |
| |
|
208 |
140719
| public final String toString() {
|
|
209 |
140719
| if (size() > 0) {
|
|
210 |
140687
| final StringBuffer buffer = new StringBuffer(getOperator() + " ( ");
|
|
211 |
140687
| for (int i = 0; i < size(); i++) {
|
|
212 |
170289
| if (i != 0) {
|
|
213 |
29602
| buffer.append(", ");
|
|
214 |
| } |
|
215 |
170289
| buffer.append(getElement(i));
|
|
216 |
| } |
|
217 |
140687
| buffer.append(")");
|
|
218 |
140687
| return buffer.toString();
|
|
219 |
| } |
|
220 |
32
| return getOperator();
|
|
221 |
| } |
|
222 |
| |
|
223 |
| } |