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        @Override
069        public ClassDescriptor getObjectDescriptor(@NotNull Name name) {
070            return filterDescriptor(workerScope.getObjectDescriptor(name));
071        }
072    
073        @NotNull
074        @Override
075        public Collection<ClassDescriptor> getObjectDescriptors() {
076            return Collections2.filter(workerScope.getObjectDescriptors(), predicate);
077        }
078    
079        @NotNull
080        @Override
081        public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
082            return Collections2.filter(workerScope.getProperties(name), predicate);
083        }
084    
085        @Override
086        public VariableDescriptor getLocalVariable(@NotNull Name name) {
087            return filterDescriptor(workerScope.getLocalVariable(name));
088        }
089    
090        @NotNull
091        @Override
092        public Collection<DeclarationDescriptor> getAllDescriptors() {
093            return Collections2.filter(workerScope.getAllDescriptors(), predicate);
094        }
095    
096        @NotNull
097        @Override
098        public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
099            return workerScope.getImplicitReceiversHierarchy();
100        }
101    
102        @NotNull
103        @Override
104        public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull LabelName labelName) {
105            return Collections2.filter(workerScope.getDeclarationsByLabel(labelName), predicate);
106        }
107    
108        @NotNull
109        @Override
110        public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
111            return Collections2.filter(workerScope.getOwnDeclaredDescriptors(), predicate);
112        }
113    
114        @TestOnly
115        @Override
116        public void printScopeStructure(@NotNull Printer p) {
117            p.println(getClass().getSimpleName(), " {");
118            p.pushIndent();
119    
120            p.print("workerScope = ");
121            workerScope.printScopeStructure(p.withholdIndentOnce());
122    
123            p.popIndent();
124            p.println("}");
125        }
126    }