2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.indexing;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.openapi.vfs.VirtualFileManager;
22 import com.intellij.util.NotNullFunction;
23 import com.intellij.util.containers.ContainerUtil;
24 import org.jetbrains.annotations.NotNull;
26 import java.util.Collections;
27 import java.util.HashSet;
33 public abstract class IndexableSetContributor implements IndexedRootsProvider {
35 protected static final Set<VirtualFile> EMPTY_FILE_SET = Collections.emptySet();
36 private static final Logger LOG = Logger.getInstance(IndexableSetContributor.class);
39 public final Set<String> getRootsToIndex() {
40 return ContainerUtil.map2Set(getAdditionalRootsToIndex(), new NotNullFunction<VirtualFile, String>() {
43 public String fun(VirtualFile virtualFile) {
44 return virtualFile.getUrl();
50 public static Set<VirtualFile> getProjectRootsToIndex(IndexedRootsProvider provider, Project project) {
51 if (provider instanceof IndexableSetContributor) {
52 IndexableSetContributor contributor = (IndexableSetContributor)provider;
53 Set<VirtualFile> roots = contributor.getAdditionalProjectRootsToIndex(project);
54 return filterOutNulls(contributor, "getAdditionalProjectRootsToIndex(Project)", roots);
56 return EMPTY_FILE_SET;
60 public static Set<VirtualFile> getRootsToIndex(IndexedRootsProvider provider) {
61 if (provider instanceof IndexableSetContributor) {
62 IndexableSetContributor contributor = (IndexableSetContributor)provider;
63 Set<VirtualFile> roots = contributor.getAdditionalRootsToIndex();
64 return filterOutNulls(contributor, "getAdditionalRootsToIndex()", roots);
67 final HashSet<VirtualFile> result = new HashSet<VirtualFile>();
68 for (String url : provider.getRootsToIndex()) {
69 ContainerUtil.addIfNotNull(VirtualFileManager.getInstance().findFileByUrl(url), result);
76 * @return an additional project-dependent set of {@link VirtualFile} instances to index,
77 * the returned set should not contain nulls
80 public Set<VirtualFile> getAdditionalProjectRootsToIndex(@NotNull Project project) {
81 return EMPTY_FILE_SET;
85 * @return an additional project-independent set of {@link VirtualFile} instances to index,
86 * the returned set should not contain nulls
89 public abstract Set<VirtualFile> getAdditionalRootsToIndex();
92 private static Set<VirtualFile> filterOutNulls(@NotNull IndexableSetContributor contributor,
93 @NotNull String methodInfo,
94 @NotNull Set<VirtualFile> roots) {
95 for (VirtualFile root : roots) {
97 LOG.error("Please fix " + contributor.getClass().getName() + "#" + methodInfo + ".\n" +
98 "The returned set is not expected to contain nulls, but it is " + roots);
99 Set<VirtualFile> result = ContainerUtil.newHashSet(roots.size());
100 ContainerUtil.addAllNotNull(result, roots);