diff --git a/src/com/facebook/buck/ide/intellij/IjProjectTemplateDataPreparer.java b/src/com/facebook/buck/ide/intellij/IjProjectTemplateDataPreparer.java index 08bb0f9e5bd..a9e5f53e7f2 100644 --- a/src/com/facebook/buck/ide/intellij/IjProjectTemplateDataPreparer.java +++ b/src/com/facebook/buck/ide/intellij/IjProjectTemplateDataPreparer.java @@ -38,6 +38,8 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableCollection; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; @@ -181,23 +183,24 @@ public ImmutableSet getLibrariesToBeWritten() { private ContentRoot createContentRoot( final IjModule module, Path contentRootPath, - ImmutableSet folders, + ImmutableCollection folders, final Path moduleLocationBasePath) { String url = IjProjectPaths.toModuleDirRelativeString(contentRootPath, moduleLocationBasePath); - ImmutableSet simplifiedFolders = + ImmutableCollection simplifiedFolders = sourceRootSimplifier.simplify(contentRootPath.getNameCount(), folders); IjFolderToIjSourceFolderTransform transformToFolder = new IjFolderToIjSourceFolderTransform(module); - ImmutableSortedSet sourceFolders = + ImmutableList sourceFolders = simplifiedFolders .stream() .map(transformToFolder::apply) - .collect(MoreCollectors.toImmutableSortedSet(Ordering.natural())); + .sorted() + .collect(MoreCollectors.toImmutableList()); return ContentRoot.builder().setUrl(url).setFolders(sourceFolders).build(); } - public ImmutableSet createExcludes(final IjModule module) throws IOException { - final ImmutableSet.Builder excludesBuilder = ImmutableSet.builder(); + public ImmutableCollection createExcludes(final IjModule module) throws IOException { + final ImmutableList.Builder excludesBuilder = ImmutableList.builder(); final Path moduleBasePath = module.getModuleBasePath(); projectFilesystem.walkRelativeFileTree( moduleBasePath, @@ -261,9 +264,10 @@ public ContentRoot getContentRoot(IjModule module) throws IOException { Path moduleLocation = module.getModuleImlFilePath(); final Path moduleLocationBasePath = (moduleLocation.getParent() == null) ? Paths.get("") : moduleLocation.getParent(); - ImmutableSet sourcesAndExcludes = + ImmutableList sourcesAndExcludes = Stream.concat(module.getFolders().stream(), createExcludes(module).stream()) - .collect(MoreCollectors.toImmutableSortedSet()); + .sorted() + .collect(MoreCollectors.toImmutableList()); return createContentRoot(module, moduleBasePath, sourcesAndExcludes, moduleLocationBasePath); } diff --git a/src/com/facebook/buck/ide/intellij/IjSourceRootSimplifier.java b/src/com/facebook/buck/ide/intellij/IjSourceRootSimplifier.java index f001da1d27c..5c2290fb5ad 100644 --- a/src/com/facebook/buck/ide/intellij/IjSourceRootSimplifier.java +++ b/src/com/facebook/buck/ide/intellij/IjSourceRootSimplifier.java @@ -27,8 +27,8 @@ import com.facebook.buck.jvm.core.JavaPackageFinder; import com.facebook.buck.util.MoreCollectors; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; import java.nio.file.Path; import java.util.Collection; import java.util.HashMap; @@ -57,7 +57,8 @@ public IjSourceRootSimplifier(JavaPackageFinder javaPackageFinder) { * @param folders set of {@link IjFolder}s to simplify. * @return simplified set of {@link IjFolder}s. */ - public ImmutableSet simplify(int simplificationLimit, ImmutableSet folders) { + public ImmutableCollection simplify( + int simplificationLimit, Iterable folders) { PackagePathCache packagePathCache = new PackagePathCache(folders, javaPackageFinder); BottomUpPathMerger walker = new BottomUpPathMerger(folders, simplificationLimit, packagePathCache); @@ -102,11 +103,11 @@ public BottomUpPathMerger( } } - public ImmutableSet getMergedFolders() { + private ImmutableCollection getMergedFolders() { for (Path topLevel : tree.getNodesWithNoIncomingEdges()) { walk(topLevel); } - return ImmutableSet.copyOf(mergePathsMap.values()); + return ImmutableList.copyOf(mergePathsMap.values()); } /** @@ -126,12 +127,12 @@ private Optional walk(Path currentPath) { .map(this::walk) .collect(MoreCollectors.toImmutableList()); - ImmutableSet presentChildren = + ImmutableList presentChildren = children .stream() .filter(Optional::isPresent) .map(Optional::get) - .collect(MoreCollectors.toImmutableSet()); + .collect(MoreCollectors.toImmutableList()); IjFolder currentFolder = mergePathsMap.get(currentPath); if (presentChildren.isEmpty()) { @@ -148,7 +149,7 @@ private Optional walk(Path currentPath) { private Optional tryMergingParentAndChildren( Path currentPath, @Nullable IjFolder parentFolder, - ImmutableSet children, + ImmutableCollection children, boolean hasNonPresentChildren) { if (parentFolder == null) { return mergeChildrenIntoNewParentFolder(currentPath, children); @@ -179,7 +180,7 @@ private Optional tryMergingParentAndChildren( * *

The best type in this algorithm is the type with the maximum number of children. */ - private FolderTypeWithPackageInfo findBestFolderType(ImmutableSet children) { + private FolderTypeWithPackageInfo findBestFolderType(ImmutableCollection children) { if (children.size() == 1) { return FolderTypeWithPackageInfo.fromFolder(children.iterator().next()); } @@ -210,14 +211,14 @@ private FolderTypeWithPackageInfo findBestFolderType(ImmutableSet chil *

The type of the result folder depends on the children. */ private Optional mergeChildrenIntoNewParentFolder( - Path currentPath, ImmutableSet children) { - ImmutableSet childrenToMerge = + Path currentPath, ImmutableCollection children) { + ImmutableList childrenToMerge = children .stream() .filter( child -> SourceFolder.class.isInstance(child) || TestFolder.class.isInstance(child)) - .collect(MoreCollectors.toImmutableSet()); + .collect(MoreCollectors.toImmutableList()); if (childrenToMerge.isEmpty()) { return Optional.empty(); @@ -238,14 +239,14 @@ private Optional mergeChildrenIntoNewParentFolder( private Optional tryCreateNewParentFolderFromChildrenWithoutPackages( FolderTypeWithPackageInfo typeForMerging, Path currentPath, - ImmutableSet children) { + ImmutableCollection children) { Class folderClass = typeForMerging.getFolderTypeClass(); - ImmutableSet childrenToMerge = + ImmutableList childrenToMerge = children .stream() .filter(folderClass::isInstance) .filter(folder -> !folder.getWantsPackagePrefix()) - .collect(MoreCollectors.toImmutableSet()); + .collect(MoreCollectors.toImmutableList()); if (childrenToMerge.isEmpty()) { return Optional.empty(); @@ -272,14 +273,14 @@ private Optional tryCreateNewParentFolderFromChildrenWithoutPackages( private Optional tryCreateNewParentFolderFromChildrenWithPackage( FolderTypeWithPackageInfo typeForMerging, Path currentPath, - ImmutableSet children) { + ImmutableCollection children) { Optional currentPackage = packagePathCache.lookup(currentPath); if (!currentPackage.isPresent()) { return Optional.empty(); } Class folderClass = typeForMerging.getFolderTypeClass(); - ImmutableSet childrenToMerge = + ImmutableList childrenToMerge = children .stream() .filter(folderClass::isInstance) @@ -288,7 +289,7 @@ private Optional tryCreateNewParentFolderFromChildrenWithPackage( child -> canMergeWithKeepingPackage( currentPath, currentPackage.get(), child, packagePathCache)) - .collect(MoreCollectors.toImmutableSet()); + .collect(MoreCollectors.toImmutableList()); if (childrenToMerge.isEmpty()) { return Optional.empty(); @@ -330,13 +331,13 @@ private Optional tryCreateNewParentFolderFromChildrenWithPackage( * */ private Optional mergeFoldersWithMatchingPackageIntoParent( - IjFolder parentFolder, ImmutableSet children) { + IjFolder parentFolder, ImmutableCollection children) { - ImmutableSet childrenToMerge = + ImmutableList childrenToMerge = children .stream() .filter(child -> canMergeWithKeepingPackage(parentFolder, child, packagePathCache)) - .collect(MoreCollectors.toImmutableSet()); + .collect(MoreCollectors.toImmutableList()); IjFolder result = mergeFolders(parentFolder, childrenToMerge); @@ -348,12 +349,12 @@ private Optional mergeFoldersWithMatchingPackageIntoParent( /** Merges children that can be merged into a parent. */ private Optional mergeAndRemoveSimilarChildren( - IjFolder parentFolder, ImmutableSet children) { - ImmutableSet childrenToMerge = + IjFolder parentFolder, ImmutableCollection children) { + ImmutableList childrenToMerge = children .stream() .filter(folder -> folder.canMergeWith(parentFolder)) - .collect(MoreCollectors.toImmutableSet()); + .collect(MoreCollectors.toImmutableList()); IjFolder result = mergeFolders(parentFolder, childrenToMerge); @@ -434,7 +435,7 @@ private static class PackagePathCache { JavaPackagePathCache delegate; public PackagePathCache( - ImmutableSet startingFolders, JavaPackageFinder javaPackageFinder) { + Iterable startingFolders, JavaPackageFinder javaPackageFinder) { delegate = new JavaPackagePathCache(); for (IjFolder startingFolder : startingFolders) { if (!startingFolder.getWantsPackagePrefix()) { diff --git a/src/com/facebook/buck/ide/intellij/ModuleBuildContext.java b/src/com/facebook/buck/ide/intellij/ModuleBuildContext.java index c9ff0cc526f..60968164e00 100644 --- a/src/com/facebook/buck/ide/intellij/ModuleBuildContext.java +++ b/src/com/facebook/buck/ide/intellij/ModuleBuildContext.java @@ -27,6 +27,8 @@ import com.facebook.buck.rules.TargetNode; import com.google.common.base.Preconditions; import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableCollection; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; @@ -42,7 +44,7 @@ public class ModuleBuildContext { private Optional androidFacetBuilder; private ImmutableSet.Builder extraClassPathDependenciesBuilder; - private ImmutableSet.Builder generatedSourceCodeFoldersBuilder; + private Map generatedSourceCodeFoldersMap = new HashMap<>(); private Map sourceFoldersMergeMap; // See comment in getDependencies for these two member variables. private Map dependencyTypeMap; @@ -55,7 +57,6 @@ public ModuleBuildContext(ImmutableSet circularDependencyInducingTa this.circularDependencyInducingTargets = circularDependencyInducingTargets; this.androidFacetBuilder = Optional.empty(); this.extraClassPathDependenciesBuilder = new ImmutableSet.Builder<>(); - this.generatedSourceCodeFoldersBuilder = ImmutableSet.builder(); this.sourceFoldersMergeMap = new HashMap<>(); this.dependencyTypeMap = new HashMap<>(); this.dependencyOriginMap = HashMultimap.create(); @@ -83,8 +84,8 @@ public Optional getAndroidFacet() { return androidFacetBuilder.map(IjModuleAndroidFacet.Builder::build); } - public ImmutableSet getSourceFolders() { - return ImmutableSet.copyOf(sourceFoldersMergeMap.values()); + public ImmutableCollection getSourceFolders() { + return ImmutableList.copyOf(sourceFoldersMergeMap.values()); } public void addExtraClassPathDependency(Path path) { @@ -96,11 +97,12 @@ public ImmutableSet getExtraClassPathDependencies() { } public void addGeneratedSourceCodeFolder(IjFolder generatedFolder) { - generatedSourceCodeFoldersBuilder.add(generatedFolder); + Preconditions.checkState( + generatedSourceCodeFoldersMap.put(generatedFolder.getPath(), generatedFolder) == null); } - public ImmutableSet getGeneratedSourceCodeFolders() { - return generatedSourceCodeFoldersBuilder.build(); + public ImmutableCollection getGeneratedSourceCodeFolders() { + return ImmutableList.copyOf(generatedSourceCodeFoldersMap.values()); } public IjModuleType getModuleType() { diff --git a/src/com/facebook/buck/ide/intellij/model/AbstractContentRoot.java b/src/com/facebook/buck/ide/intellij/model/AbstractContentRoot.java index 4bafb383873..3206f3a4a53 100644 --- a/src/com/facebook/buck/ide/intellij/model/AbstractContentRoot.java +++ b/src/com/facebook/buck/ide/intellij/model/AbstractContentRoot.java @@ -18,7 +18,7 @@ import com.facebook.buck.ide.intellij.model.folders.IjSourceFolder; import com.facebook.buck.util.immutables.BuckStyleImmutable; -import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.ImmutableList; import org.immutables.value.Value; @Value.Immutable @@ -26,7 +26,7 @@ abstract class AbstractContentRoot implements Comparable { public abstract String getUrl(); - public abstract ImmutableSortedSet getFolders(); + public abstract ImmutableList getFolders(); @Override public int compareTo(ContentRoot o) { diff --git a/src/com/facebook/buck/ide/intellij/model/AbstractIjModule.java b/src/com/facebook/buck/ide/intellij/model/AbstractIjModule.java index 1eeff950a1a..8f153e3efbf 100644 --- a/src/com/facebook/buck/ide/intellij/model/AbstractIjModule.java +++ b/src/com/facebook/buck/ide/intellij/model/AbstractIjModule.java @@ -23,6 +23,7 @@ import com.facebook.buck.model.BuildTarget; import com.facebook.buck.util.immutables.BuckStyleImmutable; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.nio.file.Path; @@ -51,7 +52,7 @@ public String getName() { public abstract Path getModuleBasePath(); /** @return paths to various directories the module is responsible for. */ - public abstract ImmutableSet getFolders(); + public abstract ImmutableList getFolders(); /** * @return map of {@link BuildTarget}s the module depends on and information on whether it's a @@ -65,7 +66,7 @@ public String getName() { public abstract ImmutableSet getExtraClassPathDependencies(); /** @return Folders which contain the generated source code. */ - public abstract ImmutableSet getGeneratedSourceCodeFolders(); + public abstract ImmutableList getGeneratedSourceCodeFolders(); public abstract Optional getLanguageLevel(); diff --git a/src/com/facebook/buck/ide/intellij/model/folders/IjFolder.java b/src/com/facebook/buck/ide/intellij/model/folders/IjFolder.java index c2dbf399e2f..c4ddac9e40a 100644 --- a/src/com/facebook/buck/ide/intellij/model/folders/IjFolder.java +++ b/src/com/facebook/buck/ide/intellij/model/folders/IjFolder.java @@ -27,13 +27,11 @@ public abstract class IjFolder implements Comparable { private final Path path; private final ImmutableSortedSet inputs; private final boolean wantsPackagePrefix; - private final int inputsHash; IjFolder(Path path, boolean wantsPackagePrefix, ImmutableSortedSet inputs) { this.path = path; this.wantsPackagePrefix = wantsPackagePrefix; this.inputs = (inputs == null) ? EMPTY_INPUTS : inputs; - this.inputsHash = inputs.hashCode(); } IjFolder(Path path, boolean wantsPackagePrefix) { @@ -124,7 +122,9 @@ public boolean equals(Object other) { @Override public int hashCode() { - return (getPath().hashCode() << 31) | (getWantsPackagePrefix() ? 0x8000 : 0) | inputsHash; + return (getPath().hashCode() << 31) + | (getWantsPackagePrefix() ? 0x8000 : 0) + | inputs.hashCode(); } @Override diff --git a/test/com/facebook/buck/ide/intellij/DefaultIjModuleFactoryTest.java b/test/com/facebook/buck/ide/intellij/DefaultIjModuleFactoryTest.java index 6135a1e443a..aa3825dd513 100644 --- a/test/com/facebook/buck/ide/intellij/DefaultIjModuleFactoryTest.java +++ b/test/com/facebook/buck/ide/intellij/DefaultIjModuleFactoryTest.java @@ -62,6 +62,7 @@ import com.facebook.buck.rules.TargetNode; import com.facebook.buck.testutil.FakeProjectFilesystem; import com.facebook.buck.util.MoreCollectors; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; @@ -415,7 +416,7 @@ public void testJavaLibraryInRoot() { assertEquals(Paths.get(""), folder.getPath()); } - private ImmutableSet getFolderPaths(ImmutableSet folders) { + private ImmutableSet getFolderPaths(ImmutableCollection folders) { return folders.stream().map(IjFolder::getPath).collect(MoreCollectors.toImmutableSet()); } diff --git a/test/com/facebook/buck/ide/intellij/IjProjectDataPreparerTest.java b/test/com/facebook/buck/ide/intellij/IjProjectDataPreparerTest.java index 07c58726371..e7b9196c284 100644 --- a/test/com/facebook/buck/ide/intellij/IjProjectDataPreparerTest.java +++ b/test/com/facebook/buck/ide/intellij/IjProjectDataPreparerTest.java @@ -48,6 +48,7 @@ import com.google.common.base.Functions; import com.google.common.base.Preconditions; import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; @@ -96,7 +97,7 @@ public void testWriteModule() throws Exception { ContentRoot contentRoot = dataPreparer.getContentRoot(baseModule); assertEquals("file://$MODULE_DIR$", contentRoot.getUrl()); - IjSourceFolder baseSourceFolder = contentRoot.getFolders().first(); + IjSourceFolder baseSourceFolder = contentRoot.getFolders().iterator().next(); assertEquals("sourceFolder", baseSourceFolder.getType()); assertFalse(baseSourceFolder.getIsTestSource()); assertEquals("com.example.base", baseSourceFolder.getPackagePrefix()); @@ -433,7 +434,7 @@ public void testCreatePackageLookupPahtSet() { containsInAnyOrder(subSourcePath, sourcePath)); } - private static ImmutableSet distillExcludeFolders(ImmutableSet folders) { + private static ImmutableSet distillExcludeFolders(ImmutableCollection folders) { Preconditions.checkArgument( !FluentIterable.from(folders).anyMatch(input -> !(input instanceof ExcludeFolder))); return FluentIterable.from(folders).uniqueIndex(IjFolder::getPath).keySet(); diff --git a/test/com/facebook/buck/ide/intellij/IjSourceRootSimplifierTest.java b/test/com/facebook/buck/ide/intellij/IjSourceRootSimplifierTest.java index aa70cdb313b..8acc1bb320d 100644 --- a/test/com/facebook/buck/ide/intellij/IjSourceRootSimplifierTest.java +++ b/test/com/facebook/buck/ide/intellij/IjSourceRootSimplifierTest.java @@ -27,6 +27,7 @@ import com.facebook.buck.jvm.java.DefaultJavaPackageFinder; import com.facebook.buck.model.BuildTarget; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.nio.file.Path; @@ -187,7 +188,7 @@ public void testDifferentTypeAreNotMergedWhileSameOnesAre() { IjFolder acTest = buildTestFolder("a/c"); IjFolder adaTest = buildTestFolder("a/d/a"); - ImmutableSet mergedFolders = + ImmutableCollection mergedFolders = simplifier.simplify( 0, ImmutableSet.of(aaaSource, aaaaSource, aabSource, abSource, acTest, adaTest)); @@ -203,7 +204,7 @@ public void testMergingIntoBiggerNumberOfSourceFolders() { IjFolder abSource = buildSourceFolder("a/b"); IjFolder acTest = buildTestFolder("a/c"); - ImmutableSet mergedFolders = + ImmutableCollection mergedFolders = simplifier.simplify(0, ImmutableSet.of(aaSource, abSource, acTest)); IjFolder aSource = buildSourceFolder("a"); @@ -219,7 +220,7 @@ public void testMergingIntoBiggerNumberOfTestFolders() { IjFolder adTest = buildTestFolder("a/d"); IjFolder aeTest = buildTestFolder("a/e"); - ImmutableSet mergedFolders = + ImmutableCollection mergedFolders = simplifier.simplify(0, ImmutableSet.of(aaSource, abSource, acTest, adTest, aeTest)); IjFolder aTest = buildTestFolder("a"); @@ -237,7 +238,7 @@ public void testDifferentTypesAreNotMergedIntoParent() { IjFolder acTest = buildTestFolder("a/c"); IjFolder adaTest = buildTestFolder("a/d/a"); - ImmutableSet mergedFolders = + ImmutableCollection mergedFolders = simplifier.simplify( 0, ImmutableSet.of(aSource, aaaSource, aaaaSource, aabSource, abSource, acTest, adaTest));