Skip to content

Commit

Permalink
Fix HashMap.containsAll for some corner cases. (JetBrains#2723)
Browse files Browse the repository at this point in the history
  • Loading branch information
olonho authored Feb 26, 2019
1 parent 55fc134 commit 3a70c3b
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions runtime/src/main/kotlin/kotlin/collections/HashMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,15 @@ actual class HashMap<K, V> private constructor(

private fun contentEquals(other: Map<*, *>): Boolean = _size == other.size && containsAllEntries(other.entries)

internal fun containsAllEntries(m: Collection<Map.Entry<*, *>>): Boolean {
internal fun containsAllEntries(m: Collection<*>): Boolean {
val it = m.iterator()
while (it.hasNext()) {
val entry = it.next()
try {
@Suppress("UNCHECKED_CAST") // todo: get rid of unchecked cast here somehow
if (!containsEntry(entry as Map.Entry<K, V>))
if (entry == null || !containsEntry(entry as Map.Entry<K, V>))
return false
} catch(e: ClassCastException) {
} catch (e: ClassCastException) {
return false
}
}
Expand Down Expand Up @@ -676,9 +676,7 @@ internal class HashMapEntrySet<K, V> internal constructor(
override fun retainAll(elements: Collection<MutableMap.MutableEntry<K, V>>): Boolean = backing.retainAllEntries(elements)

override fun equals(other: Any?): Boolean =
other === this ||
other is Set<*> &&
contentEquals(other)
other === this || other is Set<*> && contentEquals(other)

override fun hashCode(): Int {
var result = 0
Expand Down

0 comments on commit 3a70c3b

Please sign in to comment.