1 /***
2 * ***************************************************************
3 * JADE - Java Agent DEvelopment Framework is a framework to develop
4 * multi-agent systems in compliance with the FIPA specifications.
5 * Copyright (C) 2000 CSELT S.p.A.
6 *
7 * GNU Lesser General Public License
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation,
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 * **************************************************************
24 */
25 package JADE_SL.abs;
26
27 import JADE_SL.schema.ContentElementListSchema;
28 import java.util.*;
29 import java.io.PrintStream;
30
31 /***
32 * @author Federico Bergenti - Universita` di Parma
33 */
34 public class AbsContentElementList implements AbsContentElement {
35 private List elements = new ArrayList();
36 private String typeName = null;
37
38 /***
39 * Construct an Abstract descriptor to hold a content element list
40 */
41 public AbsContentElementList() {
42 typeName = ContentElementListSchema.BASE_NAME;
43 }
44
45 /***
46 * Add a new element (that must be a content element) to this
47 * content element list.
48 * @param element The element to add.
49 */
50 public void add(AbsContentElement element) {
51 elements.add(element);
52 }
53
54 /***
55 * Retrieves the number of elements in this content element list.
56 * @return The number of elements.
57 */
58 public int size() {
59 return elements.size();
60 }
61
62 /***
63 * Retrieves the <code>i</code>-th element in this content element list.
64 * @param i The index of the element to retrieve.
65 * @return The element.
66 */
67 public AbsContentElement get(int i) {
68 return (AbsContentElement) elements.get(i);
69 }
70
71 /***
72 * @return An <code>Iterator</code> over the elements of this
73 * content element list.
74 */
75 public Iterator iterator() {
76 return elements.iterator();
77 }
78
79 /***
80 * Clear all the elements in this content element list.
81 */
82 public void clear() {
83 elements.clear();
84 }
85
86 /***
87 * Test if a given content element is contained in this
88 * content element list.
89 * @return <code>true</code> if the given content element is contained
90 * in this content element list.
91 */
92 public boolean contains (AbsContentElement element) {
93 return elements.contains(element);
94 }
95
96 /***
97 * Returns the position of an element within this content element list.
98 * @return The position of an element within this content element list
99 * or -1 if the given element is not contained in this content element
100 * list.
101 */
102 public int indexOf (AbsContentElement element) {
103 return elements.indexOf(element);
104 }
105
106 /***
107 * Removes the element at the given position from this content
108 * element list.
109 * @return The removed element.
110 */
111 public AbsContentElement remove (int index) {
112 return (AbsContentElement)elements.remove(index);
113 }
114
115 /***
116 * Test if the content element list is empty.
117 * @return <code>true</code> if this content element list does
118 * not contain any element.
119 */
120 public boolean isEmpty () {
121 return elements.isEmpty();
122 }
123
124 /***
125 * Retrieve all elements in this content element list in the form of
126 * an array.
127 * @return An array containing all elements in this content element
128 * list.
129 */
130 public AbsContentElement[] toArray () {
131 int size = elements.size();
132 AbsContentElement[] tmp = new AbsContentElement[size];
133 for (int i = 0; i < size; i++)
134 tmp[i] = (AbsContentElement)elements.get(i);
135 return tmp;
136 }
137
138 protected void dump(int indent, PrintStream ps) {
139 for (int i = 0; i < indent; i++) {
140 ps.print(" ");
141 }
142
143 ps.println("(");
144
145 for (int i = 0; i < elements.size(); i++) {
146 ((AbsObjectImpl) elements.get(i)).dump(indent + 1, ps);
147 }
148
149 for (int i = 0; i < indent; i++) {
150 ps.print(" ");
151 }
152
153 ps.println(")");
154 }
155
156 /***
157 * @return The name of the type of the object held by this
158 * abstract descriptor.
159 * @see AbsObject#getTypeName()
160 */
161 public String getTypeName() {
162 return typeName;
163 }
164
165 /***
166 Makes no sense in the case of an AbsContentElementList that has no attribute
167 --> Just return null
168 */
169 public AbsObject getAbsObject(String name) {
170 return null;
171 }
172
173 /***
174 Makes no sense in the case of an AbsContentElementList that has no attribute
175 --> Just return null
176 */
177 public String[] getNames() {
178 return null;
179 }
180
181 /***
182 * Tests if this AbsContentElementList is grounded, i.e., if no one of its elements
183 * is associated with a variable
184 * @return <code>true</code> if the object is grounded.
185 */
186 public boolean isGrounded() {
187 return true;
188
189 }
190
191 /***
192 Makes no sense in the case of an AbsContentElementList that has no attribute
193 --> Just return 0
194 */
195 public int getCount() {
196 return 0;
197 }
198
199 public void dump() {
200 dump(0, System.out);
201 }
202
203 }
204