Skip to content

Commit

Permalink
Use a for loop as is common elsewhere to limit object creations
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed May 18, 2024
1 parent c7f9f98 commit 01378b0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ public List<JRightPadded<J.Import>> addImport(List<JRightPadded<J.Import>> origi

// Do not add the import if it is already present.
String qualifiedName = paddedToAdd.getElement().getQualid().toString();
if (originalImports.stream().anyMatch(i -> qualifiedName.equals(i.getElement().getQualid().toString()))) {
return originalImports;
for (JRightPadded<J.Import> originalImport : originalImports) {
if (qualifiedName.equals(originalImport.getElement().getQualid().toString())) {
return originalImports;
}
}

// don't star fold just yet, because we are only going to star fold adjacent imports along with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.config.DeclarativeNamedStyles;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JLeftPadded;
Expand Down Expand Up @@ -89,7 +90,8 @@ void deserializeInDeclarativeNamedStyles() throws IOException {
}

@Test
void testAddImport() {
@Issue("https://github.com/openrewrite/rewrite/issues/4196")
void addImportInPresenceOfDuplicateOtherImport() {
ImportLayoutStyle style = new ImportLayoutStyle(
Integer.MAX_VALUE, Integer.MAX_VALUE, Collections.emptyList(), Collections.emptyList());
JRightPadded<J.Import> import1 = new JRightPadded<>(
Expand Down

0 comments on commit 01378b0

Please sign in to comment.