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