KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: _BaseList_SubListImpl.java,v 1.2 2003/11/20 23:18:41 per_nyfelt Exp $
3  * This file is based on AbstractList.java from GNU Classpath. Quote:
4
5 AbstractList.java -- Abstract implementation of most of List
6 Copyright (C) 1998, 1999, 2000, 2001, 2002 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.ConcurrentModificationException JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.ListIterator JavaDoc;
55
56 /**
57  * This class follows the implementation requirements set forth in
58  * {@link java.util.AbstractList#subList(int, int)}. It matches Sun's implementation
59  * by using a non-public top-level class in the same package.
60  *
61  * @author Original author unknown
62  * @author Eric Blake <ebb9@email.byu.edu>
63  * @author ported to Ozone by Leo Mekenkamp
64  */

65 public class _BaseList_SubListImpl extends BaseListImpl implements _BaseList_SubList {
66
67     /** The original list. */
68     private final BaseList backingList;
69
70     /** The index of the first element of the sublist. */
71     private final int offset;
72
73     /** The size of the sublist. */
74     private int size;
75
76     /**
77      * Construct the sublist.
78      *
79      * @param backingList the list this comes from
80      * @param fromIndex the lower bound, inclusive
81      * @param toIndex the upper bound, exclusive
82      */

83     public _BaseList_SubListImpl(BaseList backingList, int fromIndex, int toIndex) {
84         this.backingList = backingList;
85         modCount = backingList._org_ozoneDB_getModCount();
86         offset = fromIndex;
87         size = toIndex - fromIndex;
88     }
89
90     /**
91      * This method checks the two modCount fields to ensure that there has
92      * not been a concurrent modification, returning if all is okay.
93      *
94      * @throws ConcurrentModificationException if the backing list has been
95      * modified externally to this sublist
96      */

97     public void _org_ozoneDB_checkMod() {
98         if (modCount != backingList._org_ozoneDB_getModCount()) {
99             throw new ConcurrentModificationException JavaDoc();
100         }
101     }
102
103     /**
104      * This method checks that a value is between 0 and size (inclusive). If
105      * it is not, an exception is thrown.
106      *
107      * @param index the value to check
108      * @throws IndexOutOfBoundsException if the value is out of range
109      */

110     public void _org_ozoneDB_checkBoundsInclusive(int index) {
111         if (index < 0 || index > size)
112             throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Size:"
113             + size);
114     }
115
116     /**
117      * This method checks that a value is between 0 (inclusive) and size
118      * (exclusive). If it is not, an exception is thrown.
119      *
120      * @param index the value to check
121      * @throws IndexOutOfBoundsException if the value is out of range
122      */

123     // This will get inlined, since it is private.
124
public void _org_ozoneDB_checkBoundsExclusive(int index) {
125         if (index < 0 || index >= size)
126             throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Size:"
127             + size);
128     }
129
130     /**
131      * Specified by AbstractList.subList to return the private field size.
132      *
133      * @return the sublist size
134      */

135     public int size() {
136         _org_ozoneDB_checkMod();
137         return size;
138     }
139
140     /**
141      * Specified by AbstractList.subList to delegate to the backing list.
142      *
143      * @param index the location to modify
144      * @param o the new value
145      * @return the old value
146      */

147     public Object JavaDoc set(int index, Object JavaDoc o) {
148         _org_ozoneDB_checkMod();
149         _org_ozoneDB_checkBoundsExclusive(index);
150         return backingList.set(index + offset, o);
151     }
152
153     /**
154      * Specified by AbstractList.subList to delegate to the backing list.
155      *
156      * @param index the location to get from
157      * @return the object at that location
158      */

159     public Object JavaDoc get(int index) {
160         _org_ozoneDB_checkMod();
161         _org_ozoneDB_checkBoundsExclusive(index);
162         return backingList.get(index + offset);
163     }
164
165     /**
166      * Specified by AbstractList.subList to delegate to the backing list.
167      *
168      * @param index the index to insert at
169      * @param o the object to add
170      */

171     public void add(int index, Object JavaDoc o) {
172         _org_ozoneDB_checkMod();
173         _org_ozoneDB_checkBoundsInclusive(index);
174         backingList.add(index + offset, o);
175         size++;
176         modCount = backingList._org_ozoneDB_getModCount();
177     }
178
179     /**
180      * Specified by AbstractList.subList to delegate to the backing list.
181      *
182      * @param index the index to remove
183      * @return the removed object
184      */

