Refactoring: replace condition with function
This commit is contained in:
+5
-5
@@ -17,10 +17,10 @@
|
||||
package org.jetbrains.kotlin.resolve.diagnostics;
|
||||
|
||||
import com.intellij.openapi.util.AtomicNotNullLazyValue;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.containers.ConcurrentMultiMap;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
||||
|
||||
@@ -28,7 +28,7 @@ import java.util.Collection;
|
||||
|
||||
public class DiagnosticsElementsCache {
|
||||
private final Diagnostics diagnostics;
|
||||
private final Condition<Diagnostic> filter;
|
||||
private final Function1<Diagnostic, Boolean> filter;
|
||||
|
||||
private final AtomicNotNullLazyValue<MultiMap<PsiElement, Diagnostic>> elementToDiagnostic = new AtomicNotNullLazyValue<MultiMap<PsiElement, Diagnostic>>() {
|
||||
@NotNull
|
||||
@@ -38,7 +38,7 @@ public class DiagnosticsElementsCache {
|
||||
}
|
||||
};
|
||||
|
||||
public DiagnosticsElementsCache(Diagnostics diagnostics, Condition<Diagnostic> filter) {
|
||||
public DiagnosticsElementsCache(Diagnostics diagnostics, Function1<Diagnostic, Boolean> filter) {
|
||||
this.diagnostics = diagnostics;
|
||||
this.filter = filter;
|
||||
}
|
||||
@@ -48,10 +48,10 @@ public class DiagnosticsElementsCache {
|
||||
return elementToDiagnostic.getValue().get(psiElement);
|
||||
}
|
||||
|
||||
private static MultiMap<PsiElement, Diagnostic> buildElementToDiagnosticCache(Diagnostics diagnostics, Condition<Diagnostic> filter) {
|
||||
private static MultiMap<PsiElement, Diagnostic> buildElementToDiagnosticCache(Diagnostics diagnostics, Function1<Diagnostic, Boolean> filter) {
|
||||
MultiMap<PsiElement, Diagnostic> elementToDiagnostic = new ConcurrentMultiMap<PsiElement, Diagnostic>();
|
||||
for (Diagnostic diagnostic : diagnostics) {
|
||||
if (filter.value(diagnostic)) {
|
||||
if (filter.invoke(diagnostic)) {
|
||||
elementToDiagnostic.putValue(diagnostic.getPsiElement(), diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
+12
-5
@@ -24,8 +24,9 @@ import com.intellij.openapi.util.ModificationTracker;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.containers.ConcurrentWeakValueHashMap;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.FilteringIterator;
|
||||
import kotlin.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||
@@ -70,12 +71,13 @@ public class DiagnosticsWithSuppression implements Diagnostics {
|
||||
// The cache is weak: we're OK with losing it
|
||||
private final Map<KtAnnotated, Suppressor> suppressors = new ConcurrentWeakValueHashMap<KtAnnotated, Suppressor>();
|
||||
|
||||
private final Condition<Diagnostic> filter = new Condition<Diagnostic>() {
|
||||
private final Function1<Diagnostic, Boolean> filter = new Function1<Diagnostic, Boolean>() {
|
||||
@Override
|
||||
public boolean value(Diagnostic diagnostic) {
|
||||
public Boolean invoke(Diagnostic diagnostic) {
|
||||
return !isSuppressed(diagnostic);
|
||||
}
|
||||
};
|
||||
|
||||
private final DiagnosticsElementsCache elementsCache = new DiagnosticsElementsCache(this, filter);
|
||||
|
||||
public DiagnosticsWithSuppression(@NotNull BindingContext context, @NotNull Collection<Diagnostic> diagnostics) {
|
||||
@@ -92,13 +94,18 @@ public class DiagnosticsWithSuppression implements Diagnostics {
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<Diagnostic> iterator() {
|
||||
return new FilteringIterator<Diagnostic, Diagnostic>(diagnostics.iterator(), filter);
|
||||
return new FilteringIterator<Diagnostic, Diagnostic>(diagnostics.iterator(), new Condition<Diagnostic>() {
|
||||
@Override
|
||||
public boolean value(Diagnostic diagnostic) {
|
||||
return filter.invoke(diagnostic);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<Diagnostic> all() {
|
||||
return ContainerUtil.filter(diagnostics, filter);
|
||||
return CollectionsKt.filter(diagnostics, filter);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -26,7 +26,7 @@ public class SimpleDiagnostics(diagnostics: Collection<Diagnostic>) : Diagnostic
|
||||
private val diagnostics = ArrayList(diagnostics)
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val elementsCache = DiagnosticsElementsCache(this, Condition.TRUE as Condition<Diagnostic>?)
|
||||
private val elementsCache = DiagnosticsElementsCache(this, { true })
|
||||
|
||||
override fun all() = diagnostics
|
||||
|
||||
|
||||
Reference in New Issue
Block a user