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.Name;
026 import org.jetbrains.jet.utils.Printer;
027
028 import java.util.Collection;
029 import java.util.List;
030
031 public class FilteringScope implements JetScope {
032 @NotNull private final JetScope workerScope;
033 @NotNull private final Predicate<DeclarationDescriptor> predicate;
034
035 public FilteringScope(@NotNull JetScope workerScope, @NotNull Predicate<DeclarationDescriptor> predicate) {
036 this.workerScope = workerScope;
037 this.predicate = predicate;
038 }
039
040 @NotNull
041 @Override
042 public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
043 return Collections2.filter(workerScope.getFunctions(name), predicate);
044 }
045
046 @NotNull
047 @Override
048 public DeclarationDescriptor getContainingDeclaration() {
049 return workerScope.getContainingDeclaration();
050 }
051
052 @Nullable
053 private <D extends DeclarationDescriptor> D filterDescriptor(@Nullable D descriptor) {
054 return descriptor != null && predicate.apply(descriptor) ? descriptor : null;
055 }
056
057 @Nullable
058 @Override
059 public PackageViewDescriptor getPackage(@NotNull Name name) {
060 return filterDescriptor(workerScope.getPackage(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 Name 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 }