[project-import] More strict typing, remove raw types

GitOrigin-RevId: 3530a6ba1d6e96c5af9479bcc2af5876e38f3ea1
This commit is contained in:
Tagir Valeev
2024-09-13 10:49:26 +02:00
committed by intellij-monorepo-bot
parent a4562da4ec
commit 89a6bb2175
8 changed files with 75 additions and 59 deletions

View File

@@ -0,0 +1,12 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ide.util.importProject;
/**
* A dependency (library, module, or file)
*/
sealed interface Dependency permits LibraryDescriptor, FileDescriptor, ModuleDescriptor {
/**
* @return dependency weight to sort them. Lower values correspond to smaller dependencies
*/
int getWeight();
}

View File

@@ -0,0 +1,16 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ide.util.importProject;
import org.jetbrains.annotations.NotNull;
import java.io.File;
/**
* File dependency
*/
record FileDescriptor(@NotNull File file) implements Dependency {
@Override
public int getWeight() {
return 10;
}
}

View File

@@ -3,6 +3,7 @@ package com.intellij.ide.util.importProject;
import com.intellij.ide.JavaUiBundle;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
@@ -14,9 +15,9 @@ import java.util.Set;
/**
* @author Eugene Zhuravlev
*/
public class LibrariesLayoutPanel extends ProjectLayoutPanel<LibraryDescriptor>{
class LibrariesLayoutPanel extends ProjectLayoutPanel<LibraryDescriptor>{
public LibrariesLayoutPanel(final ModuleInsight insight) {
LibrariesLayoutPanel(final ModuleInsight insight) {
super(insight);
}
@@ -37,8 +38,8 @@ public class LibrariesLayoutPanel extends ProjectLayoutPanel<LibraryDescriptor>{
}
@Override
protected Collection getDependencies(final LibraryDescriptor entry) {
return entry.getJars();
protected Collection<? extends Dependency> getDependencies(final LibraryDescriptor entry) {
return ContainerUtil.map(entry.getJars(), FileDescriptor::new);
}
@Override

View File

@@ -24,8 +24,8 @@ import java.util.Collections;
/**
* @author Eugene Zhuravlev
*/
public class LibraryDescriptor {
public final class LibraryDescriptor implements Dependency {
private @NlsSafe String myName;
private final Collection<File> myJars;
@@ -57,4 +57,9 @@ public class LibraryDescriptor {
public String toString() {
return "Lib[" + myName + "]";
}
@Override
public int getWeight() {
return myJars.size() > 1 ? 30 : 40;
}
}

View File

@@ -22,7 +22,7 @@ import java.util.*;
/**
* @author Eugene Zhuravlev
*/
public class ModuleDescriptor {
public non-sealed class ModuleDescriptor implements Dependency {
private String myName;
private final MultiMap<File, DetectedSourceRoot> myContentToSourceRoots = new MultiMap<>();
private final Set<File> myLibraryFiles = new HashSet<>();
@@ -64,10 +64,15 @@ public class ModuleDescriptor {
return myReuseExistingElement;
}
public ModuleType getModuleType() {
public ModuleType<?> getModuleType() {
return myModuleType;
}
@Override
public int getWeight() {
return 20;
}
private static String suggestModuleName(final File contentRoot) {
for (File dir = contentRoot; dir != null; dir = dir.getParentFile()) {
final String suggestion = dir.getName();
@@ -156,7 +161,7 @@ public class ModuleDescriptor {
public String computeModuleFilePath() throws InvalidDataException {
final String name = getName();
final Set<File> contentRoots = getContentRoots();
if (contentRoots.size() > 0) {
if (!contentRoots.isEmpty()) {
return contentRoots.iterator().next().getPath() + File.separator + name + ModuleFileType.DOT_DEFAULT_EXTENSION;
}
else {

View File

@@ -321,7 +321,7 @@ public abstract class ModuleInsight {
final LibraryDescriptor newLibrary = new LibraryDescriptor(newLibraryName, new ArrayList<>(jarsToExtract));
myLibraries.add(newLibrary);
library.removeJars(jarsToExtract);
if (library.getJars().size() == 0) {
if (library.getJars().isEmpty()) {
removeLibrary(library);
}
return newLibrary;
@@ -336,7 +336,7 @@ public abstract class ModuleInsight {
newModule = createModuleDescriptor(root, sources != null ? sources : new HashSet<>());
}
else {
if (sources != null && sources.size() > 0) {
if (sources != null && !sources.isEmpty()) {
for (DetectedSourceRoot source : sources) {
newModule.addSourceRoot(root, source);
}
@@ -377,7 +377,7 @@ public abstract class ModuleInsight {
to.addJars(files);
from.removeJars(files);
// remove the library if it became empty
if (from.getJars().size() == 0) {
if (from.getJars().isEmpty()) {
removeLibrary(from);
}
}
@@ -424,11 +424,7 @@ public abstract class ModuleInsight {
final Map<File, LibraryDescriptor> rootToLibraryMap = new HashMap<>();
for (File jar : jars) {
final File parent = jar.getParentFile();
LibraryDescriptor lib = rootToLibraryMap.get(parent);
if (lib == null) {
lib = new LibraryDescriptor(parent.getName(), new HashSet<>());
rootToLibraryMap.put(parent, lib);
}
LibraryDescriptor lib = rootToLibraryMap.computeIfAbsent(parent, p -> new LibraryDescriptor(p.getName(), new HashSet<>()));
lib.addJars(Collections.singleton(jar));
}
return new ArrayList<>(rootToLibraryMap.values());

View File

@@ -12,13 +12,13 @@ import java.util.*;
/**
* @author Eugene Zhuravlev
*/
public class ModulesLayoutPanel extends ProjectLayoutPanel<ModuleDescriptor>{
class ModulesLayoutPanel extends ProjectLayoutPanel<ModuleDescriptor>{
private final LibraryFilter myLibrariesFilter;
public interface LibraryFilter {
boolean isLibraryChosen(LibraryDescriptor libDescriptor);
}
public ModulesLayoutPanel(ModuleInsight insight, final LibraryFilter libFilter) {
ModulesLayoutPanel(ModuleInsight insight, final LibraryFilter libFilter) {
super(insight);
myLibrariesFilter = libFilter;
}
@@ -40,8 +40,8 @@ public class ModulesLayoutPanel extends ProjectLayoutPanel<ModuleDescriptor>{
}
@Override
protected Collection getDependencies(final ModuleDescriptor entry) {
final List<Object> deps = new ArrayList<>(entry.getDependencies());
protected Collection<Dependency> getDependencies(final ModuleDescriptor entry) {
final List<Dependency> deps = new ArrayList<>(entry.getDependencies());
final Collection<LibraryDescriptor> libDependencies = getInsight().getLibraryDependencies(entry);
for (LibraryDescriptor libDependency : libDependencies) {
if (myLibrariesFilter.isLibraryChosen(libDependency)) {

View File

@@ -41,20 +41,15 @@ import java.util.*;
/**
* @author Eugene Zhuravlev
*/
abstract class ProjectLayoutPanel<T> extends JPanel {
abstract class ProjectLayoutPanel<T extends Dependency> extends JPanel {
private final ElementsChooser<T> myEntriesChooser;
private final JList myDependenciesList;
private final JList<Dependency> myDependenciesList;
private final ModuleInsight myInsight;
private final Comparator<T> COMPARATOR = (o1, o2) -> {
final int w1 = getWeight(o1);
final int w2 = getWeight(o2);
if (w1 != w2) {
return w1 - w2;
}
return getElementText(o1).compareToIgnoreCase(getElementText(o2));
};
private final Comparator<Dependency> COMPARATOR = Comparator
.comparingInt(Dependency::getWeight)
.thenComparing(dependency -> getElementText(dependency), String.CASE_INSENSITIVE_ORDER);
ProjectLayoutPanel(final ModuleInsight insight) {
super(new BorderLayout());
@@ -102,11 +97,13 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
return;
}
final List<T> entries = getSelectedEntries();
final Collection deps = getDependencies(entries);
final Collection<Dependency> deps = getDependencies(entries);
final DefaultListModel depsModel = (DefaultListModel)myDependenciesList.getModel();
final DefaultListModel<Dependency> depsModel = (DefaultListModel<Dependency>)myDependenciesList.getModel();
depsModel.clear();
for (Object dep : alphaSortList(new ArrayList(deps))) {
ArrayList<Dependency> depsList = new ArrayList<>(deps);
depsList.sort(COMPARATOR);
for (Dependency dep : depsList) {
depsModel.addElement(dep);
}
}
@@ -135,15 +132,15 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
return myInsight;
}
private JList createList() {
final JList list = new JBList(new DefaultListModel());
private JList<Dependency> createList() {
final JList<Dependency> list = new JBList<>(new DefaultListModel<>());
list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setCellRenderer(new MyListCellRenderer());
return list;
}
public final Collection getDependencies(final List<? extends T> entries) {
final Set deps = new HashSet();
public final Collection<Dependency> getDependencies(final List<? extends T> entries) {
final Set<Dependency> deps = new HashSet<>();
for (T et : entries) {
deps.addAll(getDependencies(et));
}
@@ -162,7 +159,9 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
public void rebuild() {
myEntriesChooser.clear();
for (final T entry : alphaSortList(getEntries())) {
List<T> entries = getEntries();
entries.sort(COMPARATOR);
for (final T entry : entries) {
myEntriesChooser.addElement(entry, true, new EntryProperties(entry));
}
if (myEntriesChooser.getElementCount() > 0) {
@@ -170,11 +169,6 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
}
}
private List<T> alphaSortList(final List<T> entries) {
entries.sort(COMPARATOR);
return entries;
}
@Nullable
protected Icon getElementIcon(Object element) {
if (element instanceof ModuleDescriptor) {
@@ -189,19 +183,6 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
return null;
}
protected int getWeight(Object element) {
if (element instanceof File) {
return 10;
}
if (element instanceof ModuleDescriptor) {
return 20;
}
if (element instanceof LibraryDescriptor) {
return ((LibraryDescriptor)element).getJars().size() > 1? 30 : 40;
}
return Integer.MAX_VALUE;
}
protected static @NlsSafe String getElementText(Object element) {
if (element instanceof LibraryDescriptor) {
return getElementTextFromLibraryDescriptor((LibraryDescriptor)element);
@@ -226,7 +207,7 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
}
final Collection<? extends DetectedProjectRoot> sourceRoots = moduleDescriptor.getSourceRoots();
if (sourceRoots.size() > 0) {
if (!sourceRoots.isEmpty()) {
StringJoiner joiner = new StringJoiner(",", " [", "]");
for (DetectedProjectRoot root : sourceRoots) {
joiner.add(root.getDirectory().getName());
@@ -259,7 +240,7 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
protected abstract List<T> getEntries();
protected abstract Collection getDependencies(T entry);
protected abstract Collection<? extends Dependency> getDependencies(T entry);
@Nullable
protected abstract T merge(List<? extends T> entries);