78
|
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 module org.eclipse.core.internal.databinding.RandomAccessListIterator;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.List;
|
|
17 import java.util.ListIterator;
|
|
18
|
|
19 /**
|
|
20 * Class RandomAccessListIterator. A ListIterator implementation that also
|
|
21 * provides access to individual elements based on the element's index.
|
|
22 *
|
|
23 * @since 3.3
|
|
24 */
|
|
25 public class RandomAccessListIterator : ListIterator {
|
|
26 private ListIterator delegate_ = null;
|
|
27
|
|
28 /**
|
|
29 * @param iterator
|
|
30 */
|
|
31 public this(ListIterator iterator) {
|
|
32 this.delegate_ = iterator;
|
|
33 }
|
|
34
|
|
35 /**
|
|
36 * @param list
|
|
37 */
|
|
38 public this(List list) {
|
|
39 if (list is null) {
|
|
40 throw new IllegalArgumentException("list is null"); //$NON-NLS-1$
|
|
41 }
|
|
42 this.delegate_ = list.listIterator();
|
|
43 }
|
|
44
|
|
45 /* (non-Javadoc)
|
|
46 * @see java.util.ListIterator#add(java.lang.Object)
|
|
47 */
|
|
48 public void add(Object arg0) {
|
|
49 delegate_.add(arg0);
|
|
50 }
|
|
51
|
|
52 /* (non-Javadoc)
|
|
53 * @see java.util.ListIterator#hasNext()
|
|
54 */
|
|
55 public bool hasNext() {
|
|
56 return delegate_.hasNext();
|
|
57 }
|
|
58
|
|
59 /* (non-Javadoc)
|
|
60 * @see java.util.ListIterator#hasPrevious()
|
|
61 */
|
|
62 public bool hasPrevious() {
|
|
63 return delegate_.hasPrevious();
|
|
64 }
|
|
65
|
|
66 /* (non-Javadoc)
|
|
67 * @see java.util.ListIterator#next()
|
|
68 */
|
|
69 public Object next() {
|
|
70 return delegate_.next();
|
|
71 }
|
|
72
|
|
73 /* (non-Javadoc)
|
|
74 * @see java.util.ListIterator#nextIndex()
|
|
75 */
|
|
76 public int nextIndex() {
|
|
77 return delegate_.nextIndex();
|
|
78 }
|
|
79
|
|
80 /* (non-Javadoc)
|
|
81 * @see java.util.ListIterator#previous()
|
|
82 */
|
|
83 public Object previous() {
|
|
84 return delegate_.previous();
|
|
85 }
|
|
86
|
|
87 /* (non-Javadoc)
|
|
88 * @see java.util.ListIterator#previousIndex()
|
|
89 */
|
|
90 public int previousIndex() {
|
|
91 return delegate_.previousIndex();
|
|
92 }
|
|
93
|
|
94 /* (non-Javadoc)
|
|
95 * @see java.util.ListIterator#remove()
|
|
96 */
|
|
97 public void remove() {
|
|
98 delegate_.remove();
|
|
99 }
|
|
100
|
|
101 /* (non-Javadoc)
|
|
102 * @see java.util.ListIterator#set(java.lang.Object)
|
|
103 */
|
|
104 public void set(Object arg0) {
|
|
105 delegate_.set(arg0);
|
|
106 }
|
|
107
|
|
108 /**
|
|
109 * Return the element at the specified position by moving the iterator
|
|
110 * forward or backward in the list until it reaches the correct element.
|
|
111 * The iterator's position after returning the element will be one after
|
|
112 * the element returned.
|
|
113 *
|
|
114 * @param index The (0-based) index of the element to return.
|
|
115 * @return the Object at index
|
|
116 */
|
|
117 public Object get(int index) {
|
|
118 if (delegate_.nextIndex() is 0 && !delegate_.hasNext()) {
|
|
119 throw new IndexOutOfBoundsException("Request for element from empty list"); //$NON-NLS-1$
|
|
120 }
|
|
121 if (index < 0) {
|
|
122 throw new IndexOutOfBoundsException("Request for negative element index"); //$NON-NLS-1$
|
|
123 }
|
|
124
|
|
125 while (nextIndex() < index && hasNext()) {
|
|
126 next();
|
|
127 }
|
|
128 while (previousIndex() > index-1) {
|
|
129 previous();
|
|
130 }
|
|
131 if (!hasNext()) {
|
|
132 throw new IndexOutOfBoundsException("Request for element past end of list"); //$NON-NLS-1$
|
|
133 }
|
|
134 return next();
|
|
135 }
|
|
136
|
|
137 }
|