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 }
|
85
|
51 public void add(String arg0) {
|
|
52 add(stringcast(arg0));
|
|
53 }
|
78
|
54
|
|
55 /* (non-Javadoc)
|
|
56 * @see java.util.ListIterator#hasNext()
|
|
57 */
|
|
58 public bool hasNext() {
|
|
59 return delegate_.hasNext();
|
|
60 }
|
|
61
|
|
62 /* (non-Javadoc)
|
|
63 * @see java.util.ListIterator#hasPrevious()
|
|
64 */
|
|
65 public bool hasPrevious() {
|
|
66 return delegate_.hasPrevious();
|
|
67 }
|
|
68
|
|
69 /* (non-Javadoc)
|
|
70 * @see java.util.ListIterator#next()
|
|
71 */
|
|
72 public Object next() {
|
|
73 return delegate_.next();
|
|
74 }
|
|
75
|
|
76 /* (non-Javadoc)
|
|
77 * @see java.util.ListIterator#nextIndex()
|
|
78 */
|
|
79 public int nextIndex() {
|
|
80 return delegate_.nextIndex();
|
|
81 }
|
|
82
|
|
83 /* (non-Javadoc)
|
|
84 * @see java.util.ListIterator#previous()
|
|
85 */
|
|
86 public Object previous() {
|
|
87 return delegate_.previous();
|
|
88 }
|
|
89
|
|
90 /* (non-Javadoc)
|
|
91 * @see java.util.ListIterator#previousIndex()
|
|
92 */
|
|
93 public int previousIndex() {
|
|
94 return delegate_.previousIndex();
|
|
95 }
|
|
96
|
|
97 /* (non-Javadoc)
|
|
98 * @see java.util.ListIterator#remove()
|
|
99 */
|
|
100 public void remove() {
|
|
101 delegate_.remove();
|
|
102 }
|
|
103
|
|
104 /* (non-Javadoc)
|
|
105 * @see java.util.ListIterator#set(java.lang.Object)
|
|
106 */
|
|
107 public void set(Object arg0) {
|
|
108 delegate_.set(arg0);
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * Return the element at the specified position by moving the iterator
|
|
113 * forward or backward in the list until it reaches the correct element.
|
|
114 * The iterator's position after returning the element will be one after
|
|
115 * the element returned.
|
|
116 *
|
|
117 * @param index The (0-based) index of the element to return.
|
|
118 * @return the Object at index
|
|
119 */
|
|
120 public Object get(int index) {
|
|
121 if (delegate_.nextIndex() is 0 && !delegate_.hasNext()) {
|
|
122 throw new IndexOutOfBoundsException("Request for element from empty list"); //$NON-NLS-1$
|
|
123 }
|
|
124 if (index < 0) {
|
|
125 throw new IndexOutOfBoundsException("Request for negative element index"); //$NON-NLS-1$
|
|
126 }
|
|
127
|
|
128 while (nextIndex() < index && hasNext()) {
|
|
129 next();
|
|
130 }
|
|
131 while (previousIndex() > index-1) {
|
|
132 previous();
|
|
133 }
|
|
134 if (!hasNext()) {
|
|
135 throw new IndexOutOfBoundsException("Request for element past end of list"); //$NON-NLS-1$
|
|
136 }
|
|
137 return next();
|
|
138 }
|
|
139
|
|
140 }
|