Skip to content

Commit

Permalink
Introduce CollectionIterator Refaster rule (#1347)
Browse files Browse the repository at this point in the history
This rule supersedes the more specific `ImmutableCollectionIterator` rule.
  • Loading branch information
mohamedsamehsalah authored Oct 1, 2024
1 parent 4d1eeb2 commit beb96f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,20 @@ 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.
*/
static final class ImmutableCollectionIterator<T> {
/** Prefer {@link Collection#iterator()} over more contrived or less efficient alternatives. */
static final class CollectionIterator<T> {
@BeforeTemplate
Iterator<T> before(Collection<T> collection) {
return collection.stream().iterator();
}

@BeforeTemplate
Iterator<T> before(ImmutableCollection<T> collection) {
return collection.asList().iterator();
}

@AfterTemplate
Iterator<T> after(ImmutableCollection<T> collection) {
Iterator<T> after(Collection<T> collection) {
return collection.iterator();
}
}
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>> testCollectionIterator() {
return ImmutableSet.of(
ImmutableSet.of(1).stream().iterator(), ImmutableSet.of(2).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>> testCollectionIterator() {
return ImmutableSet.of(ImmutableSet.of(1).iterator(), ImmutableSet.of(2).iterator());
}

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

0 comments on commit beb96f0

Please sign in to comment.