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        @Override
059        public NamespaceDescriptor getNamespace(@NotNull Name name) {
060            return filterDescriptor(workerScope.getNamespace(name));
061        }
062    
063        @Override
064        public ClassifierDescriptor getClassifier(@NotNull Name name) {
065            return filterDescriptor(workerScope.getClassifier(name));
066        }
067    
068        @NotNull
069        @Override
070        public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
071            return Collections2.filter(workerScope.getProperties(name), predicate);
072        }
073    
074        @Override
075        public VariableDescriptor getLocalVariable(@NotNull Name name) {
076            return filterDescriptor(workerScope.getLocalVariable(name));
077        }
078    
079        @NotNull
080        @Override
081        public Collection<DeclarationDescriptor> getAllDescriptors() {
082            return Collections2.filter(workerScope.getAllDescriptors(), predicate);
083        }
084    
085        @NotNull
086        @Override
087        public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
088            return workerScope.getImplicitReceiversHierarchy();
089        }
090    
091        @NotNull
092        @Override
093        public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull LabelName labelName) {
094            return Collections2.filter(workerScope.getDeclarationsByLabel(labelName), predicate);
095        }
096    
097        @NotNull
098        @Override
099        public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
100            return Collections2.filter(workerScope.getOwnDeclaredDescriptors(), predicate);
101        }
102    
103        @TestOnly
104        @Override
105        public void printScopeStructure(@NotNull Printer p) {
106            p.println(getClass().getSimpleName(), " {");
107            p.pushIndent();
108    
109            p.print("workerScope = ");
110            workerScope.printScopeStructure(p.withholdIndentOnce());
111    
112            p.popIndent();
113            p.println("}");
114        }
115    }