Skip to content

Navigation Menu

Sign in
Sign up

Make Lists.transform() views delegate spliterator()/forEach() to the backing list #8589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pangwangshu wants to merge 3 commits into google:master
base: master
Choose a base branch
Loading
from pangwangshu:wapang/issue8165
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions guava-tests/test/com/google/common/collect/ListsTest.java
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.Spliterator;
import java.util.concurrent.CopyOnWriteArrayList;
import junit.framework.Test;
import junit.framework.TestCase;
Expand Down Expand Up @@ -822,6 +823,62 @@ public void testTransformIteratorSequential() {
assertTransformIterator(list);
}

@GwtIncompatible // CopyOnWriteArrayList, Spliterator
public void testTransformSpliteratorDoesNotThrowConcurrentModificationException() {
// CopyOnWriteArrayList is documented to never throw ConcurrentModificationException; Lists
// .transform() is documented to return a threadsafe list when the input list and function are
// threadsafe. The spliterator of the transformed list must honor that by delegating to the
// backing list's own (snapshotting) spliterator, rather than falling back to the JDK's default
// index-based RandomAccessSpliterator, which translates concurrent structural changes into a
// ConcurrentModificationException.
CopyOnWriteArrayList<Integer> fromList = new CopyOnWriteArrayList<>(SOME_LIST);
List<String> transformed = transform(fromList, SOME_FUNCTION);

Spliterator<String> spliterator = transformed.spliterator();
List<String> results = new ArrayList<>();
assertTrue(spliterator.tryAdvance(results::add));

// Structurally mutate the backing list after the spliterator was created but before it has
// finished being consumed.
fromList.clear();

// Must not throw, and must still report the elements from the original snapshot, exactly as
// fromList.spliterator() itself would.
spliterator.forEachRemaining(results::add);
assertEquals(SOME_STRING_LIST, results);
}

@GwtIncompatible // CopyOnWriteArrayList, Spliterator
public void testTransformSpliteratorPropagatesBackingListCharacteristics() {
CopyOnWriteArrayList<Integer> fromList = new CopyOnWriteArrayList<>(SOME_LIST);
List<String> transformed = transform(fromList, SOME_FUNCTION);

// CopyOnWriteArrayList.spliterator() reports IMMUTABLE (among other bits); a spliterator that
// instead falls back to the default RandomAccessSpliterator would not, since that
// implementation has no way of knowing the backing list is safe to snapshot.
assertTrue(transformed.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
}

@GwtIncompatible // Spliterator
public void testTransformSpliteratorRandomAccess() {
List<Integer> fromList = new ArrayList<>(SOME_LIST);
List<String> transformed = transform(fromList, SOME_FUNCTION);

List<String> results = new ArrayList<>();
transformed.spliterator().forEachRemaining(results::add);
assertEquals(SOME_STRING_LIST, results);
}

@GwtIncompatible // Spliterator
public void testTransformForEachRandomAccess() {
List<Integer> fromList = new ArrayList<>(SOME_LIST);
List<String> transformed = transform(fromList, SOME_FUNCTION);

List<String> results = new ArrayList<>();
transformed.forEach(results::add);
assertEquals(SOME_STRING_LIST, results);
}

/**
* This test depends on the fact that {@code AbstractSequentialList.iterator} transforms the
* {@code iterator()} call into a call on {@code listIterator(int)}. This is fine because the
Expand Down
14 changes: 14 additions & 0 deletions guava/src/com/google/common/collect/Lists.java
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.RandomAccess;
import java.util.Spliterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -661,6 +663,18 @@ public boolean isEmpty() {
return fromList.isEmpty();
}

@Override
@GwtIncompatible // Spliterator
public Spliterator<T> spliterator() {
return CollectSpliterators.map(fromList.spliterator(), 0, function);
}

@Override
public void forEach(Consumer<? super T> action) {
checkNotNull(action);
fromList.forEach((F f) -> action.accept(function.apply(f)));
}

@Override
public boolean removeIf(Predicate<? super T> filter) {
checkNotNull(filter);
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /