From d228e77c0ced8b295236ed3a15027902b427c713 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 8 Feb 2018 14:10:55 +0300 Subject: [PATCH] Add AbstractSet from main Kotlin repo --- .../kotlin/kotlin/collections/AbstractSet.kt | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 runtime/src/main/kotlin/kotlin/collections/AbstractSet.kt diff --git a/runtime/src/main/kotlin/kotlin/collections/AbstractSet.kt b/runtime/src/main/kotlin/kotlin/collections/AbstractSet.kt new file mode 100644 index 00000000000..500ab16f95d --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/collections/AbstractSet.kt @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2016 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 kotlin.collections + +/** + * Provides a skeletal implementation of the read-only [Set] interface. + * + * This class is intended to help implementing read-only sets so it doesn't support concurrent modification tracking. + * + * @param E the type of elements contained in the set. The set is covariant on its element type. + */ +@SinceKotlin("1.1") +public abstract class AbstractSet protected constructor() : AbstractCollection(), Set { + + /** + * Compares this set with other set instance with the unordered structural equality. + * + * @return true, if [other] instance is a [Set] of the same size, all elements of which are contained in this set. + */ + override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other !is Set<*>) return false + return setEquals(this, other) + } + + /** + * Returns the hash code value for this set. + */ + override fun hashCode(): Int = unorderedHashCode(this) + + internal companion object { + internal fun unorderedHashCode(c: Collection<*>): Int { + var hashCode = 0 + for (element in c) { + hashCode += (element?.hashCode() ?: 0) + } + return hashCode + } + + internal fun setEquals(c: Set<*>, other: Set<*>): Boolean { + if (c.size != other.size) return false + return c.containsAll(other) + } + } + +} \ No newline at end of file