001/*
002 * Copyright 2010-2013 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
017package org.jetbrains.jet.lang.resolve.lazy.declarations;
018
019import com.intellij.openapi.util.Computable;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.jet.lang.psi.*;
022import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue;
023import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
024import org.jetbrains.jet.lang.resolve.name.FqName;
025import org.jetbrains.jet.lang.resolve.name.Name;
026
027import java.util.Collection;
028
029public class FileBasedPackageMemberDeclarationProvider extends AbstractPsiBasedDeclarationProvider implements PackageMemberDeclarationProvider {
030
031    private final FqName fqName;
032    private final FileBasedDeclarationProviderFactory factory;
033    private final Collection<JetFile> allFiles;
034    private final NotNullLazyValue<Collection<FqName>> allDeclaredPackages;
035
036
037    /*package*/ FileBasedPackageMemberDeclarationProvider(
038            @NotNull StorageManager storageManager,
039            @NotNull FqName _fqName,
040            @NotNull FileBasedDeclarationProviderFactory _factory,
041            @NotNull Collection<JetFile> allFiles
042    ) {
043        super(storageManager);
044        this.fqName = _fqName;
045        this.factory = _factory;
046        this.allFiles = allFiles;
047        this.allDeclaredPackages = storageManager.createLazyValue(new Computable<Collection<FqName>>() {
048            @Override
049            public Collection<FqName> compute() {
050                return factory.getAllDeclaredSubPackagesOf(fqName);
051            }
052        });
053    }
054
055    @Override
056    protected void doCreateIndex(@NotNull Index index) {
057        for (JetFile file : allFiles) {
058            for (JetDeclaration declaration : file.getDeclarations()) {
059                index.putToIndex(declaration);
060            }
061        }
062    }
063
064    @Override
065    public boolean isPackageDeclared(@NotNull Name name) {
066        return factory.isPackageDeclared(fqName.child(name));
067    }
068
069    @Override
070    public Collection<FqName> getAllDeclaredPackages() {
071        return allDeclaredPackages.compute();
072    }
073
074    @Override
075    public String toString() {
076        return "Declarations for package " + fqName;
077    }
078}