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:
		
							parent
							
								
									a33a4ed036
								
							
						
					
					
						commit
						1883b5919a
					
				|  | @ -32,7 +32,7 @@ import java.util.Set; | ||||||
| 
 | 
 | ||||||
| /** A read-only view of an array of key-value pairs. */ | /** A read-only view of an array of key-value pairs. */ | ||||||
| @SuppressWarnings("unchecked") | @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}. */ |   /** Returns a read-only view of the given {@code array}. */ | ||||||
|   public static <K, V> Map<K, V> wrap(List<Object> array) { |   public static <K, V> Map<K, V> wrap(List<Object> array) { | ||||||
|  |  | ||||||
|  | @ -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(); | ||||||
|  |   } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue