gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / UnmodifiableBasicListToListAdapter.java @ 2335
History | View | Annotate | Download (5.93 KB)
1 |
package org.gvsig.tools.util; |
---|---|
2 |
|
3 |
import java.util.Collection; |
4 |
import java.util.Iterator; |
5 |
import java.util.List; |
6 |
import java.util.ListIterator; |
7 |
import java.util.NoSuchElementException; |
8 |
|
9 |
/**
|
10 |
*
|
11 |
* @author jjdelcerro
|
12 |
* @param <T>
|
13 |
*/
|
14 |
public class UnmodifiableBasicListToListAdapter<T> implements List<T> { |
15 |
|
16 |
private final UnmodifiableBasicList<T> basiclist; |
17 |
|
18 |
public UnmodifiableBasicListToListAdapter(UnmodifiableBasicList<T> basiclist) {
|
19 |
this.basiclist = basiclist;
|
20 |
} |
21 |
|
22 |
@Override
|
23 |
public int size() { |
24 |
return this.basiclist.size(); |
25 |
} |
26 |
|
27 |
@Override
|
28 |
public boolean isEmpty() { |
29 |
return this.basiclist.isEmpty(); |
30 |
} |
31 |
|
32 |
@Override
|
33 |
public boolean contains(Object o) { |
34 |
return indexOf(o) >= 0; |
35 |
} |
36 |
|
37 |
@Override
|
38 |
public Iterator<T> iterator() { |
39 |
return this.basiclist.iterator(); |
40 |
} |
41 |
|
42 |
@Override
|
43 |
public Object[] toArray() { |
44 |
int size = this.size(); |
45 |
Object[] result = new Object[size]; |
46 |
int i = 0; |
47 |
for (int j = 0; j < size; j++) { |
48 |
result[i++] = this.get(j);
|
49 |
} |
50 |
return result;
|
51 |
} |
52 |
|
53 |
@Override
|
54 |
public <T> T[] toArray(T[] a) { |
55 |
int size = this.size(); |
56 |
if (a.length < size)
|
57 |
a = (T[])java.lang.reflect.Array.newInstance(
|
58 |
a.getClass().getComponentType(), size |
59 |
); |
60 |
int i = 0; |
61 |
Object[] result = a; |
62 |
for (int j = 0; j < size; j++) { |
63 |
result[i++] = this.get(j);
|
64 |
} |
65 |
if (a.length > size)
|
66 |
a[size] = null;
|
67 |
return a;
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
public boolean add(T e) { |
72 |
throw new UnsupportedOperationException("Not supported yet."); |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public boolean remove(Object o) { |
77 |
throw new UnsupportedOperationException("Not supported yet."); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public boolean containsAll(Collection<?> c) { |
82 |
for (Object e : c) |
83 |
if (!contains(e))
|
84 |
return false; |
85 |
return true; |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public boolean addAll(Collection<? extends T> c) { |
90 |
throw new UnsupportedOperationException("Not supported yet."); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public boolean addAll(int index, Collection<? extends T> c) { |
95 |
throw new UnsupportedOperationException("Not supported yet."); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public boolean removeAll(Collection<?> c) { |
100 |
throw new UnsupportedOperationException("Not supported yet."); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public boolean retainAll(Collection<?> c) { |
105 |
throw new UnsupportedOperationException("Not supported yet."); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void clear() { |
110 |
throw new UnsupportedOperationException("Not supported yet."); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public T get(int index) { |
115 |
return this.basiclist.get(index); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public T set(int index, T element) { |
120 |
throw new UnsupportedOperationException("Not supported yet."); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void add(int index, T element) { |
125 |
throw new UnsupportedOperationException("Not supported yet."); |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public T remove(int index) { |
130 |
throw new UnsupportedOperationException("Not supported yet."); |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public int indexOf(Object o) { |
135 |
int size = this.basiclist.size(); |
136 |
if (o == null) { |
137 |
for (int i = 0; i < size; i++) |
138 |
if (this.basiclist.get(i)==null) |
139 |
return i;
|
140 |
} else {
|
141 |
for (int i = 0; i < size; i++) |
142 |
if (o.equals(this.basiclist.get(i))) |
143 |
return i;
|
144 |
} |
145 |
return -1; |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public int lastIndexOf(Object o) { |
150 |
int size = this.basiclist.size(); |
151 |
if (o == null) { |
152 |
for (int i = size-1; i >= 0; i--) |
153 |
if (this.basiclist.get(i)==null) |
154 |
return i;
|
155 |
} else {
|
156 |
for (int i = size-1; i >= 0; i--) |
157 |
if (o.equals(this.basiclist.get(i))) |
158 |
return i;
|
159 |
} |
160 |
return -1; |
161 |
} |
162 |
|
163 |
@Override
|
164 |
public ListIterator<T> listIterator() { |
165 |
return new ListItr(0); |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public ListIterator<T> listIterator(int index) { |
170 |
if (index < 0 || index > size()) |
171 |
throw new IndexOutOfBoundsException("Index: "+index); |
172 |
return new ListItr(index); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public List<T> subList(int fromIndex, int toIndex) { |
177 |
throw new UnsupportedOperationException("Not supported yet."); |
178 |
} |
179 |
|
180 |
private class Itr implements Iterator<T> { |
181 |
int cursor; // index of next element to return |
182 |
|
183 |
Itr() {} |
184 |
|
185 |
@Override
|
186 |
public boolean hasNext() { |
187 |
return cursor != size();
|
188 |
} |
189 |
|
190 |
@Override
|
191 |
public T next() {
|
192 |
int i = cursor;
|
193 |
if (i >= size())
|
194 |
throw new NoSuchElementException(); |
195 |
cursor = i + 1;
|
196 |
return basiclist.get(i);
|
197 |
} |
198 |
|
199 |
@Override
|
200 |
public void remove() { |
201 |
throw new UnsupportedOperationException("Not supported yet."); |
202 |
} |
203 |
} |
204 |
|
205 |
private class ListItr extends Itr implements ListIterator<T> { |
206 |
ListItr(int index) {
|
207 |
super();
|
208 |
cursor = index; |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public boolean hasPrevious() { |
213 |
return cursor != 0; |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public int nextIndex() { |
218 |
return cursor;
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public int previousIndex() { |
223 |
return cursor - 1; |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public T previous() {
|
228 |
int i = cursor - 1; |
229 |
if (i < 0) |
230 |
throw new NoSuchElementException(); |
231 |
cursor = i; |
232 |
return (T)basiclist.get(i);
|
233 |
} |
234 |
|
235 |
@Override
|
236 |
public void set(T e) { |
237 |
throw new UnsupportedOperationException("Not supported yet."); |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public void add(T e) { |
242 |
throw new UnsupportedOperationException("Not supported yet."); |
243 |
} |
244 |
} |
245 |
|
246 |
} |