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 kotlin.Function1;
020 import kotlin.KotlinPackage;
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.Name;
025 import org.jetbrains.jet.utils.Printer;
026
027 import java.util.Collection;
028 import java.util.List;
029
030 public class FilteringScope implements JetScope {
031 private final JetScope workerScope;
032 private final Function1<DeclarationDescriptor, Boolean> predicate;
033
034 public FilteringScope(@NotNull JetScope workerScope, @NotNull Function1<DeclarationDescriptor, Boolean> 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 KotlinPackage.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.invoke(descriptor) ? descriptor : null;
054 }
055
056 @Nullable
057 @Override
058 public PackageViewDescriptor getPackage(@NotNull Name name) {
059 return filterDescriptor(workerScope.getPackage(name));
060 }
061
062 @Override
063 public ClassifierDescriptor getClassifier(@NotNull Name name) {
064 return filterDescriptor(workerScope.getClassifier(name));
065 }
066
067 @NotNull
068 @Override
069 public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
070 return KotlinPackage.filter(workerScope.getProperties(name), predicate);
071 }
072
073 @Override
074 public VariableDescriptor getLocalVariable(@NotNull Name name) {
075 return filterDescriptor(workerScope.getLocalVariable(name));
076 }
077
078 @NotNull
079 @Override
080 public Collection<DeclarationDescriptor> getAllDescriptors() {
081 return KotlinPackage.filter(workerScope.getAllDescriptors(), predicate);
082 }
083
084 @NotNull
085 @Override
086 public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
087 return workerScope.getImplicitReceiversHierarchy();
088 }
089
090 @NotNull
091 @Override
092 public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) {
093 return KotlinPackage.filter(workerScope.getDeclarationsByLabel(labelName), predicate);
094 }
095
096 @NotNull
097 @Override
098 public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
099 return KotlinPackage.filter(workerScope.getOwnDeclaredDescriptors(), predicate);
100 }
101
102 @Override
103 public void printScopeStructure(@NotNull Printer p) {
104 p.println(getClass().getSimpleName(), " {");
105 p.pushIndent();
106
107 p.print("workerScope = ");
108 workerScope.printScopeStructure(p.withholdIndentOnce());
109
110 p.popIndent();
111 p.println("}");
112 }
113 }