1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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>
|
1 /*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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 return new Spliterator<>() {
1286 private int index = offset; // current index, modified on advance/split
1287 private int fence = -1; // -1 until used; then one past last index
1288 private int expectedModCount; // initialized when fence set
1289
1290 private int getFence() { // initialize fence to size on first use
1291 int hi; // (a specialized variant appears in method forEach)
1292 if ((hi = fence) < 0) {
1293 expectedModCount = modCount;
1294 hi = fence = offset + size;
1295 }
1296 return hi;
1297 }
1298
1299 public ArrayListSpliterator<E> trySplit() {
1300 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1301 return (lo >= mid) ? null : // divide range in half unless too small
1302 new ArrayListSpliterator<>(ArrayList.this, lo, index = mid,
1303 expectedModCount);
1304 }
1305
1306 public boolean tryAdvance(Consumer<? super E> action) {
1307 Objects.requireNonNull(action);
1308 int hi = getFence(), i = index;
1309 if (i < hi) {
1310 index = i + 1;
1311 @SuppressWarnings("unchecked") E e = (E)elementData[i];
1312 action.accept(e);
1313 if (ArrayList.this.modCount != expectedModCount)
1314 throw new ConcurrentModificationException();
1315 return true;
1316 }
1317 return false;
1318 }
1319
1320 public void forEachRemaining(Consumer<? super E> action) {
1321 Objects.requireNonNull(action);
1322 int i, hi, mc; // hoist accesses and checks from loop
1323 ArrayList<E> lst = ArrayList.this;
1324 Object[] a;
1325 if ((a = lst.elementData) != null) {
1326 if ((hi = fence) < 0) {
1327 mc = modCount;
1328 hi = offset + size;
1329 }
1330 else
1331 mc = expectedModCount;
1332 if ((i = index) >= 0 && (index = hi) <= a.length) {
1333 for (; i < hi; ++i) {
1334 @SuppressWarnings("unchecked") E e = (E) a[i];
1335 action.accept(e);
1336 }
1337 if (lst.modCount == mc)
1338 return;
1339 }
1340 }
1341 throw new ConcurrentModificationException();
1342 }
1343
1344 public long estimateSize() {
1345 return (long) (getFence() - index);
1346 }
1347
1348 public int characteristics() {
1349 return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1350 }
1351 };
1352 }
1353 }
1354
1355 @Override
1356 public void forEach(Consumer<? super E> action) {
1357 Objects.requireNonNull(action);
1358 final int expectedModCount = modCount;
1359 @SuppressWarnings("unchecked")
1360 final E[] elementData = (E[]) this.elementData;
1361 final int size = this.size;
1362 for (int i=0; modCount == expectedModCount && i < size; i++) {
1363 action.accept(elementData[i]);
1364 }
1365 if (modCount != expectedModCount) {
1366 throw new ConcurrentModificationException();
1367 }
1368 }
1369
1370 /**
1371 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
|