Skip to content

Commit

Permalink
Extend ImmutableCollectionIterator refaster rule
Browse files Browse the repository at this point in the history
By re-writing `Stream#iterator` statements.
  • Loading branch information
mohamedsamehsalah committed Sep 30, 2024
1 parent 097af51 commit beb74e6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,11 @@ S[] after(ImmutableCollection<T> collection, IntFunction<S[]> generator) {
}
}

/**
* Don't call {@link ImmutableCollection#asList()} if {@link ImmutableCollection#iterator()} is
* called on the result; call it directly.
*/
/** Prefer {@link ImmutableCollection#iterator()} over more contrived alternatives. */
static final class ImmutableCollectionIterator<T> {
@BeforeTemplate
Iterator<T> before(ImmutableCollection<T> collection) {
return collection.asList().iterator();
return Refaster.anyOf(collection.stream().iterator(), collection.asList().iterator());
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ Integer[] testImmutableCollectionToArrayWithGenerator() {
return ImmutableSet.of(1).asList().toArray(Integer[]::new);
}

Iterator<Integer> testImmutableCollectionIterator() {
return ImmutableSet.of(1).asList().iterator();
ImmutableSet<Iterator<Integer>> testImmutableCollectionIterator() {
return ImmutableSet.of(
ImmutableSet.of(1).stream().iterator(), ImmutableSet.of(1).asList().iterator());
}

ImmutableSet<Optional<Integer>> testOptionalFirstCollectionElement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ Integer[] testImmutableCollectionToArrayWithGenerator() {
return ImmutableSet.of(1).toArray(Integer[]::new);
}

Iterator<Integer> testImmutableCollectionIterator() {
return ImmutableSet.of(1).iterator();
ImmutableSet<Iterator<Integer>> testImmutableCollectionIterator() {
return ImmutableSet.of(ImmutableSet.of(1).iterator(), ImmutableSet.of(1).iterator());
}

ImmutableSet<Optional<Integer>> testOptionalFirstCollectionElement() {
Expand Down

0 comments on commit beb74e6

Please sign in to comment.