diff options
author | akhil <none@none> | 2013-04-22 09:19:34 -0700 |
---|---|---|
committer | akhil <none@none> | 2013-04-22 09:19:34 -0700 |
commit | f8babfc90ab77a734b5bc98dfab748480f42980e (patch) | |
tree | 142948760f600d3c8908f0b1fa78e6e98ed23a75 /src/share/classes/java/util/Vector.java | |
parent | de889ef6b450819545566cf194a41ab4ac7f4b16 (diff) | |
download | jdk8u_jdk-f8babfc90ab77a734b5bc98dfab748480f42980e.tar.gz |
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
Reviewed-by: alanb, dholmes, mduigou, psandoz, smarks
Contributed-by: Akhil Arora <akhil.arora@oracle.com>, Arne Siegel <v.a.ammodytes@googlemail.com>, Brian Goetz <brian.goetz@oracle.com>
Diffstat (limited to 'src/share/classes/java/util/Vector.java')
-rw-r--r-- | src/share/classes/java/util/Vector.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/share/classes/java/util/Vector.java b/src/share/classes/java/util/Vector.java index be74f617f5..ef02e74eea 100644 --- a/src/share/classes/java/util/Vector.java +++ b/src/share/classes/java/util/Vector.java @@ -25,6 +25,10 @@ package java.util; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; + /** * The {@code Vector} class implements a growable array of * objects. Like an array, it contains components that can be @@ -1209,4 +1213,89 @@ public class Vector<E> lastRet = -1; } } + + @Override + public synchronized void forEach(Consumer<? super E> action) { + Objects.requireNonNull(action); + final int expectedModCount = modCount; + @SuppressWarnings("unchecked") + final E[] elementData = (E[]) this.elementData; + final int elementCount = this.elementCount; + for (int i=0; modCount == expectedModCount && i < elementCount; i++) { + action.accept(elementData[i]); + } + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + } + + @Override + @SuppressWarnings("unchecked") + public synchronized boolean removeIf(Predicate<? super E> filter) { + Objects.requireNonNull(filter); + // figure out which elements are to be removed + // any exception thrown from the filter predicate at this stage + // will leave the collection unmodified + int removeCount = 0; + final int size = elementCount; + final BitSet removeSet = new BitSet(size); + final int expectedModCount = modCount; + for (int i=0; modCount == expectedModCount && i < size; i++) { + @SuppressWarnings("unchecked") + final E element = (E) elementData[i]; + if (filter.test(element)) { + removeSet.set(i); + removeCount++; + } + } + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + + // shift surviving elements left over the spaces left by removed elements + final boolean anyToRemove = removeCount > 0; + if (anyToRemove) { + final int newSize = size - removeCount; + for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) { + i = removeSet.nextClearBit(i); + elementData[j] = elementData[i]; + } + for (int k=newSize; k < size; k++) { + elementData[k] = null; // Let gc do its work + } + elementCount = newSize; + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + modCount++; + } + + return anyToRemove; + } + + @Override + @SuppressWarnings("unchecked") + public synchronized void replaceAll(UnaryOperator<E> operator) { + Objects.requireNonNull(operator); + final int expectedModCount = modCount; + final int size = elementCount; + for (int i=0; modCount == expectedModCount && i < size; i++) { + elementData[i] = operator.apply((E) elementData[i]); + } + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + modCount++; + } + + @Override + @SuppressWarnings("unchecked") + public synchronized void sort(Comparator<? super E> c) { + final int expectedModCount = modCount; + Arrays.sort((E[]) elementData, 0, elementCount, c); + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + modCount++; + } } |