diff --git a/platform/indexing-impl/src/com/intellij/openapi/module/impl/scopes/IndexedSet.java b/platform/indexing-impl/src/com/intellij/openapi/module/impl/scopes/IndexedSet.java deleted file mode 100644 index 288076de8971..000000000000 --- a/platform/indexing-impl/src/com/intellij/openapi/module/impl/scopes/IndexedSet.java +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.intellij.openapi.module.impl.scopes; - -import com.intellij.util.UnmodifiableIterator; -import com.intellij.util.containers.ObjectIntHashMap; - -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.function.Predicate; - -class IndexedSet extends LinkedHashSet { - - private final ObjectIntHashMap myIndex = new ObjectIntHashMap<>(); - - /** - * @return index of element or -1 if element is not in this set - */ - int indexOf(E element) { - return myIndex.get(element); - } - - @Override - public boolean add(E e) { - if (super.add(e)) { - myIndex.put(e, size() - 1); - return true; - } - else { - return false; - } - } - - @Override - public Iterator iterator() { - return new UnmodifiableIterator<>(super.iterator()); - } - - @Override - public boolean remove(Object o) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean removeAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean retainAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean removeIf(Predicate filter) { - throw new UnsupportedOperationException(); - } - - @Override - public Object clone() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - if (!super.equals(o)) return false; - return myIndex.equals(((IndexedSet)o).myIndex); - } - - @Override - public int hashCode() { - return 31 * super.hashCode() + myIndex.hashCode(); - } -} diff --git a/platform/indexing-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java b/platform/indexing-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java index b9928595b936..fe1430ac7ea7 100644 --- a/platform/indexing-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java +++ b/platform/indexing-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java @@ -9,7 +9,7 @@ import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.ObjectIntHashMap; import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -22,7 +22,7 @@ import java.util.*; */ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { private final ProjectFileIndex myIndex; - private final IndexedSet myEntries = new IndexedSet<>(); + private final ObjectIntHashMap myEntries = new ObjectIntHashMap<>(); private int myCachedHashCode; @@ -47,8 +47,8 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { public LibraryRuntimeClasspathScope(@NotNull Project project, @NotNull LibraryOrderEntry entry) { super(project); myIndex = ProjectRootManager.getInstance(project).getFileIndex(); - Collections.addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES)); - Collections.addAll(myEntries, entry.getRootFiles(OrderRootType.SOURCES)); + addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES)); + addAll(myEntries, entry.getRootFiles(OrderRootType.SOURCES)); } public int hashCode() { @@ -74,41 +74,43 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { @NotNull Condition condition) { if (!processedModules.add(module)) return; - ModuleRootManager.getInstance(module).orderEntries().recursively().satisfying(condition).process(new RootPolicy>() { + ModuleRootManager.getInstance(module).orderEntries().recursively().satisfying(condition).process(new RootPolicy>() { @Override - public Set visitLibraryOrderEntry(@NotNull final LibraryOrderEntry libraryOrderEntry, - final Set value) { + public ObjectIntHashMap visitLibraryOrderEntry(@NotNull final LibraryOrderEntry libraryOrderEntry, + final ObjectIntHashMap value) { final Library library = libraryOrderEntry.getLibrary(); if (library != null && processedLibraries.add(library)) { - ContainerUtil.addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES)); - ContainerUtil.addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.SOURCES)); + addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES)); + addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.SOURCES)); } return value; } @Override - public Set visitModuleSourceOrderEntry(@NotNull final ModuleSourceOrderEntry moduleSourceOrderEntry, - final Set value) { + public ObjectIntHashMap visitModuleSourceOrderEntry(@NotNull final ModuleSourceOrderEntry moduleSourceOrderEntry, + final ObjectIntHashMap value) { processedModules.add(moduleSourceOrderEntry.getOwnerModule()); - ContainerUtil.addAll(value, moduleSourceOrderEntry.getRootModel().getSourceRoots()); + addAll(value, moduleSourceOrderEntry.getRootModel().getSourceRoots()); return value; } @Override - public Set visitModuleOrderEntry(@NotNull ModuleOrderEntry moduleOrderEntry, Set value) { + public ObjectIntHashMap visitModuleOrderEntry(@NotNull ModuleOrderEntry moduleOrderEntry, + ObjectIntHashMap value) { final Module depModule = moduleOrderEntry.getModule(); if (depModule != null) { - ContainerUtil.addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots()); + addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots()); } return value; } @Override - public Set visitJdkOrderEntry(@NotNull final JdkOrderEntry jdkOrderEntry, final Set value) { + public ObjectIntHashMap visitJdkOrderEntry(@NotNull final JdkOrderEntry jdkOrderEntry, + final ObjectIntHashMap value) { final Sdk jdk = jdkOrderEntry.getJdk(); if (jdk != null && processedSdk.add(jdk)) { - ContainerUtil.addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES)); - ContainerUtil.addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.SOURCES)); + addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES)); + addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.SOURCES)); } return value; } @@ -136,8 +138,8 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { if (file1.equals(file2)) return 0; final VirtualFile r1 = getFileRoot(file1); final VirtualFile r2 = getFileRoot(file2); - final int i1 = myEntries.indexOf(r1); - final int i2 = myEntries.indexOf(r2); + final int i1 = myEntries.get(r1); + final int i2 = myEntries.get(r2); if (i1 == i2) return 0; if (i1 == -1) return -1; if (i2 == -1) return 1; @@ -147,7 +149,13 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { @TestOnly @NotNull public List getRoots() { - return new ArrayList<>(myEntries); + if (myEntries.isEmpty()) return Collections.emptyList(); + VirtualFile[] result = new VirtualFile[myEntries.size()]; + myEntries.forEachEntry((a, b) -> { + result[b] = a; + return true; + }); + return Arrays.asList(result); } @Override @@ -159,4 +167,12 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { public boolean isSearchInLibraries() { return true; } + + private static void addAll(ObjectIntHashMap entries, VirtualFile[] files) { + for (VirtualFile file : files) { + if (!entries.contains(file)) { + entries.put(file, entries.size()); + } + } + } }