001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.se.dto.list;
017
018 import java.util.ArrayList;
019 import java.util.Arrays;
020 import java.util.List;
021
022 import org.qedeq.kernel.se.base.list.Atom;
023 import org.qedeq.kernel.se.base.list.Element;
024 import org.qedeq.kernel.se.base.list.ElementList;
025
026
027 /**
028 * Every Operator must inherit from this class. Its main function is to
029 * provide the standard implementation of {@link #equals}
030 * and {@link #hashCode}.
031 *
032 * @author Michael Meyling
033 */
034 public final class DefaultElementList implements ElementList {
035
036 /** Operator string, e.g. "AND". */
037 private final String operator;
038
039 /** Here is are the elements stored. */
040 private final List elements;
041
042
043 /**
044 * Constructs an element list with no elements.
045 *
046 * @param operator Operator name.
047 * @throws IllegalArgumentException Element or operator was a NullPointer.
048 */
049 public DefaultElementList(final String operator) {
050 if (operator == null) {
051 throw new IllegalArgumentException(
052 "NullPointer as operator is not allowed");
053 }
054 this.operator = operator;
055 this.elements = new ArrayList();
056 }
057
058 /**
059 * Constructs an element list.
060 *
061 * @param operator Operator name.
062 * @param elements the elements to make a list of
063 * @throws IllegalArgumentException Element or operator was a NullPointer.
064 */
065 public DefaultElementList(final String operator, final Element[] elements) {
066 if (operator == null) {
067 throw new IllegalArgumentException(
068 "NullPointer as operator is not allowed");
069 }
070 if (elements == null) {
071 throw new IllegalArgumentException(
072 "NullPointer as element array is not allowed");
073 }
074 this.operator = operator;
075 this.elements = new ArrayList(Arrays.asList(elements));
076 }
077
078 public final boolean isAtom() {
079 return false;
080 }
081
082 public final Atom getAtom() {
083 throw new ClassCastException("this is no " + Atom.class.getName()
084 + ", but a " + this.getClass().getName());
085 }
086
087 public final boolean isList() {
088 return true;
089 }
090
091 public final ElementList getList() {
092 return this;
093 }
094
095 public final String getOperator() {
096 return this.operator;
097 }
098
099 public final int size() {
100 return this.elements.size();
101 }
102
103 public final Element getElement(final int i) {
104 if (i >= 0 && i < elements.size()) {
105 return (Element) elements.get(i);
106 }
107 if (size() == 0) {
108 throw new IllegalArgumentException(
109 "there are no elements, therefore no element number "
110 + i);
111 }
112 throw new IllegalArgumentException(
113 "there is no element number " + i
114 + " the maximum element number is " + size());
115 }
116
117 public final List getElements() {
118 return this.elements;
119 }
120
121 public final boolean equals(final Object object) {
122 // first we check if we compare us with ourself
123 if (this == object) {
124 return true;
125 }
126 if (object == null) {
127 return false;
128 }
129 if (object.getClass() == this.getClass()) {
130 final ElementList element = (ElementList) object;
131 if (getOperator().equals(element.getOperator())
132 && size() == element.size()) {
133 for (int i = 0; i < size(); i++) {
134 if (!getElement(i).equals(element.getElement(i))) {
135 return false;
136 }
137 }
138 return true;
139 }
140 }
141 return false;
142 }
143
144 public final Element copy() {
145 final Element[] copied = new Element[size()];
146 for (int i = 0; i < size(); i++) {
147 copied[i] = getElement(i).copy();
148 }
149 return new DefaultElementList(getOperator(), copied);
150 }
151
152 public final Element replace(final Element search,
153 final Element replacement) {
154 if (this.equals(search)) {
155 return replacement.copy();
156 }
157 final Element[] replaced = new Element[size()];
158 for (int i = 0; i < size(); i++) {
159 replaced[i] = getElement(i).replace(search, replacement);
160 }
161 return new DefaultElementList(getOperator(), replaced);
162 }
163
164 public final void add(final Element element) {
165 if (element == null) {
166 throw new IllegalArgumentException(
167 "NullPointer couldn't be added");
168 }
169 this.elements.add(element);
170 }
171
172 public final void insert(final int position, final Element element) {
173 if (element == null) {
174 throw new IllegalArgumentException(
175 "NullPointer couldn't be inserted");
176 }
177 if (position >= 0 && position <= this.elements.size()) {
178 this.elements.add(position, element);
179 } else {
180 throw new IllegalArgumentException(
181 "allowed set is {0"
182 + (this.elements.size() > 0 ? ", .. "
183 + this.elements.size() : "")
184 + "}, and " + position + " is not in this set");
185 }
186 }
187
188 public final void replace(final int position, final Element element) {
189 if (element == null) {
190 throw new IllegalArgumentException(
191 "NullPointer is no element");
192 }
193 if (position >= 0 && position < this.elements.size()) {
194 this.elements.set(position, element);
195 return;
196 }
197 if (size() == 0) {
198 throw new IllegalArgumentException(
199 "there are no elements, therefore no element number "
200 + position + " could be replaced");
201 }
202 if (size() > 1) {
203 throw new IllegalArgumentException(
204 "there is no element number " + position
205 + ", you have to choose a number between 0 and " + (size() - 1));
206 }
207 throw new IllegalArgumentException(
208 "there is no element number " + position
209 + ", you can only ask for the element at position 0");
210 }
211
212 public final void remove(final int i) {
213 if (i >= 0 && i < elements.size()) {
214 elements.remove(i);
215 return;
216 }
217 if (size() == 0) {
218 throw new IllegalArgumentException(
219 "there are no elements, therefore no element number "
220 + i + " could be removed");
221 }
222 throw new IllegalArgumentException(
223 "there is no element number " + i
224 + " the maximum element number is " + size());
225 }
226
227 public final int hashCode() {
228 return toString().hashCode();
229 }
230
231 public final String toString() {
232 if (size() > 0) {
233 final StringBuffer buffer = new StringBuffer(getOperator() + " ( ");
234 for (int i = 0; i < size(); i++) {
235 if (i != 0) {
236 buffer.append(", ");
237 }
238 buffer.append(getElement(i));
239 }
240 buffer.append(")");
241 return buffer.toString();
242 }
243 return getOperator();
244 }
245
246 }
|