View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  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.se.dto.list;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.qedeq.kernel.se.base.list.Atom;
23  import org.qedeq.kernel.se.base.list.Element;
24  import org.qedeq.kernel.se.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   * @author  Michael Meyling
33   */
34  public final class DefaultElementList implements ElementList {
35  
36      /** Operator string, e.g. "AND". */
37      private final String operator;
38  
39      /** Here is are the elements stored. */
40      private final List elements;
41  
42  
43      /**
44       * Constructs an element list with no elements.
45       *
46       * @param   operator    Operator name.
47       * @throws  IllegalArgumentException Element or operator was a NullPointer.
48       */
49      public DefaultElementList(final String operator) {
50          if (operator == null) {
51              throw new IllegalArgumentException(
52                  "NullPointer as operator is not allowed");
53          }
54          this.operator = operator;
55          this.elements = new ArrayList();
56      }
57  
58      /**
59       * Constructs an element list.
60       *
61       * @param   operator    Operator name.
62       * @param   elements the elements to make a list of
63       * @throws  IllegalArgumentException Element or operator was a NullPointer.
64       */
65      public DefaultElementList(final String operator, final Element[] elements) {
66          if (operator == null) {
67              throw new IllegalArgumentException(
68                  "NullPointer as operator is not allowed");
69          }
70          if (elements == null) {
71              throw new IllegalArgumentException(
72                  "NullPointer as element array is not allowed");
73          }
74          this.operator = operator;
75          this.elements = new ArrayList(Arrays.asList(elements));
76      }
77  
78      public final boolean isAtom() {
79          return false;
80      }
81  
82      public final Atom getAtom() {
83          throw new ClassCastException("this is no " + Atom.class.getName()
84              + ", but a " + this.getClass().getName());
85      }
86  
87      public final boolean isList() {
88          return true;
89      }
90  
91      public final ElementList getList() {
92          return this;
93      }
94  
95      public final String getOperator() {
96          return this.operator;
97      }
98  
99      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 }