KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > RandomAccessListIterator


1 /*******************************************************************************
2  * Copyright (c) 2006 The Pampered Chef and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * The Pampered Chef - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.core.internal.databinding;
13
14 import java.util.List JavaDoc;
15 import java.util.ListIterator JavaDoc;
16
17 /**
18  * Class RandomAccessListIterator. A ListIterator implementation that also
19  * provides access to individual elements based on the element's index.
20  *
21  * @since 3.3
22  */

23 public class RandomAccessListIterator implements ListIterator JavaDoc {
24     private ListIterator JavaDoc delegate = null;
25
26     /**
27      * @param iterator
28      */

29     public RandomAccessListIterator(ListIterator JavaDoc iterator) {
30         this.delegate = iterator;
31     }
32
33     /**
34      * @param list
35      */

36     public RandomAccessListIterator(List JavaDoc list) {
37         if (list == null) {
38             throw new IllegalArgumentException JavaDoc("list is null"); //$NON-NLS-1$
39
}
40         this.delegate = list.listIterator();
41     }
42
43     /* (non-Javadoc)
44      * @see java.util.ListIterator#add(java.lang.Object)
45      */

46     public void add(Object JavaDoc arg0) {
47         delegate.add(arg0);
48     }
49
50     /* (non-Javadoc)
51      * @see java.util.ListIterator#hasNext()
52      */

53     public boolean hasNext() {
54         return delegate.hasNext();
55     }
56
57     /* (non-Javadoc)
58      * @see java.util.ListIterator#hasPrevious()
59      */

60     public boolean hasPrevious() {
61         return delegate.hasPrevious();
62     }
63
64     /* (non-Javadoc)
65      * @see java.util.ListIterator#next()
66      */

67     public Object JavaDoc next() {
68         return delegate.next();
69     }
70
71     /* (non-Javadoc)
72      * @see java.util.ListIterator#nextIndex()
73      */

74     public int nextIndex() {
75         return delegate.nextIndex();
76     }
77
78     /* (non-Javadoc)
79      * @see java.util.ListIterator#previous()
80      */

81     public Object JavaDoc previous() {
82         return delegate.previous();
83     }
84
85     /* (non-Javadoc)
86      * @see java.util.ListIterator#previousIndex()
87      */

88     public int previousIndex() {
89         return delegate.previousIndex();
90     }
91
92     /* (non-Javadoc)
93      * @see java.util.ListIterator#remove()
94      */

95     public void remove() {
96         delegate.remove();
97     }
98
99     /* (non-Javadoc)
100      * @see java.util.ListIterator#set(java.lang.Object)
101      */

102     public void set(Object JavaDoc arg0) {
103         delegate.set(arg0);
104     }
105     
106     /**
107      * Return the element at the specified position by moving the iterator
108      * forward or backward in the list until it reaches the correct element.
109      * The iterator's position after returning the element will be one after
110      * the element returned.
111      *
112      * @param index The (0-based) index of the element to return.
113      * @return the Object at index
114      */

115     public Object JavaDoc get(int index) {
116         if (delegate.nextIndex() == 0 && !delegate.hasNext()) {
117             throw new IndexOutOfBoundsException JavaDoc("Request for element from empty list"); //$NON-NLS-1$
118
}
119         if (index < 0) {
120             throw new IndexOutOfBoundsException JavaDoc("Request for negative element index"); //$NON-NLS-1$
121
}
122         
123         while (nextIndex() < index && hasNext()) {
124             next();
125         }
126         while (previousIndex() > index-1) {
127             previous();
128         }
129         if (!hasNext()) {
130             throw new IndexOutOfBoundsException JavaDoc("Request for element past end of list"); //$NON-NLS-1$
131
}
132         return next();
133     }
134
135 }
136
Popular Tags