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