KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > collections > BaseSequentialListImpl


1 /*
2  * $Id: BaseSequentialListImpl.java,v 1.1 2003/04/24 15:14:33 leomekenkamp Exp $
3  * This file is based on TreeMap.java from GNU Classpath. Quote:
4
5 AbstractSequentialList.java -- List implementation for sequential access
6 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
7
8 This file is part of GNU Classpath.
9
10 GNU Classpath is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GNU Classpath is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Classpath; see the file COPYING. If not, write to the
22 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307 USA.
24
25 Linking this library statically or dynamically with other modules is
26 making a combined work based on this library. Thus, the terms and
27 conditions of the GNU General Public License cover the whole
28 combination.
29
30 As a special exception, the copyright holders of this library give you
31 permission to link this library with independent modules to produce an
32 executable, regardless of the license terms of these independent
33 modules, and to copy and distribute the resulting executable under
34 terms of your choice, provided that you also meet, for each linked
35 independent module, the terms and conditions of the license of that
36 module. An independent module is a module which is not derived from
37 or based on this library. If you modify this library, you may extend
38 this exception to your version of the library, but you are not
39 obligated to do so. If you do not wish to do so, delete this
40 exception statement from your version.
41
42  * end quote.
43  *
44  * This file is licenced under the same conditions as its original (GPL +
45  * "special exception").
46  */

47
48 package org.ozoneDB.collections;
49
50 import java.util.Collection JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.ListIterator JavaDoc;
53
54 /**
55  * Abstract superclass to make it easier to implement the List interface when
56  * backed by a sequential-access store, such as a linked list. For random
57  * access data, use AbstractList. This class implements the random access
58  * methods (<code>get</code>, <code>set</code>, <code>add</code>, and
59  * <code>remove</code>) atop the list iterator, opposite of AbstractList's
60  * approach of implementing the iterator atop random access.
61  * <p>
62  *
63  * To implement a list, you need an implementation for <code>size()</code>
64  * and <code>listIterator</code>. With just <code>hasNext</code>,
65  * <code>next</code>, <code>hasPrevious</code>, <code>previous</code>,
66  * <code>nextIndex</code>, and <code>previousIndex</code>, you have an
67  * unmodifiable list. For a modifiable one, add <code>set</code>, and for
68  * a variable-size list, add <code>add</code> and <code>remove</code>.
69  * <p>
70  *
71  * The programmer should provide a no-argument constructor, and one that
72  * accepts another Collection, as recommended by the Collection interface.
73  * Unfortunately, there is no way to enforce this in Java.
74  *
75  * @author Original author unknown
76  * @author Bryce McKinlay
77  * @author Eric Blake <ebb9@email.byu.edu>
78  * @see Collection
79  * @see java.util.List
80  * @see java.util.AbstractList
81  * @see java.util.AbstractCollection
82  * @see ListIterator
83  * @see java.util.LinkedList
84  * @since 1.2
85  * @status updated to 1.4
86  */

