Make the ReadOnlyArrayMap extend AbstractMap for equals/hashcode implementation (#2963)

* Make the ReadOnlyArrayMap extend AbstractMap for equals/hashcode implementation

* formatting

* use guava's EqualsTester to save a few lines

* remove redundant hashcode verifications

* add an empty map to the equals test
This commit is contained in:
John Watson 2021-03-03 14:05:18 -08:00 committed by GitHub
parent a33a4ed036
commit 1883b5919a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -32,7 +32,7 @@ import java.util.Set;
/** A read-only view of an array of key-value pairs. */
@SuppressWarnings("unchecked")
public final class ReadOnlyArrayMap<K, V> implements Map<K, V> {
public final class ReadOnlyArrayMap<K, V> extends AbstractMap<K, V> {
/** Returns a read-only view of the given {@code array}. */
public static <K, V> Map<K, V> wrap(List<Object> array) {

View File

@ -0,0 +1,28 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.api.internal;
import com.google.common.testing.EqualsTester;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
class ReadOnlyArrayMapTest {
@Test
void equalsHashCode() {
Map<String, String> one = ReadOnlyArrayMap.wrap(Arrays.asList("a", "b"));
Map<String, String> two = ReadOnlyArrayMap.wrap(Arrays.asList("a", "b"));
Map<String, String> three = ReadOnlyArrayMap.wrap(Arrays.asList("c", "d"));
Map<String, String> empty = ReadOnlyArrayMap.wrap(Collections.emptyList());
new EqualsTester()
.addEqualityGroup(one, two)
.addEqualityGroup(three)
.addEqualityGroup(empty, empty)
.testEquals();
}
}