update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / PackagePrefixIndex.java
blobd109e8163aa6ecb06529a5e72dcda2698a5c9df3
1 /*
2 * Copyright 2000-2009 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.psi.impl;
18 import com.intellij.ProjectTopics;
19 import com.intellij.openapi.module.Module;
20 import com.intellij.openapi.module.ModuleManager;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.roots.*;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.psi.search.GlobalSearchScope;
25 import com.intellij.util.SmartList;
26 import com.intellij.util.containers.MultiMap;
27 import org.jetbrains.annotations.Nullable;
29 import java.util.Collection;
30 import java.util.List;
32 /**
33 * @author peter
35 public class PackagePrefixIndex {
36 private static final Object LOCK = new Object();
37 private MultiMap<String, Module> myMap;
38 private final Project myProject;
40 public PackagePrefixIndex(Project project) {
41 myProject = project;
42 project.getMessageBus().connect(project).subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {
43 public void beforeRootsChange(final ModuleRootEvent event) {
46 public void rootsChanged(final ModuleRootEvent event) {
47 synchronized (LOCK) {
48 myMap = null;
51 });
54 public Collection<String> getAllPackagePrefixes(@Nullable GlobalSearchScope scope) {
55 MultiMap<String, Module> map = myMap;
56 if (map != null) {
57 return getAllPackagePrefixes(scope, map);
60 map = new MultiMap<String, Module>();
61 for (final Module module : ModuleManager.getInstance(myProject).getModules()) {
62 for (final ContentEntry entry : ModuleRootManager.getInstance(module).getContentEntries()) {
63 for (final SourceFolder folder : entry.getSourceFolders()) {
64 final String prefix = folder.getPackagePrefix();
65 if (StringUtil.isNotEmpty(prefix)) {
66 map.putValue(prefix, module);
72 synchronized (LOCK) {
73 if (myMap == null) {
74 myMap = map;
76 return getAllPackagePrefixes(scope, myMap);
80 private static Collection<String> getAllPackagePrefixes(final GlobalSearchScope scope, final MultiMap<String, Module> map) {
81 if (scope == null) return map.keySet();
83 List<String> result = new SmartList<String>();
84 for (final String prefix : map.keySet()) {
85 modules: for (final Module module : map.get(prefix)) {
86 if (scope.isSearchInModuleContent(module)) {
87 result.add(prefix);
88 break modules;
92 return result;