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.scopes;
018    
019    import com.google.common.base.Predicate;
020    import com.google.common.collect.Collections2;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.annotations.TestOnly;
024    import org.jetbrains.jet.lang.descriptors.*;
025    import org.jetbrains.jet.lang.resolve.name.LabelName;
026    import org.jetbrains.jet.lang.resolve.name.Name;
027    import org.jetbrains.jet.utils.Printer;
028    
029    import java.util.Collection;
030    import java.util.List;
031    
032    public class FilteringScope implements JetScope {
033        @NotNull private final JetScope workerScope;
034        @NotNull private final Predicate<DeclarationDescriptor> predicate;
035    
036        public FilteringScope(@NotNull JetScope workerScope, @NotNull Predicate<DeclarationDescriptor> predicate) {
037            this.workerScope = workerScope;
038            this.predicate = predicate;
039        }
040    
041        @NotNull
042        @Override
043        public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
044            return Collections2.filter(workerScope.getFunctions(name), predicate);
045        }
046    
047        @NotNull
048        @Override
049        public DeclarationDescriptor getContainingDeclaration() {
050            return workerScope.getContainingDeclaration();
051        }
052    
053        @Nullable
054        private <D extends DeclarationDescriptor> D filterDescriptor(@Nullable D descriptor) {
055            return descriptor != null && predicate.apply(descriptor) ? descriptor : null;
056        }
057    
058        @Nullable
059        @Override
060        public PackageViewDescriptor getPackage(@NotNull Name name) {
061            return filterDescriptor(workerScope.getPackage(name));
062        }
063    
064        @Override
065        public ClassifierDescriptor getClassifier(@NotNull Name name) {
066            return filterDescriptor(workerScope.getClassifier(name));
067        }
068    
069        @NotNull
070        @Override
071        public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
072            return Collections2.filter(workerScope.getProperties(name), predicate);
073        }
074    
075        @Override
076        public VariableDescriptor getLocalVariable(@NotNull Name name) {
077            return filterDescriptor(workerScope.getLocalVariable(name));
078        }
079    
080        @NotNull
081        @Override
082        public Collection<DeclarationDescriptor> getAllDescriptors() {
083            return Collections2.filter(workerScope.getAllDescriptors(), predicate);
084        }
085    
086        @NotNull
087        @Override
088        public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
089            return workerScope.getImplicitReceiversHierarchy();
090        }
091    
092        @NotNull
093        @Override
094        public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull LabelName labelName) {
095            return Collections2.filter(workerScope.getDeclarationsByLabel(labelName), predicate);
096        }
097    
098        @NotNull
099        @Override
100        public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
101            return Collections2.filter(workerScope.getOwnDeclaredDescriptors(), predicate);
102        }
103    
104        @TestOnly
105        @Override
106        public void printScopeStructure(@NotNull Printer p) {
107            p.println(getClass().getSimpleName(), " {");
108            p.pushIndent();
109    
110            p.print("workerScope = ");
111            workerScope.printScopeStructure(p.withholdIndentOnce());
112    
113            p.popIndent();
114            p.println("}");
115        }
116    }