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.intellij.openapi.components.ServiceManager;
021    import com.intellij.openapi.project.Project;
022    import com.intellij.psi.PsiFile;
023    import com.intellij.psi.search.GlobalSearchScope;
024    import com.intellij.util.Function;
025    import org.jetbrains.annotations.NotNull;
026    import org.jetbrains.jet.lang.psi.JetFile;
027    import org.jetbrains.jet.lang.psi.JetPsiUtil;
028    import org.jetbrains.jet.lang.resolve.name.FqName;
029    
030    import java.util.Collection;
031    import java.util.LinkedList;
032    import java.util.List;
033    
034    public abstract class JetFilesProvider {
035        public static JetFilesProvider getInstance(Project project) {
036            return ServiceManager.getService(project, JetFilesProvider.class);
037        }
038    
039        public final Function<JetFile, List<JetFile>> allNamespaceFiles() {
040            return new Function<JetFile, List<JetFile>>() {
041                @Override
042                public List<JetFile> fun(JetFile file) {
043                    return new SameJetFilePredicate(file).filter(sampleToAllFilesInModule().fun(file));
044                }
045            };
046        }
047    
048        public abstract Function<JetFile, Collection<JetFile>> sampleToAllFilesInModule();
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            public List<JetFile> filter(Collection<JetFile> allFiles) {
065                LinkedList<JetFile> files = new LinkedList<JetFile>();
066                for (JetFile aFile : allFiles) {
067                    if(apply(aFile))
068                        files.add(aFile);
069                }
070                return files;
071            }
072        }
073    }