From ca20b8a82886aa74a57847be40152910c58fbdaf Mon Sep 17 00:00:00 2001 From: Tim Vernum Date: Wed, 8 Apr 2020 15:31:50 +1000 Subject: [PATCH] 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. --- .../main/java/org/elasticsearch/common/collect/Map.java | 8 ++++++++ .../main/java11/org/elasticsearch/common/collect/Map.java | 7 +++++++ .../java/org/elasticsearch/common/collect/MapTests.java | 8 +++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java b/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java index caa3aacd97f..3697f01abea 100644 --- a/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java +++ b/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java @@ -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 java.util.Map.Entry entry(K k, V v) { + return new AbstractMap.SimpleImmutableEntry<>(k, v); + } + /** * Returns an unmodifiable {@code Map} containing the entries of the given {@code Map}. * diff --git a/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java b/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java index f9badb14c2e..c213518ab2e 100644 --- a/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java +++ b/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java @@ -112,6 +112,13 @@ public class Map { return java.util.Map.ofEntries(entries); } + /** + * Delegates to the Java9 {@code Map.entry()} method. + */ + public static java.util.Map.Entry entry(K k, V v) { + return java.util.Map.entry(k, v); + } + /** * Delegates to the Java10 {@code Map.copyOf()} method. */ diff --git a/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java b/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java index 84034fcbec4..49e92008988 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java @@ -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 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); }