22 * or visit www.oracle.com if you need additional information or have any |
22 * or visit www.oracle.com if you need additional information or have any |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package java.util; |
26 package java.util; |
|
27 |
|
28 import java.util.function.Consumer; |
|
29 import java.util.function.Predicate; |
|
30 import java.util.function.UnaryOperator; |
27 |
31 |
28 /** |
32 /** |
29 * Resizable-array implementation of the <tt>List</tt> interface. Implements |
33 * Resizable-array implementation of the <tt>List</tt> interface. Implements |
30 * all optional list operations, and permits all elements, including |
34 * all optional list operations, and permits all elements, including |
31 * <tt>null</tt>. In addition to implementing the <tt>List</tt> interface, |
35 * <tt>null</tt>. In addition to implementing the <tt>List</tt> interface, |
1166 private void checkForComodification() { |
1170 private void checkForComodification() { |
1167 if (ArrayList.this.modCount != this.modCount) |
1171 if (ArrayList.this.modCount != this.modCount) |
1168 throw new ConcurrentModificationException(); |
1172 throw new ConcurrentModificationException(); |
1169 } |
1173 } |
1170 } |
1174 } |
|
1175 |
|
1176 @Override |
|
1177 public void forEach(Consumer<? super E> action) { |
|
1178 Objects.requireNonNull(action); |
|
1179 final int expectedModCount = modCount; |
|
1180 @SuppressWarnings("unchecked") |
|
1181 final E[] elementData = (E[]) this.elementData; |
|
1182 final int size = this.size; |
|
1183 for (int i=0; modCount == expectedModCount && i < size; i++) { |
|
1184 action.accept(elementData[i]); |
|
1185 } |
|
1186 if (modCount != expectedModCount) { |
|
1187 throw new ConcurrentModificationException(); |
|
1188 } |
|
1189 } |
|
1190 |
|
1191 @Override |
|
1192 public boolean removeIf(Predicate<? super E> filter) { |
|
1193 Objects.requireNonNull(filter); |
|
1194 // figure out which elements are to be removed |
|
1195 // any exception thrown from the filter predicate at this stage |
|
1196 // will leave the collection unmodified |
|
1197 int removeCount = 0; |
|
1198 final BitSet removeSet = new BitSet(size); |
|
1199 final int expectedModCount = modCount; |
|
1200 final int size = this.size; |
|
1201 for (int i=0; modCount == expectedModCount && i < size; i++) { |
|
1202 @SuppressWarnings("unchecked") |
|
1203 final E element = (E) elementData[i]; |
|
1204 if (filter.test(element)) { |
|
1205 removeSet.set(i); |
|
1206 removeCount++; |
|
1207 } |
|
1208 } |
|
1209 if (modCount != expectedModCount) { |
|
1210 throw new ConcurrentModificationException(); |
|
1211 } |
|
1212 |
|
1213 // shift surviving elements left over the spaces left by removed elements |
|
1214 final boolean anyToRemove = removeCount > 0; |
|
1215 if (anyToRemove) { |
|
1216 final int newSize = size - removeCount; |
|
1217 for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) { |
|
1218 i = removeSet.nextClearBit(i); |
|
1219 elementData[j] = elementData[i]; |
|
1220 } |
|
1221 for (int k=newSize; k < size; k++) { |
|
1222 elementData[k] = null; // Let gc do its work |
|
1223 } |
|
1224 this.size = newSize; |
|
1225 if (modCount != expectedModCount) { |
|
1226 throw new ConcurrentModificationException(); |
|
1227 } |
|
1228 modCount++; |
|
1229 } |
|
1230 |
|
1231 return anyToRemove; |
|
1232 } |
|
1233 |
|
1234 @Override |
|
1235 @SuppressWarnings("unchecked") |
|
1236 public void replaceAll(UnaryOperator<E> operator) { |
|
1237 Objects.requireNonNull(operator); |
|
1238 final int expectedModCount = modCount; |
|
1239 final int size = this.size; |
|
1240 for (int i=0; modCount == expectedModCount && i < size; i++) { |
|
1241 elementData[i] = operator.apply((E) elementData[i]); |
|
1242 } |
|
1243 if (modCount != expectedModCount) { |
|
1244 throw new ConcurrentModificationException(); |
|
1245 } |
|
1246 modCount++; |
|
1247 } |
|
1248 |
|
1249 @Override |
|
1250 @SuppressWarnings("unchecked") |
|
1251 public void sort(Comparator<? super E> c) { |
|
1252 final int expectedModCount = modCount; |
|
1253 Arrays.sort((E[]) elementData, 0, size, c); |
|
1254 if (modCount != expectedModCount) { |
|
1255 throw new ConcurrentModificationException(); |
|
1256 } |
|
1257 modCount++; |
|
1258 } |
1171 } |
1259 } |