Remove repeating contexts in CompositeBindingContext creation

This commit is contained in:
Nikolay Krasko
2016-12-06 14:36:00 +03:00
parent 76916b0877
commit 45532e480f
@@ -25,9 +25,10 @@ import com.intellij.openapi.util.ModificationTracker
import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import java.util.*
class CompositeBindingContext private constructor( class CompositeBindingContext private constructor(
private val delegates: List<BindingContext> private val delegates: LinkedHashSet<BindingContext>
) : BindingContext { ) : BindingContext {
override fun getType(expression: KtExpression): KotlinType? { override fun getType(expression: KtExpression): KotlinType? {
return delegates.asSequence().map { it.getType(expression) }.firstOrNull { it != null } return delegates.asSequence().map { it.getType(expression) }.firstOrNull { it != null }
@@ -36,8 +37,9 @@ class CompositeBindingContext private constructor(
companion object { companion object {
fun create(delegates: List<BindingContext>): BindingContext { fun create(delegates: List<BindingContext>): BindingContext {
if (delegates.isEmpty()) return BindingContext.EMPTY if (delegates.isEmpty()) return BindingContext.EMPTY
if (delegates.size == 1) return delegates.first() val delegatesSet = LinkedHashSet(delegates)
return CompositeBindingContext(delegates) if (delegatesSet.size == 1) return delegates.first()
return CompositeBindingContext(delegatesSet)
} }
} }
@@ -51,7 +53,7 @@ class CompositeBindingContext private constructor(
override fun <K, V> getSliceContents(slice: ReadOnlySlice<K, V>): ImmutableMap<K, V> { override fun <K, V> getSliceContents(slice: ReadOnlySlice<K, V>): ImmutableMap<K, V> {
//we need intermediate map cause ImmutableMap doesn't support same entries obtained from different slices //we need intermediate map cause ImmutableMap doesn't support same entries obtained from different slices
var map = hashMapOf<K, V>() val map = hashMapOf<K, V>()
delegates.forEach { map.putAll(it.getSliceContents(slice)) } delegates.forEach { map.putAll(it.getSliceContents(slice)) }
return ImmutableMap.builder<K, V>().putAll(map).build() return ImmutableMap.builder<K, V>().putAll(map).build()
} }
@@ -72,8 +74,8 @@ class CompositeBindingContext private constructor(
return delegates.fold(emptySequence<Diagnostic>(), { r, t -> r + t.asSequence() }).iterator() return delegates.fold(emptySequence<Diagnostic>(), { r, t -> r + t.asSequence() }).iterator()
} }
override val modificationTracker = object : ModificationTracker { override val modificationTracker = ModificationTracker {
override fun getModificationCount() = delegates.fold(0L, { r, t -> r + t.modificationTracker.modificationCount }) delegates.fold(0L, { r, t -> r + t.modificationTracker.modificationCount })
} }
override fun all(): Collection<Diagnostic> { override fun all(): Collection<Diagnostic> {