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.jet.lang.psi.JetFile;
026    import org.jetbrains.jet.lang.psi.JetPsiUtil;
027    import org.jetbrains.jet.lang.resolve.name.FqName;
028    
029    import java.util.Collection;
030    import java.util.LinkedList;
031    import java.util.List;
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, List<JetFile>> allNamespaceFiles() {
039            return new Function<JetFile, List<JetFile>>() {
040                @Override
041                public List<JetFile> fun(JetFile file) {
042                    return new SameJetFilePredicate(file).filter(sampleToAllFilesInModule().fun(file));
043                }
044            };
045        }
046    
047        public abstract Function<JetFile, Collection<JetFile>> sampleToAllFilesInModule();
048        public abstract Collection<JetFile> allInScope(GlobalSearchScope scope);
049    
050        public static class SameJetFilePredicate implements Predicate<PsiFile> {
051            private final FqName name;
052    
053            public SameJetFilePredicate(JetFile file) {
054                this.name = JetPsiUtil.getFQName(file);
055            }
056    
057            @Override
058            public boolean apply(PsiFile psiFile) {
059                return JetPsiUtil.getFQName((JetFile) psiFile).equals(name);
060            }
061    
062            public List<JetFile> filter(Collection<JetFile> allFiles) {
063                LinkedList<JetFile> files = new LinkedList<JetFile>();
064                for (JetFile aFile : allFiles) {
065                    if(apply(aFile))
066                        files.add(aFile);
067                }
068                return files;
069            }
070        }
071    }