87 public abstract class BaseSequentialListImpl extends AbstractOzoneList {
88
89     /**
90      * The main constructor, for use by subclasses.
91      */

92     protected BaseSequentialListImpl() {
93     }
94
95     /**
96      * Returns a ListIterator over the list, starting from position index.
97      * Subclasses must provide an implementation of this method.
98      *
99      * @param index the starting position of the list
100      * @return the list iterator
101      * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
102      */

103     public abstract ListIterator JavaDoc listIterator(int index);
104
105     /**
106      * Insert an element into the list at a given position (optional operation).
107      * This shifts all existing elements from that position to the end one
108      * index to the right. This version of add has no return, since it is
109      * assumed to always succeed if there is no exception. This iteration
110      * uses listIterator(index).add(o).
111      *
112      * @param index the location to insert the item
113      * @param o the object to insert
114      * @throws UnsupportedOperationException if this list does not support the
115      * add operation
116      * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
117      * @throws ClassCastException if o cannot be added to this list due to its
118      * type
119      * @throws IllegalArgumentException if o cannot be added to this list for
120      * some other reason
121      */

122     public void add(int index, Object JavaDoc o) {
123         listIterator(index).add(o);
124     }
125
126     /**
127      * Insert the contents of a collection into the list at a given position
128      * (optional operation). Shift all elements at that position to the right
129      * by the number of elements inserted. This operation is undefined if
130      * this list is modified during the operation (for example, if you try
131      * to insert a list into itself).
132      * <p>
133      *
134      * This implementation grabs listIterator(index), then proceeds to use add
135      * for each element returned by c's iterator. Sun's online specs are wrong,
136      * claiming that this also calls next(): listIterator.add() correctly
137      * skips the added element.
138      *
139      * @param index the location to insert the collection
140      * @param c the collection to insert
141      * @return true if the list was modified by this action, that is, if c is
142      * non-empty
143      * @throws UnsupportedOperationException if this list does not support the
144      * addAll operation
145      * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
146      * @throws ClassCastException if some element of c cannot be added to this
147      * list due to its type
148      * @throws IllegalArgumentException if some element of c cannot be added
149      * to this list for some other reason
150      * @throws NullPointerException if the specified collection is null
151      * @see #add(int, Object)
152      */

153     public boolean addAll(int index, Collection JavaDoc c) {
154         Iterator JavaDoc ci = c.iterator();
155         int size = c.size();
156         ListIterator JavaDoc i = listIterator(index);
157         for (int pos = size; pos > 0; pos--)
158             i.add(ci.next());
159         return size > 0;
160     }
161
162     /**
163      * Get the element at a given index in this list. This implementation
164      * returns listIterator(index).next().
165      *
166      * @param index the index of the element to be returned
167      * @return the element at index index in this list
168      * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
169      */

170     public Object JavaDoc get(int index) {
171         // This is a legal listIterator position, but an illegal get.
172
if (index == size())
173             throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Size:"
174             + size());
175         return listIterator(index).next();
176     }
177
178     /**
179      * Obtain an Iterator over this list, whose sequence is the list order. This
180      * implementation returns listIterator().
181      *
182      * @return an Iterator over the elements of this list, in order
183      */

184     public Iterator JavaDoc iterator() {
185         return listIterator();
186     }
187
188     /**
189      * Remove the element at a given position in this list (optional operation).
190      * Shifts all remaining elements to the left to fill the gap. This
191      * implementation uses listIterator(index) and ListIterator.remove().
192      *
193      * @param index the position within the list of the object to remove
194      * @return the object that was removed
195      * @throws UnsupportedOperationException if this list does not support the
196      * remove operation
197      * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
198      */

199     public Object JavaDoc remove(int index) {
200         // This is a legal listIterator position, but an illegal remove.
201
if (index == size())
202             throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Size:"
203             + size());
204         ListIterator JavaDoc i = listIterator(index);
205         Object JavaDoc removed = i.next();
206         i.remove();
207         return removed;
208     }
209
210     /**
211      * Replace an element of this list with another object (optional operation).
212      * This implementation uses listIterator(index) and ListIterator.set(o).
213      *
214      * @param index the position within this list of the element to be replaced
215      * @param o the object to replace it with
216      * @return the object that was replaced
217      * @throws UnsupportedOperationException if this list does not support the
218      * set operation
219      * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
220      * @throws ClassCastException if o cannot be added to this list due to its
221      * type
222      * @throws IllegalArgumentException if o cannot be added to this list for
223      * some other reason
224      */

225     public Object JavaDoc set(int index, Object JavaDoc o) {
226         // This is a legal listIterator position, but an illegal set.
227
if (index == size())
228             throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Size:"
229             + size());
230         ListIterator JavaDoc i = listIterator(index);
231         Object JavaDoc old = i.next();
232         i.set(o);
233         return old;
234     }
235 }
236
Popular Tags