| 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 RandomAccessFloatList extends AbstractFloatCollection implements FloatList { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 798 | protected RandomAccessFloatList() { |
| 47 | 798 | } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public abstract float get(int index); |
| 53 | |
public abstract int size(); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public float removeElementAt(int index) { |
| 63 | 1 | throw new UnsupportedOperationException(); |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public float set(int index, float element) { |
| 71 | 2 | throw new UnsupportedOperationException(); |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public void add(int index, float element) { |
| 79 | 2 | throw new UnsupportedOperationException(); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public boolean add(float element) { |
| 87 | 2752 | add(size(),element); |
| 88 | 2750 | return true; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public boolean addAll(int index, FloatCollection collection) { |
| 92 | 1 | boolean modified = false; |
| 93 | 1 | for(FloatIterator 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(float element) { |
| 101 | 84 | int i = 0; |
| 102 | 84 | for(FloatIterator 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(float element) { |
| 113 | 84 | for(FloatListIterator 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 FloatIterator iterator() { |
| 122 | 6820 | return listIterator(); |
| 123 | |
} |
| 124 | |
|
| 125 | |
public FloatListIterator listIterator() { |
| 126 | 8089 | return listIterator(0); |
| 127 | |
} |
| 128 | |
|
| 129 | |
public FloatListIterator listIterator(int index) { |
| 130 | 8319 | return new RandomAccessFloatListIterator(this,index); |
| 131 | |
} |
| 132 | |
|
| 133 | |
public FloatList subList(int fromIndex, int toIndex) { |
| 134 | 171 | return new RandomAccessFloatSubList(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 FloatList) { |
| 141 | 26 | FloatList thatList = (FloatList)that; |
| 142 | 26 | if(size() != thatList.size()) { |
| 143 | 4 | return false; |
| 144 | |
} |
| 145 | 22 | for(FloatIterator 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(FloatIterator iter = iterator(); iter.hasNext(); ) { |
| 159 | 15509 | hash = 31*hash + Float.floatToIntBits(iter.next()); |
| 160 | |
} |
| 161 | 1165 | return hash; |
| 162 | |
} |
| 163 | |
|
| 164 | |
public String toString() { |
| 165 | 6 | StringBuffer buf = new StringBuffer(); |
| 166 | 6 | buf.append("["); |
| 167 | 6 | for(FloatIterator iter = iterator(); iter.hasNext();) { |
| 168 | 42 | buf.append(iter.next()); |
| 169 | 42 | if(iter.hasNext()) { |
| 170 | 39 | buf.append(", "); |
| 171 | |
} |
| 172 | |
} |
| 173 | 6 | buf.append("]"); |
| 174 | 6 | return buf.toString(); |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
protected int getModCount() { |
| 182 | 433390 | return _modCount; |
| 183 | |
} |
| 184 | |
|
| 185 | |
|
| 186 | |
protected void incrModCount() { |
| 187 | 7534 | _modCount++; |
| 188 | 7534 | } |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | 798 | private int _modCount = 0; |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
private static class ComodChecker { |
| 199 | 8486 | ComodChecker(RandomAccessFloatList source) { |
| 200 | 8486 | _source = source; |
| 201 | 8486 | resyncModCount(); |
| 202 | 8486 | } |
| 203 | |
|
| 204 | |
protected RandomAccessFloatList getList() { |
| 205 | 757578 | return _source; |
| 206 | |
} |
| 207 | |
|
| 208 | |
protected void assertNotComodified() throws ConcurrentModificationException { |
| 209 | 415600 | if(_expectedModCount != getList().getModCount()) { |
| 210 | 1 | throw new ConcurrentModificationException(); |
| 211 | |
} |
| 212 | 415599 | } |
| 213 | |
|
| 214 | |
protected void resyncModCount() { |
| 215 | 17790 | _expectedModCount = getList().getModCount(); |
| 216 | 17790 | } |
| 217 | |
|
| 218 | 8486 | private RandomAccessFloatList _source = null; |
| 219 | 8486 | private int _expectedModCount = -1; |
| 220 | |
} |
| 221 | |
|
| 222 | |
protected static class RandomAccessFloatListIterator extends ComodChecker implements FloatListIterator { |
| 223 | |
RandomAccessFloatListIterator(RandomAccessFloatList list, int index) { |
| 224 | 8319 | super(list); |
| 225 | 8319 | if(index < 0 || index > getList().size()) { |
| 226 | 9 | throw new IndexOutOfBoundsException("Index " + index + " not in [0," + getList().size() + ")"); |
| 227 | |
} else { |
| 228 | 8310 | _nextIndex = index; |
| 229 | 8310 | resyncModCount(); |
| 230 | |
} |
| 231 | 8310 | } |
| 232 | |
|
| 233 | |
public boolean hasNext() { |
| 234 | 211073 | assertNotComodified(); |
| 235 | 211073 | return _nextIndex < getList().size(); |
| 236 | |
} |
| 237 | |
|
| 238 | |
public boolean hasPrevious() { |
| 239 | 3663 | assertNotComodified(); |
| 240 | 3663 | return _nextIndex > 0; |
| 241 | |
} |
| 242 | |
|
| 243 | |
public int nextIndex() { |
| 244 | 1369 | assertNotComodified(); |
| 245 | 1369 | return _nextIndex; |
| 246 | |
} |
| 247 | |
|
| 248 | |
public int previousIndex() { |
| 249 | 1327 | assertNotComodified(); |
| 250 | 1327 | return _nextIndex - 1; |
| 251 | |
} |
| 252 | |
|
| 253 | |
public float next() { |
| 254 | 102600 | assertNotComodified(); |
| 255 | 102599 | if(!hasNext()) { |
| 256 | 72 | throw new NoSuchElementException(); |
| 257 | |
} else { |
| 258 | 102527 | float val = getList().get(_nextIndex); |
| 259 | 102527 | _lastReturnedIndex = _nextIndex; |
| 260 | 102527 | _nextIndex++; |
| 261 | 102527 | return val; |
| 262 | |
} |
| 263 | |
} |
| 264 | |
|
| 265 | |
public float previous() { |
| 266 | 1856 | assertNotComodified(); |
| 267 | 1856 | if(!hasPrevious()) { |
| 268 | 101 | throw new NoSuchElementException(); |
| 269 | |
} else { |
| 270 | 1755 | float val = getList().get(_nextIndex-1); |
| 271 | 1755 | _lastReturnedIndex = _nextIndex-1; |
| 272 | 1755 | _nextIndex--; |
| 273 | 1755 | return val; |
| 274 | |
} |
| 275 | |
} |
| 276 | |
|
| 277 | |
public void add(float value) { |
| 278 | 152 | assertNotComodified(); |
| 279 | 152 | getList().add(_nextIndex,value); |
| 280 | 152 | _nextIndex++; |
| 281 | 152 | _lastReturnedIndex = -1; |
| 282 | 152 | resyncModCount(); |
| 283 | 152 | } |
| 284 | |
|
| 285 | |
public void remove() { |
| 286 | 333 | assertNotComodified(); |
| 287 | 333 | if (_lastReturnedIndex == -1) { |
| 288 | 26 | throw new IllegalStateException(); |
| 289 | |
} |
| 290 | 307 | if (_lastReturnedIndex == _nextIndex) { |
| 291 | |
|
| 292 | 3 | getList().removeElementAt(_lastReturnedIndex); |
| 293 | |
} else { |
| 294 | |
|
| 295 | 304 | getList().removeElementAt(_lastReturnedIndex); |
| 296 | 304 | _nextIndex--; |
| 297 | |
} |
| 298 | 307 | _lastReturnedIndex = -1; |
| 299 | 307 | resyncModCount(); |
| 300 | 307 | } |
| 301 | |
|
| 302 | |
public void set(float value) { |
| 303 | 64 | assertNotComodified(); |
| 304 | 64 | if(-1 == _lastReturnedIndex) { |
| 305 | 14 | throw new IllegalStateException(); |
| 306 | |
} else { |
| 307 | 50 | getList().set(_lastReturnedIndex,value); |
| 308 | 50 | resyncModCount(); |
| 309 | |
} |
| 310 | 50 | } |
| 311 | |
|
| 312 | 8319 | private int _nextIndex = 0; |
| 313 | 8319 | private int _lastReturnedIndex = -1; |
| 314 | |
} |
| 315 | |
|
| 316 | |
protected static class RandomAccessFloatSubList extends RandomAccessFloatList implements FloatList { |
| 317 | 171 | RandomAccessFloatSubList(RandomAccessFloatList list, int fromIndex, int toIndex) { |
| 318 | 171 | if(fromIndex < 0 || toIndex > list.size()) { |
| 319 | 3 | throw new IndexOutOfBoundsException(); |
| 320 | 168 | } else if(fromIndex > toIndex) { |
| 321 | 1 | throw new IllegalArgumentException(); |
| 322 | |
} else { |
| 323 | 167 | _list = list; |
| 324 | 167 | _offset = fromIndex; |
| 325 | 167 | _limit = toIndex - fromIndex; |
| 326 | 167 | _comod = new ComodChecker(list); |
| 327 | 167 | _comod.resyncModCount(); |
| 328 | |
} |
| 329 | 167 | } |
| 330 | |
|
| 331 | |
public float get(int index) { |
| 332 | 23692 | checkRange(index); |
| 333 | 23683 | _comod.assertNotComodified(); |
| 334 | 23683 | return _list.get(toUnderlyingIndex(index)); |
| 335 | |
} |
| 336 | |
|
| 337 | |
public float removeElementAt(int index) { |
| 338 | 167 | checkRange(index); |
| 339 | 158 | _comod.assertNotComodified(); |
| 340 | 158 | float val = _list.removeElementAt(toUnderlyingIndex(index)); |
| 341 | 158 | _limit--; |
| 342 | 158 | _comod.resyncModCount(); |
| 343 | 158 | incrModCount(); |
| 344 | 158 | return val; |
| 345 | |
} |
| 346 | |
|
| 347 | |
public float set(int index, float element) { |
| 348 | 37 | checkRange(index); |
| 349 | 28 | _comod.assertNotComodified(); |
| 350 | 28 | float val = _list.set(toUnderlyingIndex(index),element); |
| 351 | 28 | incrModCount(); |
| 352 | 28 | _comod.resyncModCount(); |
| 353 | 28 | return val; |
| 354 | |
} |
| 355 | |
|
| 356 | |
public void add(int index, float element) { |
| 357 | 140 | checkRangeIncludingEndpoint(index); |
| 358 | 132 | _comod.assertNotComodified(); |
| 359 | 132 | _list.add(toUnderlyingIndex(index),element); |
| 360 | 132 | _limit++; |
| 361 | 132 | _comod.resyncModCount(); |
| 362 | 132 | incrModCount(); |
| 363 | 132 | } |
| 364 | |
|
| 365 | |
public int size() { |
| 366 | 69162 | _comod.assertNotComodified(); |
| 367 | 69162 | return _limit; |
| 368 | |
} |
| 369 | |
|
| 370 | |
private void checkRange(int index) { |
| 371 | 23896 | if(index < 0 || index >= size()) { |
| 372 | 27 | throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + ")"); |
| 373 | |
} |
| 374 | 23869 | } |
| 375 | |
|
| 376 | |
private void checkRangeIncludingEndpoint(int index) { |
| 377 | 140 | if(index < 0 || index > size()) { |
| 378 | 8 | throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + "]"); |
| 379 | |
} |
| 380 | 132 | } |
| 381 | |
|
| 382 | |
private int toUnderlyingIndex(int index) { |
| 383 | 24001 | return (index + _offset); |
| 384 | |
} |
| 385 | |
|
| 386 | 171 | private int _offset = 0; |
| 387 | 171 | private int _limit = 0; |
| 388 | 171 | private RandomAccessFloatList _list = null; |
| 389 | 171 | private ComodChecker _comod = null; |
| 390 | |
|
| 391 | |
} |
| 392 | |
} |
| 393 | |
|