DefaultElementList.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  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 >= && i < elements.size()) {
105             return (Elementelements.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 = (ElementListobject;
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 >= && 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() ", .. "
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 couldn't be set");
192         }
193         if (position >= && position < this.elements.size()) {
194             this.elements.set(position, element);
195         }
196         if (size() == 0)  {
197             throw new IllegalArgumentException(
198                 "there are no elements, therefore no element number "
199                 + position + " could be replaced");
200         }
201         throw new IllegalArgumentException(
202             "there is no element number " + position
203             " the maximum element number is " + size());
204     }
205 
206     public final void remove(final int i) {
207         if (i >= && i < elements.size()) {
208             elements.remove(i);
209             return;
210         }
211         if (size() == 0)  {
212             throw new IllegalArgumentException(
213                 "there are no elements, therefore no element number "
214                 + i + " could be removed");
215         }
216         throw new IllegalArgumentException(
217             "there is no element number " + i
218             " the maximum element number is " + size());
219     }
220 
221     public final int hashCode() {
222         return toString().hashCode();
223     }
224 
225     public final String toString() {
226         if (size() 0) {
227             final StringBuffer buffer = new StringBuffer(getOperator() " ( ");
228             for (int i = 0; i < size(); i++) {
229                 if (i != 0) {
230                     buffer.append(", ");
231                 }
232                 buffer.append(getElement(i));
233             }
234             buffer.append(")");
235             return buffer.toString();
236         }
237         return getOperator();
238     }
239 
240 }