Java8 implementation of Map.Entry (#54778)

A Java8 compatible version of Map.ofEntries() was added in #54183,
but it really needs a compat version of Map.entry as well in order to
facilitate easy backports from master.
This commit is contained in:
Tim Vernum 2020-04-08 15:31:50 +10:00 committed by GitHub
parent d02f774cb6
commit ca20b8a828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.common.collect;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
@ -148,6 +149,13 @@ public class Map {
}
}
/**
* Returns an unmodifiable Map.Entry for the provided key and value.
*/
public static <K, V> java.util.Map.Entry<K, V> entry(K k, V v) {
return new AbstractMap.SimpleImmutableEntry<>(k, v);
}
/**
* Returns an unmodifiable {@code Map} containing the entries of the given {@code Map}.
*

View File

@ -112,6 +112,13 @@ public class Map {
return java.util.Map.ofEntries(entries);
}
/**
* Delegates to the Java9 {@code Map.entry()} method.
*/
public static <K, V> java.util.Map.Entry<K, V> entry(K k, V v) {
return java.util.Map.entry(k, v);
}
/**
* Delegates to the Java10 {@code Map.copyOf()} method.
*/

View File

@ -21,8 +21,6 @@ package org.elasticsearch.common.collect;
import org.elasticsearch.test.ESTestCase;
import java.util.AbstractMap;
import static org.hamcrest.CoreMatchers.equalTo;
public class MapTests extends ESTestCase {
@ -101,9 +99,9 @@ public class MapTests extends ESTestCase {
public void testOfEntries() {
final java.util.Map<String, Integer> map = Map.ofEntries(
new AbstractMap.SimpleEntry<>(numbers[0], 0),
new AbstractMap.SimpleEntry<>(numbers[1], 1),
new AbstractMap.SimpleEntry<>(numbers[2], 2)
Map.entry(numbers[0], 0),
Map.entry(numbers[1], 1),
Map.entry(numbers[2], 2)
);
validateMapContents(map, 3);
}