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    
017    package org.jetbrains.jet.lang.resolve.java;
018    
019    import com.google.common.base.Predicate;
020    import com.google.common.collect.Collections2;
021    import com.intellij.openapi.components.ServiceManager;
022    import com.intellij.openapi.project.Project;
023    import com.intellij.psi.PsiFile;
024    import com.intellij.psi.search.GlobalSearchScope;
025    import com.intellij.util.Function;
026    import org.jetbrains.annotations.NotNull;
027    import org.jetbrains.jet.lang.psi.JetFile;
028    import org.jetbrains.jet.lang.psi.JetPsiUtil;
029    import org.jetbrains.jet.lang.resolve.name.FqName;
030    
031    import java.util.Collection;
032    
033    public abstract class JetFilesProvider {
034        public static JetFilesProvider getInstance(Project project) {
035            return ServiceManager.getService(project, JetFilesProvider.class);
036        }
037    
038        public final Function<JetFile, Collection<JetFile>> allPackageFiles() {
039            return new Function<JetFile, Collection<JetFile>>() {
040                @Override
041                public Collection<JetFile> fun(JetFile file) {
042                    return Collections2.filter(sampleToAllFilesInModule().fun(file), new SameJetFilePredicate(file));
043                }
044            };
045        }
046    
047        public abstract Function<JetFile, Collection<JetFile>> sampleToAllFilesInModule();
048        @NotNull
049        public abstract Collection<JetFile> allInScope(@NotNull GlobalSearchScope scope);
050        public abstract boolean isFileInScope(@NotNull JetFile file, @NotNull GlobalSearchScope scope);
051    
052        public static class SameJetFilePredicate implements Predicate<PsiFile> {
053            private final FqName name;
054    
055            public SameJetFilePredicate(JetFile file) {
056                this.name = JetPsiUtil.getFQName(file);
057            }
058    
059            @Override
060            public boolean apply(PsiFile psiFile) {
061                return JetPsiUtil.getFQName((JetFile) psiFile).equals(name);
062            }
063        }
064    }