001 /*
002 * Copyright 2010-2014 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.lazy.declarations;
018
019 import kotlin.Function0;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.jet.lang.psi.JetDeclaration;
022 import org.jetbrains.jet.lang.psi.JetFile;
023 import org.jetbrains.jet.lang.resolve.name.FqName;
024 import org.jetbrains.jet.storage.NotNullLazyValue;
025 import org.jetbrains.jet.storage.StorageManager;
026
027 import java.util.Collection;
028
029 public class FileBasedPackageMemberDeclarationProvider extends AbstractPsiBasedDeclarationProvider implements PackageMemberDeclarationProvider {
030
031 private final FqName fqName;
032 private final FileBasedDeclarationProviderFactory factory;
033 private final Collection<JetFile> packageFiles;
034 private final NotNullLazyValue<Collection<FqName>> allDeclaredSubPackages;
035
036
037 /*package*/ FileBasedPackageMemberDeclarationProvider(
038 @NotNull StorageManager storageManager,
039 @NotNull FqName _fqName,
040 @NotNull FileBasedDeclarationProviderFactory _factory,
041 @NotNull Collection<JetFile> packageFiles
042 ) {
043 super(storageManager);
044 this.fqName = _fqName;
045 this.factory = _factory;
046 this.packageFiles = packageFiles;
047 this.allDeclaredSubPackages = storageManager.createLazyValue(new Function0<Collection<FqName>>() {
048 @Override
049 public Collection<FqName> invoke() {
050 return factory.getAllDeclaredSubPackagesOf(fqName);
051 }
052 });
053 }
054
055 @Override
056 protected void doCreateIndex(@NotNull Index index) {
057 for (JetFile file : packageFiles) {
058 for (JetDeclaration declaration : file.getDeclarations()) {
059 assert fqName.equals(file.getPackageFqName()) : "Files declaration utils contains file with invalid package";
060 index.putToIndex(declaration);
061 }
062 }
063 }
064
065 @NotNull
066 @Override
067 public Collection<FqName> getAllDeclaredSubPackages() {
068 return allDeclaredSubPackages.invoke();
069 }
070
071 @NotNull
072 @Override
073 public Collection<JetFile> getPackageFiles() {
074 return packageFiles;
075 }
076
077 @Override
078 public String toString() {
079 return "Declarations for package " + fqName;
080 }
081 }