1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.collections.primitives; |
18 | |
|
19 | |
import java.util.ConcurrentModificationException; |
20 | |
import java.util.NoSuchElementException; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public abstract class RandomAccessDoubleList extends AbstractDoubleCollection implements DoubleList { |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 798 | protected RandomAccessDoubleList() { |
47 | 798 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public abstract double get(int index); |
53 | |
public abstract int size(); |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public double removeElementAt(int index) { |
63 | 1 | throw new UnsupportedOperationException(); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public double set(int index, double element) { |
71 | 2 | throw new UnsupportedOperationException(); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void add(int index, double element) { |
79 | 2 | throw new UnsupportedOperationException(); |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public boolean add(double element) { |
87 | 2752 | add(size(),element); |
88 | 2750 | return true; |
89 | |
} |
90 | |
|
91 | |
public boolean addAll(int index, DoubleCollection collection) { |
92 | 1 | boolean modified = false; |
93 | 1 | for(DoubleIterator iter = collection.iterator(); iter.hasNext(); ) { |
94 | 7 | add(index++,iter.next()); |
95 | 7 | modified = true; |
96 | |
} |
97 | 1 | return modified; |
98 | |
} |
99 | |
|
100 | |
public int indexOf(double element) { |
101 | 84 | int i = 0; |
102 | 84 | for(DoubleIterator iter = iterator(); iter.hasNext(); ) { |
103 | 948 | if(iter.next() == element) { |
104 | 42 | return i; |
105 | |
} else { |
106 | 906 | i++; |
107 | |
} |
108 | |
} |
109 | 42 | return -1; |
110 | |
} |
111 | |
|
112 | |
public int lastIndexOf(double element) { |
113 | 84 | for(DoubleListIterator iter = listIterator(size()); iter.hasPrevious(); ) { |
114 | 948 | if(iter.previous() == element) { |
115 | 42 | return iter.nextIndex(); |
116 | |
} |
117 | |
} |
118 | 42 | return -1; |
119 | |
} |
120 | |
|
121 | |
public DoubleIterator iterator() { |
122 | 6820 | return listIterator(); |
123 | |
} |
124 | |
|
125 | |
public DoubleListIterator listIterator() { |
126 | 8089 | return listIterator(0); |
127 | |
} |
128 | |
|
129 | |
public DoubleListIterator listIterator(int index) { |
130 | 8319 | return new RandomAccessDoubleListIterator(this,index); |
131 | |
} |
132 | |
|
133 | |
public DoubleList subList(int fromIndex, int toIndex) { |
134 | 171 | return new RandomAccessDoubleSubList(this,fromIndex,toIndex); |
135 | |
} |
136 | |
|
137 | |
public boolean equals(Object that) { |
138 | 32 | if(this == that) { |
139 | 3 | return true; |
140 | 29 | } else if(that instanceof DoubleList) { |
141 | 26 | DoubleList thatList = (DoubleList)that; |
142 | 26 | if(size() != thatList.size()) { |
143 | 4 | return false; |
144 | |
} |
145 | 22 | for(DoubleIterator thatIter = thatList.iterator(), thisIter = iterator(); thisIter.hasNext();) { |
146 | 120 | if(thisIter.next() != thatIter.next()) { |
147 | 2 | return false; |
148 | |
} |
149 | |
} |
150 | 20 | return true; |
151 | |
} else { |
152 | 3 | return false; |
153 | |
} |
154 | |
} |
155 | |
|
156 | |
public int hashCode() { |
157 | 1165 | int hash = 1; |
158 | 1165 | for(DoubleIterator iter = iterator(); iter.hasNext(); ) { |
159 | 15509 | long bits = Double.doubleToLongBits(iter.next()); |
160 | 15509 | hash = 31*hash + ((int)(bits ^ (bits >>> 32))); |
161 | 15509 | } |
162 | 1165 | return hash; |
163 | |
} |
164 | |
|
165 | |
public String toString() { |
166 | 6 | StringBuffer buf = new StringBuffer(); |
167 | 6 | buf.append("["); |
168 | 6 | for(DoubleIterator iter = iterator(); iter.hasNext();) { |
169 | 42 | buf.append(iter.next()); |
170 | 42 | if(iter.hasNext()) { |
171 | 39 | buf.append(", "); |
172 | |
} |
173 | |
} |
174 | 6 | buf.append("]"); |
175 | 6 | return buf.toString(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
protected int getModCount() { |
183 | 433520 | return _modCount; |
184 | |
} |
185 | |
|
186 | |
|
187 | |
protected void incrModCount() { |
188 | 7534 | _modCount++; |
189 | 7534 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | 798 | private int _modCount = 0; |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
private static class ComodChecker { |
200 | 8486 | ComodChecker(RandomAccessDoubleList source) { |
201 | 8486 | _source = source; |
202 | 8486 | resyncModCount(); |
203 | 8486 | } |
204 | |
|
205 | |
protected RandomAccessDoubleList getList() { |
206 | 757762 | return _source; |
207 | |
} |
208 | |
|
209 | |
protected void assertNotComodified() throws ConcurrentModificationException { |
210 | 415730 | if(_expectedModCount != getList().getModCount()) { |
211 | 1 | throw new ConcurrentModificationException(); |
212 | |
} |
213 | 415729 | } |
214 | |
|
215 | |
protected void resyncModCount() { |
216 | 17790 | _expectedModCount = getList().getModCount(); |
217 | 17790 | } |
218 | |
|
219 | 8486 | private RandomAccessDoubleList _source = null; |
220 | 8486 | private int _expectedModCount = -1; |
221 | |
} |
222 | |
|
223 | |
protected static class RandomAccessDoubleListIterator extends ComodChecker implements DoubleListIterator { |
224 | |
RandomAccessDoubleListIterator(RandomAccessDoubleList list, int index) { |
225 | 8319 | super(list); |
226 | 8319 | if(index < 0 || index > getList().size()) { |
227 | 9 | throw new IndexOutOfBoundsException("Index " + index + " not in [0," + getList().size() + ")"); |
228 | |
} else { |
229 | 8310 | _nextIndex = index; |
230 | 8310 | resyncModCount(); |
231 | |
} |
232 | 8310 | } |
233 | |
|
234 | |
public boolean hasNext() { |
235 | 211109 | assertNotComodified(); |
236 | 211109 | return _nextIndex < getList().size(); |
237 | |
} |
238 | |
|
239 | |
public boolean hasPrevious() { |
240 | 3663 | assertNotComodified(); |
241 | 3663 | return _nextIndex > 0; |
242 | |
} |
243 | |
|
244 | |
public int nextIndex() { |
245 | 1369 | assertNotComodified(); |
246 | 1369 | return _nextIndex; |
247 | |
} |
248 | |
|
249 | |
public int previousIndex() { |
250 | 1327 | assertNotComodified(); |
251 | 1327 | return _nextIndex - 1; |
252 | |
} |
253 | |
|
254 | |
public double next() { |
255 | 102618 | assertNotComodified(); |
256 | 102617 | if(!hasNext()) { |
257 | 72 | throw new NoSuchElementException(); |
258 | |
} else { |
259 | 102545 | double val = getList().get(_nextIndex); |
260 | 102545 | _lastReturnedIndex = _nextIndex; |
261 | 102545 | _nextIndex++; |
262 | 102545 | return val; |
263 | |
} |
264 | |
} |
265 | |
|
266 | |
public double previous() { |
267 | 1856 | assertNotComodified(); |
268 | 1856 | if(!hasPrevious()) { |
269 | 101 | throw new NoSuchElementException(); |
270 | |
} else { |
271 | 1755 | double val = getList().get(_nextIndex-1); |
272 | 1755 | _lastReturnedIndex = _nextIndex-1; |
273 | 1755 | _nextIndex--; |
274 | 1755 | return val; |
275 | |
} |
276 | |
} |
277 | |
|
278 | |
public void add(double value) { |
279 | 152 | assertNotComodified(); |
280 | 152 | getList().add(_nextIndex,value); |
281 | 152 | _nextIndex++; |
282 | 152 | _lastReturnedIndex = -1; |
283 | 152 | resyncModCount(); |
284 | 152 | } |
285 | |
|
286 | |
public void remove() { |
287 | 333 | assertNotComodified(); |
288 | 333 | if (_lastReturnedIndex == -1) { |
289 | 26 | throw new IllegalStateException(); |
290 | |
} |
291 | 307 | if (_lastReturnedIndex == _nextIndex) { |
292 | |
|
293 | 3 | getList().removeElementAt(_lastReturnedIndex); |
294 | |
} else { |
295 | |
|
296 | 304 | getList().removeElementAt(_lastReturnedIndex); |
297 | 304 | _nextIndex--; |
298 | |
} |
299 | 307 | _lastReturnedIndex = -1; |
300 | 307 | resyncModCount(); |
301 | 307 | } |
302 | |
|
303 | |
public void set(double value) { |
304 | 64 | assertNotComodified(); |
305 | 64 | if(-1 == _lastReturnedIndex) { |
306 | 14 | throw new IllegalStateException(); |
307 | |
} else { |
308 | 50 | getList().set(_lastReturnedIndex,value); |
309 | 50 | resyncModCount(); |
310 | |
} |
311 | 50 | } |
312 | |
|
313 | 8319 | private int _nextIndex = 0; |
314 | 8319 | private int _lastReturnedIndex = -1; |
315 | |
} |
316 | |
|
317 | |
protected static class RandomAccessDoubleSubList extends RandomAccessDoubleList implements DoubleList { |
318 | 171 | RandomAccessDoubleSubList(RandomAccessDoubleList list, int fromIndex, int toIndex) { |
319 | 171 | if(fromIndex < 0 || toIndex > list.size()) { |
320 | 3 | throw new IndexOutOfBoundsException(); |
321 | 168 | } else if(fromIndex > toIndex) { |
322 | 1 | throw new IllegalArgumentException(); |
323 | |
} else { |
324 | 167 | _list = list; |
325 | 167 | _offset = fromIndex; |
326 | 167 | _limit = toIndex - fromIndex; |
327 | 167 | _comod = new ComodChecker(list); |
328 | 167 | _comod.resyncModCount(); |
329 | |
} |
330 | 167 | } |
331 | |
|
332 | |
public double get(int index) { |
333 | 23711 | checkRange(index); |
334 | 23702 | _comod.assertNotComodified(); |
335 | 23702 | return _list.get(toUnderlyingIndex(index)); |
336 | |
} |
337 | |
|
338 | |
public double removeElementAt(int index) { |
339 | 167 | checkRange(index); |
340 | 158 | _comod.assertNotComodified(); |
341 | 158 | double val = _list.removeElementAt(toUnderlyingIndex(index)); |
342 | 158 | _limit--; |
343 | 158 | _comod.resyncModCount(); |
344 | 158 | incrModCount(); |
345 | 158 | return val; |
346 | |
} |
347 | |
|
348 | |
public double set(int index, double element) { |
349 | 37 | checkRange(index); |
350 | 28 | _comod.assertNotComodified(); |
351 | 28 | double val = _list.set(toUnderlyingIndex(index),element); |
352 | 28 | incrModCount(); |
353 | 28 | _comod.resyncModCount(); |
354 | 28 | return val; |
355 | |
} |
356 | |
|
357 | |
public void add(int index, double element) { |
358 | 140 | checkRangeIncludingEndpoint(index); |
359 | 132 | _comod.assertNotComodified(); |
360 | 132 | _list.add(toUnderlyingIndex(index),element); |
361 | 132 | _limit++; |
362 | 132 | _comod.resyncModCount(); |
363 | 132 | incrModCount(); |
364 | 132 | } |
365 | |
|
366 | |
public int size() { |
367 | 69219 | _comod.assertNotComodified(); |
368 | 69219 | return _limit; |
369 | |
} |
370 | |
|
371 | |
private void checkRange(int index) { |
372 | 23915 | if(index < 0 || index >= size()) { |
373 | 27 | throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + ")"); |
374 | |
} |
375 | 23888 | } |
376 | |
|
377 | |
private void checkRangeIncludingEndpoint(int index) { |
378 | 140 | if(index < 0 || index > size()) { |
379 | 8 | throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + "]"); |
380 | |
} |
381 | 132 | } |
382 | |
|
383 | |
private int toUnderlyingIndex(int index) { |
384 | 24020 | return (index + _offset); |
385 | |
} |
386 | |
|
387 | 171 | private int _offset = 0; |
388 | 171 | private int _limit = 0; |
389 | 171 | private RandomAccessDoubleList _list = null; |
390 | 171 | private ComodChecker _comod = null; |
391 | |
|
392 | |
} |
393 | |
} |
394 | |
|