KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > au > id > jericho > lib > html > nodoc > SequentialListSegment


1 // Jericho HTML Parser - Java based library for analysing and manipulating HTML
2
// Version 2.1
3
// Copyright (C) 2005 Martin Jericho
4
// http://sourceforge.net/projects/jerichohtml/
5
//
6
// This library is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 2.1 of the License, or (at your option) any later version.
10
// http://www.gnu.org/copyleft/lesser.html
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20

21 package au.id.jericho.lib.html.nodoc;
22
23 import au.id.jericho.lib.html.*;
24 import java.util.*;
25
26 /**
27  * A base class used internally to simulate multiple inheritance of {@link Segment} and <code>java.util.AbstractSequentialList</code>.
28  * <p>
29  * It allows a {@link Segment} based class to implement <code>java.util.List</code> without having to implement
30  * all of the <code>List</code> methods explicitly, which would clutter the API documentation with mostly irrelevant methods.
31  * By extending this class, most of the list implementation methods are simply listed in the inherited methods list.
32  *
33  * @see Attributes
34  */

35 public abstract class SequentialListSegment extends Segment implements List {
36     public SequentialListSegment(final Source source, final int begin, final int end) {
37         super(source,begin,end);
38     }
39
40     /**
41      * Returns the number of items in the list.
42      * @return the number of items in the list.
43      */

44     public abstract int getCount();
45
46     /**
47      * Returns a list iterator of the items in this list (in proper sequence), starting at the specified position in the list.
48      * <p>
49      * The specified index indicates the first item that would be returned by an initial call to the <code>next()</code> method.
50      * An initial call to the <code>previous()</code> method would return the item with the specified index minus one.
51      *
52      * @param index index of the first item to be returned from the list iterator (by a call to the <code>next()</code> method).
53      * @return a list iterator of the items in this list (in proper sequence), starting at the specified position in the list.
54      * @throws IndexOutOfBoundsException if the specified index is out of range (<code>index &lt; 0 || index &gt; size()</code>).
55      */

56     public abstract ListIterator listIterator(int index);
57
58     /**
59      * Returns the item at the specified position in this list.
60      * <p>
61      * This implementation first gets a list iterator pointing to the indexed item (with <code>listIterator(index)</code>).
62      * Then, it gets the element using <code>ListIterator.next</code> and returns it.
63      *
64      * @param index the index of the item to return.
65      * @return the item at the specified position in this list.
66      * @throws IndexOutOfBoundsException if the specified index is out of range (<code>index &lt; 0 || index &gt;= size()</code>).
67      */

68     public Object JavaDoc get(final int index) {
69         final ListIterator li=listIterator(index);
70         try {
71             return(li.next());
72         } catch(NoSuchElementException ex) {
73             throw(new IndexOutOfBoundsException JavaDoc("index="+index));
74         }
75     }
76
77     /**
78      * Returns the number of items in the list.
79      * <p>
80      * This is equivalent to {@link #getCount()},
81      * and is necessary to for the implementation of the <code>java.util.Collection</code> interface.
82      *
83      * @return the number of items in the list.
84      */

85     public int size() {
86         return getCount();
87     }
88
89     /**
90      * Indicates whether this list is empty.
91      * @return <code>true</code> if there are no items in the list, otherwise <code>false</code>.
92      */

93     public boolean isEmpty() {
94         return getCount()==0;
95     }
96
97     /**
98      * Indicates whether this list contains the specified object.
99      *
100      * @param o object to be checked for containment in this list.
101      * @return <code>true</code> if this list contains the specified object, otherwise <code>false</code>.
102      */

103     public boolean contains(final Object JavaDoc o) {
104         return indexOf(o)>=0;
105     }
106
107     /**
108      * Returns an array containing all of the items in this list.
109      * @return an array containing all of the items in this list.
110      */

111     public Object JavaDoc[] toArray() {
112         final Object JavaDoc[] array=new Object JavaDoc[getCount()];
113         int x=0;
114         for (final ListIterator li=listIterator(0); li.hasNext();) array[x++]=li.next();
115         return array;
116     }
117
118     /**
119      * Returns an array containing all of the items in this list in the correct order;
120      * the runtime type of the returned array is that of the specified array.
121      * If the list fits in the specified array, it is returned therein.
122      * Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
123      * <p>
124      * If the list fits in the specified array with room to spare (i.e., the array has more elements than the list),
125      * the item in the array immediately following the end of the collection is set to <code>null</code>.
126      * This is useful in determining the length of the list <i>only</i>
127      * if the caller knows that the list does not contain any <code>null</code> items.
128      *
129      * @param a the array into which the items of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
130      * @return an array containing the items of the list.
131      * @throws NullPointerException if the specified array is <code>null</code>.
132      * @throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every item in this list.
133      */

134     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
135         final int count=getCount();
136         if (a.length<count) a=(Object JavaDoc[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(),count);
137         int x=0;
138         for (final ListIterator li=listIterator(0); li.hasNext();) a[x++]=li.next();
139         if (a.length>count) a[count]=null;
140         return a;
141     }
142
143     /**
144      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
145      * @throws UnsupportedOperationException
146      */

147     public boolean remove(Object JavaDoc o) {
148         throw new UnsupportedOperationException JavaDoc();
149     }
150
151     /**
152      * Indicates whether this list contains all of the items in the specified collection.
153      * @param collection the collection to be checked for containment in this list.
154      * @return <code>true</code> if this list contains all of the items in the specified collection, otherwise <code>false</code>.
155      * @throws NullPointerException if the specified collection is null.
156      * @see #contains(Object)
157      */

158     public boolean containsAll(final Collection collection) {
159         for (final Iterator i=collection.iterator(); i.hasNext();)
160             if(!contains(i.next())) return false;
161         return true;
162     }
163
164     /**
165      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
166      * @throws UnsupportedOperationException
167      */

168     public boolean addAll(Collection collection) {
169         throw new UnsupportedOperationException JavaDoc();
170     }
171
172     /**
173      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
174      * @throws UnsupportedOperationException
175      */

176     public boolean removeAll(Collection collection) {
177         throw new UnsupportedOperationException JavaDoc();
178     }
179
180     /**
181      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
182      * @throws UnsupportedOperationException
183      */

184     public boolean retainAll(Collection collection) {
185         throw new UnsupportedOperationException JavaDoc();
186     }
187
188     /**
189      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
190      * @throws UnsupportedOperationException
191      */

192     public boolean add(Object JavaDoc o) {
193         throw new UnsupportedOperationException JavaDoc();
194     }
195
196     /**
197      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
198      * @throws UnsupportedOperationException
199      */

200     public Object JavaDoc set(int index, Object JavaDoc element) {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203
204     /**
205      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
206      * @throws UnsupportedOperationException
207      */

208     public void add(int index, Object JavaDoc element) {
209         throw new UnsupportedOperationException JavaDoc();
210     }
211
212     /**
213      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
214      * @throws UnsupportedOperationException
215      */

216     public Object JavaDoc remove(int index) {
217         throw new UnsupportedOperationException JavaDoc();
218     }
219
220     /**
221      * Returns the index in this list of the first occurence of the specified object, or -1 if the list does not contain this object.
222      *
223      * @param o object to search for.
224      * @return the index in this list of the first occurence of the specified object, or -1 if the list does not contain this object.
225      */

226     public int indexOf(final Object JavaDoc o) {
227         final ListIterator li=listIterator(0);
228         if (o==null) {
229             while (li.hasNext()) if (li.next()==null) return li.previousIndex();
230         } else {
231             while (li.hasNext()) if (o.equals(li.next())) return li.previousIndex();
232         }
233         return -1;
234     }
235
236     /**
237      * Returns the index in this list of the last occurence of the specified object, or -1 if the list does not contain this object.
238      *
239      * @param o object to search for.
240      * @return the index in this list of the last occurence of the specified object, or -1 if the list does not contain this object.
241      */

242     public int lastIndexOf(final Object JavaDoc o) {
243         final ListIterator li=listIterator(getCount());
244         if (o==null) {
245             while (li.hasPrevious()) if (li.previous()==null) return li.nextIndex();
246         } else {
247             while (li.hasPrevious()) if (o.equals(li.previous())) return li.nextIndex();
248         }
249         return -1;
250     }
251
252     /**
253      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
254      * @throws UnsupportedOperationException
255      */

256     public void clear() {
257         throw new UnsupportedOperationException JavaDoc();
258     }
259
260     /**
261      * This list is unmodifiable, so this method always throws an <code>UnsupportedOperationException</code>.
262      * @throws UnsupportedOperationException
263      */

264     public boolean addAll(int index, Collection collection) {
265         throw new UnsupportedOperationException JavaDoc();
266     }
267
268     /**
269      * Returns an iterator over the items in the list in proper sequence.
270      * @return an iterator over the items in the list in proper sequence.
271      */

272     public Iterator iterator() {
273         return listIterator();
274     }
275
276     /**
277      * Returns a list iterator of the items in this list (in proper sequence), starting with the first item in the list.
278      * @return a list iterator of the items in this list (in proper sequence), starting with the first item in the list.
279      * @see #listIterator(int)
280      */

281     public ListIterator listIterator() {
282         return listIterator(0);
283     }
284
285     /**
286      * Returns a view of the portion of this list between <code>fromIndex</code>, inclusive, and <code>toIndex</code>, exclusive.
287      * (If <code>fromIndex</code> and <code>toIndex</code> are equal, the returned list is empty.)
288      * The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa.
289      * The returned list supports all of the optional list operations supported by this list.
290      *
291      * @param fromIndex low endpoint (inclusive) of the subList.
292      * @param toIndex high endpoint (exclusive) of the subList.
293      * @return a view of the specified range within this list.
294      * @throws IndexOutOfBoundsException endpoint index value out of range <code>(fromIndex &lt; 0 || toIndex &gt; size)</code>
295      * @throws IllegalArgumentException endpoint indices out of order <code>(fromIndex &gt; toIndex)</code>
296      * @see java.util.List#subList(int fromIndex, int toIndex)
297      */

298     public List subList(final int fromIndex, final int toIndex) {
299         return (new SubList(this,fromIndex,toIndex));
300     }
301
302     private static class SubList extends AbstractList {
303         private final List list;
304         private final int offset;
305         private final int size;
306
307         SubList(final List list, final int fromIndex, final int toIndex) {
308             if (fromIndex<0) throw new IndexOutOfBoundsException JavaDoc("fromIndex="+fromIndex);
309             if (toIndex>list.size()) throw new IndexOutOfBoundsException JavaDoc("toIndex="+toIndex);
310             if (fromIndex>toIndex) throw new IllegalArgumentException JavaDoc("fromIndex("+fromIndex+") > toIndex("+toIndex+")");
311             this.list=list;
312             offset=fromIndex;
313             size=toIndex-fromIndex;
314         }
315
316         public Object JavaDoc get(final int index) {
317             return list.get(getSuperListIndex(index));
318         }
319
320         public int size() {
321             return size;
322         }
323
324         public Iterator iterator() {
325             return listIterator();
326         }
327
328         public ListIterator listIterator(final int index) {
329             return new ListIterator() {
330                 private final ListIterator i=list.listIterator(getSuperListIndex(index));
331                 public boolean hasNext() {
332                     return nextIndex()<size;
333                 }
334                 public Object JavaDoc next() {
335                     if (!hasNext()) throw new NoSuchElementException();
336                     return i.next();
337                 }
338                 public boolean hasPrevious() {
339                     return previousIndex()>=0;
340                 }
341                 public Object JavaDoc previous() {
342                     if (!hasPrevious()) throw new NoSuchElementException();
343                     return i.previous();
344                 }
345                 public int nextIndex() {
346                     return i.nextIndex()-offset;
347                 }
348                 public int previousIndex() {
349                     return i.previousIndex()-offset;
350                 }
351                 public void remove() {
352                     throw new UnsupportedOperationException JavaDoc();
353                 }
354                 public void set(Object JavaDoc o) {
355                     throw new UnsupportedOperationException JavaDoc();
356                 }
357                 public void add(Object JavaDoc o) {
358                     throw new UnsupportedOperationException JavaDoc();
359                 }
360             };
361         }
362
363         public List subList(final int fromIndex, final int toIndex) {
364             return new SubList(this,fromIndex,toIndex);
365         }
366
367         private int getSuperListIndex(final int index) {
368             if (index<0 || index>=size) throw new IndexOutOfBoundsException JavaDoc("index="+index+", size="+size);
369             return index+offset;
370         }
371     }
372 }
373
Popular Tags