Converted FilteringScope to Kotlin

This commit is contained in:
Valentin Kipyatkov
2014-10-23 11:28:38 +04:00
parent 0f5b23e254
commit ac306a1839
2 changed files with 58 additions and 113 deletions
@@ -1,113 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.scopes;
import kotlin.Function1;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.utils.Printer;
import java.util.Collection;
import java.util.List;
public class FilteringScope implements JetScope {
private final JetScope workerScope;
private final Function1<DeclarationDescriptor, Boolean> predicate;
public FilteringScope(@NotNull JetScope workerScope, @NotNull Function1<DeclarationDescriptor, Boolean> predicate) {
this.workerScope = workerScope;
this.predicate = predicate;
}
@NotNull
@Override
public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
return KotlinPackage.filter(workerScope.getFunctions(name), predicate);
}
@NotNull
@Override
public DeclarationDescriptor getContainingDeclaration() {
return workerScope.getContainingDeclaration();
}
@Nullable
private <D extends DeclarationDescriptor> D filterDescriptor(@Nullable D descriptor) {
return descriptor != null && predicate.invoke(descriptor) ? descriptor : null;
}
@Nullable
@Override
public PackageViewDescriptor getPackage(@NotNull Name name) {
return filterDescriptor(workerScope.getPackage(name));
}
@Override
public ClassifierDescriptor getClassifier(@NotNull Name name) {
return filterDescriptor(workerScope.getClassifier(name));
}
@NotNull
@Override
public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
return KotlinPackage.filter(workerScope.getProperties(name), predicate);
}
@Override
public VariableDescriptor getLocalVariable(@NotNull Name name) {
return filterDescriptor(workerScope.getLocalVariable(name));
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
return KotlinPackage.filter(workerScope.getAllDescriptors(), predicate);
}
@NotNull
@Override
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return workerScope.getImplicitReceiversHierarchy();
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) {
return KotlinPackage.filter(workerScope.getDeclarationsByLabel(labelName), predicate);
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
return KotlinPackage.filter(workerScope.getOwnDeclaredDescriptors(), predicate);
}
@Override
public void printScopeStructure(@NotNull Printer p) {
p.println(getClass().getSimpleName(), " {");
p.pushIndent();
p.print("workerScope = ");
workerScope.printScopeStructure(p.withholdIndentOnce());
p.popIndent();
p.println("}");
}
}
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.scopes
import org.jetbrains.jet.lang.descriptors.*
import org.jetbrains.jet.lang.resolve.name.Name
import org.jetbrains.jet.utils.Printer
public class FilteringScope(private val workerScope: JetScope, private val predicate: (DeclarationDescriptor) -> Boolean) : JetScope {
override fun getFunctions(name: Name) = workerScope.getFunctions(name).filter(predicate)
override fun getContainingDeclaration() = workerScope.getContainingDeclaration()
private fun <D : DeclarationDescriptor> filterDescriptor(descriptor: D?): D?
= if (descriptor != null && predicate(descriptor)) descriptor else null
override fun getPackage(name: Name) = filterDescriptor(workerScope.getPackage(name))
override fun getClassifier(name: Name) = filterDescriptor(workerScope.getClassifier(name))
override fun getProperties(name: Name) = workerScope.getProperties(name).filter(predicate)
override fun getLocalVariable(name: Name) = filterDescriptor(workerScope.getLocalVariable(name))
override fun getAllDescriptors() = workerScope.getAllDescriptors().filter(predicate)
override fun getImplicitReceiversHierarchy() = workerScope.getImplicitReceiversHierarchy()
override fun getDeclarationsByLabel(labelName: Name) = workerScope.getDeclarationsByLabel(labelName).filter(predicate)
override fun getOwnDeclaredDescriptors() = workerScope.getOwnDeclaredDescriptors().filter(predicate)
override fun printScopeStructure(p: Printer) {
p.println(javaClass.getSimpleName(), " {")
p.pushIndent()
p.print("workerScope = ")
workerScope.printScopeStructure(p.withholdIndentOnce())
p.popIndent()
p.println("}")
}
}