From ac3de3ceaa56ca401a8a1f74f288a7a454c33c97 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 29 Dec 2016 14:54:58 +0100 Subject: [PATCH] =?UTF-8?q?ConcurrentHashSet=20=E2=80=94=20implement=20equ?= =?UTF-8?q?als=20and=20hashCode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/containers/ConcurrentHashSet.java | 36 +++++++++++++++ .../containers/hash/ConcurrentHashSetTest.kt | 44 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 platform/util/testSrc/com/intellij/util/containers/hash/ConcurrentHashSetTest.kt diff --git a/platform/util/src/com/intellij/util/containers/ConcurrentHashSet.java b/platform/util/src/com/intellij/util/containers/ConcurrentHashSet.java index cfae26778ee9..fadb48d0806b 100644 --- a/platform/util/src/com/intellij/util/containers/ConcurrentHashSet.java +++ b/platform/util/src/com/intellij/util/containers/ConcurrentHashSet.java @@ -117,5 +117,41 @@ public class ConcurrentHashSet implements Set { public String toString() { return map.keySet().toString(); } + + @SuppressWarnings("deprecation") + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof ConcurrentHashSet) { + return map.equals(((ConcurrentHashSet)obj).map); + } + + if (!(obj instanceof Set)) { + return false; + } + + Set c = (Set)obj; + if (c.size() != size()) { + return false; + } + + try { + return containsAll(c); + } + catch (ClassCastException ignored) { + return false; + } + catch (NullPointerException ignored) { + return false; + } + } + + @Override + public int hashCode() { + return map.hashCode(); + } } diff --git a/platform/util/testSrc/com/intellij/util/containers/hash/ConcurrentHashSetTest.kt b/platform/util/testSrc/com/intellij/util/containers/hash/ConcurrentHashSetTest.kt new file mode 100644 index 000000000000..3d5686955df1 --- /dev/null +++ b/platform/util/testSrc/com/intellij/util/containers/hash/ConcurrentHashSetTest.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2000-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 com.intellij.util.containers.hash + +import com.intellij.util.containers.ConcurrentHashSet +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class ConcurrentHashSetTest { + @Suppress("DEPRECATION") + @Test + fun testEquals() { + val set = ConcurrentHashSet() + assertThat(set).isNotEqualTo(emptyMap()) + assertThat(set).isEqualTo(emptySet()) + assertThat(set).isEqualTo(ConcurrentHashSet()) + + set.add("foo") + assertThat(set).isNotEqualTo(emptySet()) + assertThat(set).isEqualTo(setOf("foo")) + assertThat(set).isNotEqualTo(setOf("bar")) + + val otherSet = ConcurrentHashSet() + otherSet.add("bar") + assertThat(set).isNotEqualTo(otherSet) + + otherSet.remove("bar") + otherSet.add("foo") + assertThat(set).isEqualTo(otherSet) + } +} \ No newline at end of file