KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > data > PArrayList


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: PArrayList.java,v 1.4 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.data;
21
22 import java.io.*;
23 import java.util.*;
24
25 /**
26  * <p>This class extends AbstractPData (which provides the
27  * parental/statemap functionality) and delegates most of
28  * the List functionality back to an underlying ArrayList
29  */

30 public class PArrayList extends AbstractPData implements PList {
31
32     private List list = new ArrayList();
33
34
35     //--------------- PArrayList ---------------------------------
36
/**
37      * Set the underlying store (you only really need to use
38      * this method if you want to store the data in something
39      * other than an ArrayList, which is the default)
40      *
41      * @param ilist the List structure to be used as the underlying
42      * store
43      */

44     public void setStore(List ilist) {
45         list = ilist;
46     }
47     
48     //--------------- PList --------------------------------------
49
/**
50      * Inserts the specified element at the specified position in this list
51      * (optional operation).
52      */

53     public void add(int index, Object JavaDoc el) {
54         //this check is used to ensure the parental hierarchy is automatically
55
//maintained. If you add an element to this list and that element implements
56
//PData and that element has inheritParents=true, then this list should
57
//automatically automatically become that objects parent
58
if (el!=null && el instanceof PData) {
59             PData pdata = (PData) el;
60 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(this);
61
if (pdata.isInheritParents() && pdata.getParent()==null) pdata.setParent(this); //csc_012003.1
62
}
63         
64         //add the element to the list
65
list.add(index, el);
66     }
67
68     /**
69      * Appends the specified element to the end of this list (optional
70      * operation).
71      */

72     public boolean add(Object JavaDoc el) {
73         //this check is used to ensure the parental hierarchy is automatically
74
//maintained. If you add an element to this list and that element implements
75
//PData and that element has inheritParents=true, then this list should
76
//automatically automatically become that objects parent
77
if (el!=null && el instanceof PData) {
78             PData pdata = (PData) el;
79 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(this);
80
if (pdata.isInheritParents() && pdata.getParent()==null) pdata.setParent(this); //csc_012003.1
81
}
82
83         //add the element to the list
84
return list.add(el);
85     }
86
87     /**
88      * Appends all of the elements in the specified collection to the end
89      * of this list, in the order that they are returned by the specified
90      * collection's iterator (optional operation).
91      */

92     public boolean addAll(Collection c) {
93         //this check is used to ensure the parental hierarchy is automatically
94
//maintained. If you add an element to this list and that element implements
95
//PData and that element has inheritParents=true, then this list should
96
//automatically automatically become that objects parent. Here we have to
97
//iterate through all the elements in the collection making this check...
98
if (c!=null) {
99             Iterator it = c.iterator();
100             while (it.hasNext()) {
101                 Object JavaDoc el = it.next();
102                 if (el!=null && el instanceof PData) {
103                     PData pdata = (PData) el;
104 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(this);
105
if (pdata.isInheritParents() && pdata.getParent()==null) pdata.setParent(this); //csc_012003.1
106
}
107             }
108         }
109
110         //add the collection to the list
111
return list.addAll(c);
112     }
113
114     /**
115      * Inserts all of the elements in the specified collection into this
116      * list at the specified position (optional operation).
117      */

118     public boolean addAll(int index, Collection c) {
119         //this check is used to ensure the parental hierarchy is automatically
120
//maintained. If you add an element to this list and that element implements
121
//PData and that element has inheritParents=true, then this list should
122
//automatically automatically become that objects parent. Here we have to
123
//iterate through all the elements in the collection making this check...
124
if (c!=null) {
125             Iterator it = c.iterator();
126             while (it.hasNext()) {
127                 Object JavaDoc el = it.next();
128                 if (el!=null && el instanceof PData) {
129                     PData pdata = (PData) el;
130 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(this);
131
if (pdata.isInheritParents() && pdata.getParent()==null) pdata.setParent(this); //csc_012003.1
132
}
133             }
134         }
135     
136         //add the collection to the list
137
return list.addAll(index, c);
138     }
139
140     /**
141      * Removes all of the elements from this list (optional operation).
142      */

143     public void clear() {
144         //we need to start by clearing parents for any PData items in the list
145
Iterator it = list.iterator();
146         while (it.hasNext()) {
147             Object JavaDoc el = it.next();
148             if (el!=null && el instanceof PData) {
149                 PData pdata = (PData) el;
150 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
151
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
152
}
153         }
154     
155         //now clear the list
156
list.clear();
157     }
158
159     /**
160      * Returns true if this list contains the specified element.
161      */

162     public boolean contains(Object JavaDoc el) {
163         return list.contains(el);
164     }
165
166     /**
167      * Returns true if this list contains all of the elements of the
168      * specified collection.
169      */

170     public boolean containsAll(Collection c) {
171         //the containsAll implementation in AbstractCollection
172
//throws a NullPointerException if our list or c's list
173
//is empty...this manual check prevents that error: if
174
//the list is empty we say that it contains all items
175
//if the other list is also empty.
176
if (list.size()<1) return c.size()<1;
177         
178         //else just delegate to the underlying list function
179
return list.containsAll(c);
180     }
181
182     /**
183      * Returns the element at the specified position in this list.
184      */

185     public Object JavaDoc get(int index) {
186         return list.get(index);
187     }
188
189     /**
190      * Returns the index in this list of the first occurrence of the
191      * specified element, or -1 if this list does not contain this element.
192      */

193     public int indexOf(Object JavaDoc el) {
194         return list.indexOf(el);
195     }
196
197     /**
198      * Returns true if this list contains no elements.
199      */

200     public boolean isEmpty() {
201         return list.isEmpty();
202     }
203
204     /**
205      * Returns an iterator over the elements in this list in proper sequence.
206      */

207     public Iterator iterator() {
208         return list.iterator();
209     }
210
211     /**
212      * Returns the index in this list of the last occurrence of the
213      * specified element, or -1 if this list does not contain this element.
214      */

215     public int lastIndexOf(Object JavaDoc el) {
216         return list.lastIndexOf(el);
217     }
218
219     /**
220      * Returns a list iterator of the elements in this list (in proper sequence).
221      */

222     public ListIterator listIterator() {
223         return list.listIterator();
224     }
225
226     /**
227      * Returns a list iterator of the elements in this list (in proper sequence),
228      * starting at the specified position in this list.
229      */

230     public ListIterator listIterator(int index) {
231         return listIterator(index);
232     }
233
234     /**
235      * Removes the element at the specified position in this list (optional
236      * operation).
237      */

238     public Object JavaDoc remove(int index) {
239         //this check is to ensure that the parental relationship is automatically
240
//cleaned up from the item currently at the specified index. The idea here
241
//is that if you're removing an element (which you are effectively doing
242
//via a set) then that element should no longer point to this object as its
243
//parent.
244
Object JavaDoc curEl = list.get(index);
245         if (curEl!=null && curEl instanceof PData) {
246             PData pdata = (PData) curEl;
247 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
248
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
249
}
250
251         //remove the element at the specified index from the list
252
return list.remove(index);
253     }
254
255     /**
256      * Removes the first occurrence in this list of the specified element
257      * (optional operation).
258      */

259     public boolean remove(Object JavaDoc el) {
260         //this check is to ensure that the parental relationship is automatically
261
//cleaned up from the item currently at the specified index. The idea here
262
//is that if you're removing an element (which you are effectively doing
263
//via a set) then that element should no longer point to this object as its
264
//parent.
265
if (el!=null && el instanceof PData && list.contains(el)) {
266             PData pdata = (PData) el;
267 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
268
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
269
}
270
271         //remove the element from the list
272
return list.remove(el);
273     }
274
275     /**
276      * Removes from this list all the elements that are contained in the
277      * specified collection (optional operation).
278      */

279     public boolean removeAll(Collection c) {
280         //this check is to ensure that the parental relationship is automatically
281
//cleaned up from the item currently at the specified index. The idea here
282
//is that if you're removing an element (which you are effectively doing
283
//via a set) then that element should no longer point to this object as its
284
//parent.
285
if (c!=null) {
286             Iterator it = c.iterator();
287             while (it.hasNext()) {
288                 Object JavaDoc el = it.next();
289                 if (el!=null && el instanceof PData) {
290                     PData pdata = (PData) el;
291 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
292
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
293
}
294             }
295         }
296
297         //remove the collection from the list
298
return list.removeAll(c);
299     }
300
301     /**
302      * Retains only the elements in this list that are contained in the
303      * specified collection (optional operation).
304      */

305     public boolean retainAll(Collection c) {
306         //this check is to ensure that the parental relationship is automatically
307
//cleaned up from the item currently at the specified index. The idea here
308
//is that if you're removing an element (which you are effectively doing
309
//via a set) then that element should no longer point to this object as its
310
//parent.
311
if (c!=null) {
312             Iterator it = list.iterator();
313             while (it.hasNext()) {
314                 Object JavaDoc el = it.next();
315                 if (el!=null && el instanceof PData) {
316                     PData pdata = (PData) el;
317 //csc_012003.1 if (pdata.isInheritParents() && !(c.contains(el))) pdata.setParent(null);
318
if (pdata.isInheritParents() && pdata.getParent()==this && !(c.contains(el))) pdata.setParent(null); //csc_012003.1
319
}
320             }
321         }
322
323         //retain the items in the collection
324
return list.retainAll(c);
325     }
326
327     /**
328      * Replaces the element at the specified position in this list with the
329      * specified element (optional operation).
330      */

331     public Object JavaDoc set(int index, Object JavaDoc el) {
332         //this check is to ensure that the parental relationship is automatically
333
//cleaned up from the item currently at the specified index. The idea here
334
//is that if you're removing an element (which you are effectively doing
335
//via a set) then that element should no longer point to this object as its
336
//parent.
337
Object JavaDoc curEl = list.get(index);
338         if (curEl!=null && curEl instanceof PData) {
339             PData pdata = (PData) curEl;
340 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
341
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
342
}
343
344         //this check is used to ensure the parental hierarchy is automatically
345
//maintained. If you add an element to this list and that element implements
346
//PData and that element has inheritParents=true, then this list should
347
//automatically automatically become that objects parent
348
if (el!=null && el instanceof PData) {
349             PData pdata = (PData) el;
350 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(this);
351
if (pdata.isInheritParents() && pdata.getParent()==null) pdata.setParent(this); //csc_012003.1
352
}
353
354         //set the element in the list
355
return list.set(index, el);
356     }
357
358     /**
359      * Returns the number of elements in this list.
360      */

361     public int size() {
362         return list.size();
363     }
364
365     /**
366      * Returns a view of the portion of this list between the specified fromIndex,
367      * inclusive, and toIndex, exclusive.
368      */

369     public List subList(int fromIndex, int toIndex){
370         return list.subList(fromIndex, toIndex);
371     }
372
373     /**
374      * Returns an array containing all of the elements in this list in proper
375      * sequence.
376      */

377     public Object JavaDoc[] toArray() {
378         return list.toArray();
379     }
380
381     /**
382      * Returns an array containing all of the elements in this list in proper
383      * sequence; the runtime type of the returned array is that of the specified
384      * array.
385      */

386     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
387         return list.toArray(a);
388     }
389
390
391     //--------------- Cloneable ----------------------------------
392
/**
393      * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
394      * elements themselves are not copied.)
395      *
396      * @return a clone of this <tt>ArrayList</tt> instance.
397      */

398     public Object JavaDoc clone() {
399         try {
400             PArrayList pal = (PArrayList) super.clone();
401             pal.list = new ArrayList(list);
402             return pal;
403         } catch (CloneNotSupportedException JavaDoc e) {
404             // this shouldn't happen, since we are Cloneable
405
throw new InternalError JavaDoc();
406         }
407     }
408
409
410     //--------------- Object -------------------------------------
411
/**
412      * Check object for equality. Will return true if the incoming
413      * object is a) non-null, b) the size of the underlying list
414      * structures is the same and c) the list containsAll() the
415      * same elements
416      *
417      * @param obj the object we're comparing against
418      * @return true if the objects are equal
419      */

420     public boolean equals(Object JavaDoc obj) {
421         if (obj==null) return false;
422         if (obj==this) return true;
423         if (!(obj instanceof PList)) return false;
424         PList pl = (PList) obj;
425         if (this.size()!=pl.size()) return false;
426         return (this.containsAll(pl));
427     }
428
429     /**
430      * Returns the hash code value for this list.
431      */

432     public int hashCode() {
433         return list.hashCode();
434     }
435
436
437     //--------------- Utility Methods ----------------------------
438

439
440
441 }
442
Popular Tags