List<Library> (@NotNull List<OrderRoot> chosenRoots, @Nullable LibraryType libraryType, @NotNull LibraryTable.ModifiableModel moduleLibrariesModel, @Nullable Function<? super LibraryType, ? extends LibraryProperties> defaultPropertiesFactory) { List<OrderRoot> roots = filterAlreadyAdded(chosenRoots, moduleLibrariesModel); if (roots.isEmpty()) { return Collections.emptyList(); } Map<VirtualFile, List<OrderRoot>> byFile = roots.stream().collect(Collectors.groupingBy(OrderRoot::getFile, LinkedHashMap::new, Collectors.toList())); Predicate<List<OrderRoot>> containsClasses = it -> it.stream().anyMatch(root -> root.getType().equals(OrderRootType.CLASSES)); //noinspection SSBasedInspection if (byFile.values().stream().allMatch(containsClasses)) { List<Library> addedLibraries = new ArrayList<>(); for (List<OrderRoot> rootsForFile : byFile.values()) { addedLibraries.add(createLibraryFromRoots(rootsForFile, libraryType, moduleLibrariesModel, defaultPropertiesFactory)); } return addedLibraries; } else { return List.of(createLibraryFromRoots(roots, libraryType, moduleLibrariesModel, defaultPropertiesFactory)); } }