185     public Object JavaDoc remove(int index) {
186         _org_ozoneDB_checkMod();
187         _org_ozoneDB_checkBoundsExclusive(index);
188         Object JavaDoc o = backingList.remove(index + offset);
189         size--;
190         modCount = backingList._org_ozoneDB_getModCount();
191         return o;
192     }
193
194     /**
195      * Specified by AbstractList.subList to delegate to the backing list.
196      * This does no bounds checking, as it assumes it will only be called
197      * by trusted code like clear() which has already checked the bounds.
198      *
199      * @param fromIndex the lower bound, inclusive
200      * @param toIndex the upper bound, exclusive
201      */

202     public void _org_ozoneDB_removeRange(int fromIndex, int toIndex) {
203         _org_ozoneDB_checkMod();
204
205         backingList._org_ozoneDB_removeRange(offset + fromIndex, offset + toIndex);
206         size -= toIndex - fromIndex;
207         modCount = backingList._org_ozoneDB_getModCount();
208     }
209
210     /**
211      * Specified by AbstractList.subList to delegate to the backing list.
212      *
213      * @param index the location to insert at
214      * @param c the collection to insert
215      * @return true if this list was modified, in other words, c is non-empty
216      */

217     public boolean addAll(int index, Collection JavaDoc c) {
218         _org_ozoneDB_checkMod();
219         _org_ozoneDB_checkBoundsInclusive(index);
220         int csize = c.size();
221         boolean result = backingList.addAll(offset + index, c);
222         size += csize;
223         modCount = backingList._org_ozoneDB_getModCount();
224         return result;
225     }
226
227     /**
228      * Specified by AbstractList.subList to return addAll(size, c).
229      *
230      * @param c the collection to insert
231      * @return true if this list was modified, in other words, c is non-empty
232      */

233     public boolean addAll(Collection JavaDoc c) {
234         return addAll(size, c);
235     }
236
237     public Iterator JavaDoc _org_ozoneDB_internalIterator() {
238         throw new RuntimeException JavaDoc("_org_ozoneDB_internalIterator(), Not yet Implemented");
239     }
240
241     /**
242      * Specified by AbstractList.subList to return listIterator().
243      *
244      * @return an iterator over the sublist
245      */

246     public Iterator JavaDoc iterator() {
247         return listIterator();
248     }
249
250 // public int modCount() {
251
// return modCount;
252
// }
253

254     /**
255      * Specified by AbstractList.subList to return a wrapper around the
256      * backing list's iterator.
257      *
258      * @param index the start location of the iterator
259      * @return a list iterator over the sublist
260      */

261     public ListIterator JavaDoc listIterator(final int index) {
262         _org_ozoneDB_checkMod();
263         _org_ozoneDB_checkBoundsInclusive(index);
264
265
266         OzoneListIterator iterator = (OzoneListIterator) backingList.listIterator(index + offset);
267 // TODO: replace when FakeFactoryGenerator is ready
268
ListIterator JavaDoc result = (ListIterator JavaDoc) database().createObject(
269                 _BaseList_SubList_listIterator.class,
270                 new Class JavaDoc[] {_BaseList_SubList.class, OzoneListIterator.class},
271                 new Object JavaDoc[] {self(), iterator}
272         );
273         return result;
274     }
275
276     public int _org_ozoneDB_getModCount() {
277         return modCount;
278     }
279
280     public void _org_ozoneDB_incSize(int amount) {
281         size += amount;
282     }
283
284     public List JavaDoc getClientList() {
285         List JavaDoc result = backingList._org_ozoneDB_emptyClientCollection();
286         result.addAll((List JavaDoc) self());
287         return result;
288     }
289
290     public int _org_ozoneDB_getOffset() {
291         return offset;
292     }
293
294     public List JavaDoc _org_ozoneDB_emptyClientCollection() {
295         return backingList._org_ozoneDB_emptyClientCollection();
296     }
297
298     public void _org_ozoneDB_syncModCountWithBackingList() {
299         modCount = backingList._org_ozoneDB_getModCount();
300     }
301
302 }
303
Popular Tags