| 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 RandomAccessShortList extends AbstractShortCollection implements ShortList { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 1340 | protected RandomAccessShortList() { |
| 47 | 1340 | } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public abstract short get(int index); |
| 53 | |
public abstract int size(); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public short removeElementAt(int index) { |
| 63 | 1 | throw new UnsupportedOperationException(); |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public short set(int index, short element) { |
| 71 | 2 | throw new UnsupportedOperationException(); |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public void add(int index, short element) { |
| 79 | 2 | throw new UnsupportedOperationException(); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public boolean add(short element) { |
| 87 | 9093 | add(size(),element); |
| 88 | 9089 | return true; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public boolean addAll(int index, ShortCollection collection) { |
| 92 | 4 | boolean modified = false; |
| 93 | 4 | for(ShortIterator iter = collection.iterator(); iter.hasNext(); ) { |
| 94 | 52 | add(index++,iter.next()); |
| 95 | 52 | modified = true; |
| 96 | |
} |
| 97 | 4 | return modified; |
| 98 | |
} |
| 99 | |
|
| 100 | |
public int indexOf(short element) { |
| 101 | 148 | int i = 0; |
| 102 | 148 | for(ShortIterator iter = iterator(); iter.hasNext(); ) { |
| 103 | 1741 | if(iter.next() == element) { |
| 104 | 74 | return i; |
| 105 | |
} else { |
| 106 | 1667 | i++; |
| 107 | |
} |
| 108 | |
} |
| 109 | 74 | return -1; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public int lastIndexOf(short element) { |
| 113 | 148 | for(ShortListIterator iter = listIterator(size()); iter.hasPrevious(); ) { |
| 114 | 1741 | if(iter.previous() == element) { |
| 115 | 74 | return iter.nextIndex(); |
| 116 | |
} |
| 117 | |
} |
| 118 | 74 | return -1; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public ShortIterator iterator() { |
| 122 | 12257 | return listIterator(); |
| 123 | |
} |
| 124 | |
|
| 125 | |
public ShortListIterator listIterator() { |
| 126 | 14508 | return listIterator(0); |
| 127 | |
} |
| 128 | |
|
| 129 | |
public ShortListIterator listIterator(int index) { |
| 130 | 14911 | return new RandomAccessShortListIterator(this,index); |
| 131 | |
} |
| 132 | |
|
| 133 | |
public ShortList subList(int fromIndex, int toIndex) { |
| 134 | 337 | return new RandomAccessShortSubList(this,fromIndex,toIndex); |
| 135 | |
} |
| 136 | |
|
| 137 | |
public boolean equals(Object that) { |
| 138 | 63 | if(this == that) { |
| 139 | 6 | return true; |
| 140 | 57 | } else if(that instanceof ShortList) { |
| 141 | 51 | ShortList thatList = (ShortList)that; |
| 142 | 51 | if(size() != thatList.size()) { |
| 143 | 8 | return false; |
| 144 | |
} |
| 145 | 43 | for(ShortIterator thatIter = thatList.iterator(), thisIter = iterator(); thisIter.hasNext();) { |
| 146 | 230 | if(thisIter.next() != thatIter.next()) { |
| 147 | 4 | return false; |
| 148 | |
} |
| 149 | |
} |
| 150 | 39 | return true; |
| 151 | |
} else { |
| 152 | 6 | return false; |
| 153 | |
} |
| 154 | |
} |
| 155 | |
|
| 156 | |
public int hashCode() { |
| 157 | 2103 | int hash = 1; |
| 158 | 2103 | for(ShortIterator iter = iterator(); iter.hasNext(); ) { |
| 159 | 29317 | hash = 31*hash + iter.next(); |
| 160 | |
} |
| 161 | 2103 | return hash; |
| 162 | |
} |
| 163 | |
|
| 164 | |
public String toString() { |
| 165 | 10 | StringBuffer buf = new StringBuffer(); |
| 166 | 10 | buf.append("["); |
| 167 | 10 | for(ShortIterator iter = iterator(); iter.hasNext();) { |
| 168 | 74 | buf.append(iter.next()); |
| 169 | 74 | if(iter.hasNext()) { |
| 170 | 69 | buf.append(", "); |
| 171 | |
} |
| 172 | |
} |
| 173 | 10 | buf.append("]"); |
| 174 | 10 | return buf.toString(); |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
protected int getModCount() { |
| 182 | 823867 | return _modCount; |
| 183 | |
} |
| 184 | |
|
| 185 | |
|
| 186 | |
protected void incrModCount() { |
| 187 | 21299 | _modCount++; |
| 188 | 21299 | } |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | 1340 | private int _modCount = 0; |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
private static class ComodChecker { |
| 199 | 15240 | ComodChecker(RandomAccessShortList source) { |
| 200 | 15240 | _source = source; |
| 201 | 15240 | resyncModCount(); |
| 202 | 15240 | } |
| 203 | |
|
| 204 | |
protected RandomAccessShortList getList() { |
| 205 | 1432809 | return _source; |
| 206 | |
} |
| 207 | |
|
| 208 | |
protected void assertNotComodified() throws ConcurrentModificationException { |
| 209 | 791889 | if(_expectedModCount != getList().getModCount()) { |
| 210 | 2 | throw new ConcurrentModificationException(); |
| 211 | |
} |
| 212 | 791887 | } |
| 213 | |
|
| 214 | |
protected void resyncModCount() { |
| 215 | 31978 | _expectedModCount = getList().getModCount(); |
| 216 | 31978 | } |
| 217 | |
|
| 218 | 15240 | private RandomAccessShortList _source = null; |
| 219 | 15240 | private int _expectedModCount = -1; |
| 220 | |
} |
| 221 | |
|
| 222 | |
protected static class RandomAccessShortListIterator extends ComodChecker implements ShortListIterator { |
| 223 | |
RandomAccessShortListIterator(RandomAccessShortList list, int index) { |
| 224 | 14911 | super(list); |
| 225 | 14911 | if(index < 0 || index > getList().size()) { |
| 226 | 16 | throw new IndexOutOfBoundsException("Index " + index + " not in [0," + getList().size() + ")"); |
| 227 | |
} else { |
| 228 | 14895 | _nextIndex = index; |
| 229 | 14895 | resyncModCount(); |
| 230 | |
} |
| 231 | 14895 | } |
| 232 | |
|
| 233 | |
public boolean hasNext() { |
| 234 | 396919 | assertNotComodified(); |
| 235 | 396919 | return _nextIndex < getList().size(); |
| 236 | |
} |
| 237 | |
|
| 238 | |
public boolean hasPrevious() { |
| 239 | 6625 | assertNotComodified(); |
| 240 | 6625 | return _nextIndex > 0; |
| 241 | |
} |
| 242 | |
|
| 243 | |
public int nextIndex() { |
| 244 | 2416 | assertNotComodified(); |
| 245 | 2416 | return _nextIndex; |
| 246 | |
} |
| 247 | |
|
| 248 | |
public int previousIndex() { |
| 249 | 2342 | assertNotComodified(); |
| 250 | 2342 | return _nextIndex - 1; |
| 251 | |
} |
| 252 | |
|
| 253 | |
public short next() { |
| 254 | 193167 | assertNotComodified(); |
| 255 | 193165 | if(!hasNext()) { |
| 256 | 120 | throw new NoSuchElementException(); |
| 257 | |
} else { |
| 258 | 193045 | short val = getList().get(_nextIndex); |
| 259 | 193045 | _lastReturnedIndex = _nextIndex; |
| 260 | 193045 | _nextIndex++; |
| 261 | 193045 | return val; |
| 262 | |
} |
| 263 | |
} |
| 264 | |
|
| 265 | |
public short previous() { |
| 266 | 3355 | assertNotComodified(); |
| 267 | 3355 | if(!hasPrevious()) { |
| 268 | 175 | throw new NoSuchElementException(); |
| 269 | |
} else { |
| 270 | 3180 | short val = getList().get(_nextIndex-1); |
| 271 | 3180 | _lastReturnedIndex = _nextIndex-1; |
| 272 | 3180 | _nextIndex--; |
| 273 | 3180 | return val; |
| 274 | |
} |
| 275 | |
} |
| 276 | |
|
| 277 | |
public void add(short value) { |
| 278 | 256 | assertNotComodified(); |
| 279 | 256 | getList().add(_nextIndex,value); |
| 280 | 256 | _nextIndex++; |
| 281 | 256 | _lastReturnedIndex = -1; |
| 282 | 256 | resyncModCount(); |
| 283 | 256 | } |
| 284 | |
|
| 285 | |
public void remove() { |
| 286 | 574 | assertNotComodified(); |
| 287 | 574 | if (_lastReturnedIndex == -1) { |
| 288 | 38 | throw new IllegalStateException(); |
| 289 | |
} |
| 290 | 536 | if (_lastReturnedIndex == _nextIndex) { |
| 291 | |
|
| 292 | 5 | getList().removeElementAt(_lastReturnedIndex); |
| 293 | |
} else { |
| 294 | |
|
| 295 | 531 | getList().removeElementAt(_lastReturnedIndex); |
| 296 | 531 | _nextIndex--; |
| 297 | |
} |
| 298 | 536 | _lastReturnedIndex = -1; |
| 299 | 536 | resyncModCount(); |
| 300 | 536 | } |
| 301 | |
|
| 302 | |
public void set(short value) { |
| 303 | 108 | assertNotComodified(); |
| 304 | 108 | if(-1 == _lastReturnedIndex) { |
| 305 | 22 | throw new IllegalStateException(); |
| 306 | |
} else { |
| 307 | 86 | getList().set(_lastReturnedIndex,value); |
| 308 | 86 | resyncModCount(); |
| 309 | |
} |
| 310 | 86 | } |
| 311 | |
|
| 312 | 14911 | private int _nextIndex = 0; |
| 313 | 14911 | private int _lastReturnedIndex = -1; |
| 314 | |
} |
| 315 | |
|
| 316 | |
protected static class RandomAccessShortSubList extends RandomAccessShortList implements ShortList { |
| 317 | 337 | RandomAccessShortSubList(RandomAccessShortList list, int fromIndex, int toIndex) { |
| 318 | 337 | if(fromIndex < 0 || toIndex > list.size()) { |
| 319 | 6 | throw new IndexOutOfBoundsException(); |
| 320 | 331 | } else if(fromIndex > toIndex) { |
| 321 | 2 | throw new IllegalArgumentException(); |
| 322 | |
} else { |
| 323 | 329 | _list = list; |
| 324 | 329 | _offset = fromIndex; |
| 325 | 329 | _limit = toIndex - fromIndex; |
| 326 | 329 | _comod = new ComodChecker(list); |
| 327 | 329 | _comod.resyncModCount(); |
| 328 | |
} |
| 329 | 329 | } |
| 330 | |
|
| 331 | |
public short get(int index) { |
| 332 | 47335 | checkRange(index); |
| 333 | 47317 | _comod.assertNotComodified(); |
| 334 | 47317 | return _list.get(toUnderlyingIndex(index)); |
| 335 | |
} |
| 336 | |
|
| 337 | |
public short removeElementAt(int index) { |
| 338 | 334 | checkRange(index); |
| 339 | 316 | _comod.assertNotComodified(); |
| 340 | 316 | short val = _list.removeElementAt(toUnderlyingIndex(index)); |
| 341 | 316 | _limit--; |
| 342 | 316 | _comod.resyncModCount(); |
| 343 | 316 | incrModCount(); |
| 344 | 316 | return val; |
| 345 | |
} |
| 346 | |
|
| 347 | |
public short set(int index, short element) { |
| 348 | 74 | checkRange(index); |
| 349 | 56 | _comod.assertNotComodified(); |
| 350 | 56 | short val = _list.set(toUnderlyingIndex(index),element); |
| 351 | 56 | incrModCount(); |
| 352 | 56 | _comod.resyncModCount(); |
| 353 | 56 | return val; |
| 354 | |
} |
| 355 | |
|
| 356 | |
public void add(int index, short element) { |
| 357 | 280 | checkRangeIncludingEndpoint(index); |
| 358 | 264 | _comod.assertNotComodified(); |
| 359 | 264 | _list.add(toUnderlyingIndex(index),element); |
| 360 | 264 | _limit++; |
| 361 | 264 | _comod.resyncModCount(); |
| 362 | 264 | incrModCount(); |
| 363 | 264 | } |
| 364 | |
|
| 365 | |
public int size() { |
| 366 | 138174 | _comod.assertNotComodified(); |
| 367 | 138174 | return _limit; |
| 368 | |
} |
| 369 | |
|
| 370 | |
private void checkRange(int index) { |
| 371 | 47743 | if(index < 0 || index >= size()) { |
| 372 | 54 | throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + ")"); |
| 373 | |
} |
| 374 | 47689 | } |
| 375 | |
|
| 376 | |
private void checkRangeIncludingEndpoint(int index) { |
| 377 | 280 | if(index < 0 || index > size()) { |
| 378 | 16 | throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + "]"); |
| 379 | |
} |
| 380 | 264 | } |
| 381 | |
|
| 382 | |
private int toUnderlyingIndex(int index) { |
| 383 | 47953 | return (index + _offset); |
| 384 | |
} |
| 385 | |
|
| 386 | 337 | private int _offset = 0; |
| 387 | 337 | private int _limit = 0; |
| 388 | 337 | private RandomAccessShortList _list = null; |
| 389 | 337 | private ComodChecker _comod = null; |
| 390 | |
|
| 391 | |
} |
| 392 | |
} |
| 393 | |
|