ConcurrentHashSet — implement equals and hashCode

This commit is contained in:
Vladimir Krivosheev
2016-12-29 16:01:55 +01:00
parent dc78d8c88a
commit ac3de3ceaa
2 changed files with 80 additions and 0 deletions
@@ -117,5 +117,41 @@ public class ConcurrentHashSet<K> implements Set<K> {
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();
}
}
@@ -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<String>()
assertThat(set).isNotEqualTo(emptyMap<String, String>())
assertThat(set).isEqualTo(emptySet<String>())
assertThat(set).isEqualTo(ConcurrentHashSet<String>())
set.add("foo")
assertThat(set).isNotEqualTo(emptySet<String>())
assertThat(set).isEqualTo(setOf("foo"))
assertThat(set).isNotEqualTo(setOf("bar"))
val otherSet = ConcurrentHashSet<String>()
otherSet.add("bar")
assertThat(set).isNotEqualTo(otherSet)
otherSet.remove("bar")
otherSet.add("foo")
assertThat(set).isEqualTo(otherSet)
}
}