1264 if (index < 0 || index >= this.size) 1265 throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 1266 } 1267 1268 private void rangeCheckForAdd(int index) { 1269 if (index < 0 || index > this.size) 1270 throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 1271 } 1272 1273 private String outOfBoundsMsg(int index) { 1274 return "Index: "+index+", Size: "+this.size; 1275 } 1276 1277 private void checkForComodification() { 1278 if (ArrayList.this.modCount != this.modCount) 1279 throw new ConcurrentModificationException(); 1280 } 1281 1282 public Spliterator<E> spliterator() { 1283 checkForComodification(); 1284 return new ArrayListSpliterator<>(ArrayList.this, offset, 1285 offset + this.size, this.modCount); 1286 } 1287 } 1288 1289 @Override 1290 public void forEach(Consumer<? super E> action) { 1291 Objects.requireNonNull(action); 1292 final int expectedModCount = modCount; 1293 @SuppressWarnings("unchecked") 1294 final E[] elementData = (E[]) this.elementData; 1295 final int size = this.size; 1296 for (int i=0; modCount == expectedModCount && i < size; i++) { 1297 action.accept(elementData[i]); 1298 } 1299 if (modCount != expectedModCount) { 1300 throw new ConcurrentModificationException(); 1301 } 1302 } 1303 1304 /** 1305 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> | 1264 if (index < 0 || index >= this.size) 1265 throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 1266 } 1267 1268 private void rangeCheckForAdd(int index) { 1269 if (index < 0 || index > this.size) 1270 throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 1271 } 1272 1273 private String outOfBoundsMsg(int index) { 1274 return "Index: "+index+", Size: "+this.size; 1275 } 1276 1277 private void checkForComodification() { 1278 if (ArrayList.this.modCount != this.modCount) 1279 throw new ConcurrentModificationException(); 1280 } 1281 1282 public Spliterator<E> spliterator() { 1283 checkForComodification(); 1284 1285 // ArrayListSpliterator is not used because late-binding logic 1286 // is different here 1287 return new Spliterator<>() { 1288 private int index = offset; // current index, modified on advance/split 1289 private int fence = -1; // -1 until used; then one past last index 1290 private int expectedModCount; // initialized when fence set 1291 1292 private int getFence() { // initialize fence to size on first use 1293 int hi; // (a specialized variant appears in method forEach) 1294 if ((hi = fence) < 0) { 1295 expectedModCount = modCount; 1296 hi = fence = offset + size; 1297 } 1298 return hi; 1299 } 1300 1301 public ArrayListSpliterator<E> trySplit() { 1302 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; 1303 // ArrayListSpliterator could be used here as the source is already bound 1304 return (lo >= mid) ? null : // divide range in half unless too small 1305 new ArrayListSpliterator<>(ArrayList.this, lo, index = mid, 1306 expectedModCount); 1307 } 1308 1309 public boolean tryAdvance(Consumer<? super E> action) { 1310 Objects.requireNonNull(action); 1311 int hi = getFence(), i = index; 1312 if (i < hi) { 1313 index = i + 1; 1314 @SuppressWarnings("unchecked") E e = (E)elementData[i]; 1315 action.accept(e); 1316 if (ArrayList.this.modCount != expectedModCount) 1317 throw new ConcurrentModificationException(); 1318 return true; 1319 } 1320 return false; 1321 } 1322 1323 public void forEachRemaining(Consumer<? super E> action) { 1324 Objects.requireNonNull(action); 1325 int i, hi, mc; // hoist accesses and checks from loop 1326 ArrayList<E> lst = ArrayList.this; 1327 Object[] a; 1328 if ((a = lst.elementData) != null) { 1329 if ((hi = fence) < 0) { 1330 mc = modCount; 1331 hi = offset + size; 1332 } 1333 else 1334 mc = expectedModCount; 1335 if ((i = index) >= 0 && (index = hi) <= a.length) { 1336 for (; i < hi; ++i) { 1337 @SuppressWarnings("unchecked") E e = (E) a[i]; 1338 action.accept(e); 1339 } 1340 if (lst.modCount == mc) 1341 return; 1342 } 1343 } 1344 throw new ConcurrentModificationException(); 1345 } 1346 1347 public long estimateSize() { 1348 return (long) (getFence() - index); 1349 } 1350 1351 public int characteristics() { 1352 return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; 1353 } 1354 }; 1355 } 1356 } 1357 1358 @Override 1359 public void forEach(Consumer<? super E> action) { 1360 Objects.requireNonNull(action); 1361 final int expectedModCount = modCount; 1362 @SuppressWarnings("unchecked") 1363 final E[] elementData = (E[]) this.elementData; 1364 final int size = this.size; 1365 for (int i=0; modCount == expectedModCount && i < size; i++) { 1366 action.accept(elementData[i]); 1367 } 1368 if (modCount != expectedModCount) { 1369 throw new ConcurrentModificationException(); 1370 } 1371 } 1372 1373 /** 1374 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |