From a23d4fb13f5fe4e4887368e92982e1f4b4464259 Mon Sep 17 00:00:00 2001 From: johnson-abraham Date: Wed, 16 Jun 2021 19:59:35 +0530 Subject: [PATCH] methods to get list from a map. --- .../apache/commons/collections4/MapUtils.java | 56 ++++++++++++++++++- .../commons/collections4/MapUtilsTest.java | 13 +++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/MapUtils.java b/src/main/java/org/apache/commons/collections4/MapUtils.java index a84b783201..f86c9e42ac 100644 --- a/src/main/java/org/apache/commons/collections4/MapUtils.java +++ b/src/main/java/org/apache/commons/collections4/MapUtils.java @@ -26,6 +26,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Map.Entry; @@ -861,6 +862,57 @@ public static long getLongValue(final Map map, final K key, fi return applyDefaultValue(map, key, MapUtils::getLong, defaultValue).longValue(); } + /** + * Gets a List from a Map in a null-safe manner. + *

+ * If the value returned from the specified list is not a List then {@code null} is returned. + *

+ * + * @param the key type + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a List, {@code null} if null map input + */ + public static List getList(final Map map, final K key) { + if (map != null) { + final Object answer = map.get(key); + if (answer instanceof List) { + return (List) answer; + } + } + return null; + } + + /** + * Looks up the given key in the given map, converting the result into a list, using the defaultFunction to produce + * the default value if the conversion fails. + * + * @param the key type + * @param map the map whose value to look up + * @param key the key of the value to look up in that map + * @param defaultFunction what to produce the default value if the value is null or if the conversion fails + * @return the value in the map as a List, or defaultValue produced by the defaultFunction if the original value + * is null, the map is null or the List conversion fails + */ + public static List getList(final Map map, final K key, final Function> defaultFunction) { + return applyDefaultFunction(map, key, MapUtils::getList, defaultFunction); + } + + /** + * Looks up the given key in the given map, converting the result into a List, using the default value if the + * conversion fails. + * + * @param the key type + * @param map the map whose value to look up + * @param key the key of the value to look up in that map + * @param defaultValue what to return if the value is null or if the conversion fails + * @return the value in the map as a List, or defaultValue if the original value is null, the map is null or the + * List conversion fails + */ + public static List getList(final Map map, final K key, final List defaultValue) { + return applyDefaultValue(map, key, MapUtils::getList, defaultValue); + } + /** * Gets a Map from a Map in a null-safe manner. *

@@ -890,7 +942,7 @@ public static long getLongValue(final Map map, final K key, fi * @param map the map whose value to look up * @param key the key of the value to look up in that map * @param defaultFunction what to produce the default value if the value is null or if the conversion fails - * @return the value in the map as a number, or defaultValue produced by the defaultFunction if the original value + * @return the value in the map as a Map, or defaultValue produced by the defaultFunction if the original value * is null, the map is null or the map conversion fails * @since 4.5 */ @@ -907,7 +959,7 @@ public static long getLongValue(final Map map, final K key, fi * @param map the map whose value to look up * @param key the key of the value to look up in that map * @param defaultValue what to return if the value is null or if the conversion fails - * @return the value in the map as a number, or defaultValue if the original value is null, the map is null or the + * @return the value in the map as a Map, or defaultValue if the original value is null, the map is null or the * map conversion fails */ public static Map getMap(final Map map, final K key, final Map defaultValue) { diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java index e6aa766168..08701e6d60 100644 --- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java @@ -1273,6 +1273,19 @@ public void testGetBooleanValue() { assertTrue(MapUtils.getBoolean(inStr, "str1", true)); } + @Test + public void testGetList() { + final Map> in = new HashMap<>(); + final List valList = new ArrayList<>(); + valList.add("value1"); + in.put("key1", valList); + final List outValue = MapUtils.getList(in, "key1", (List) null); + + assertEquals("value1", outValue.get(0)); + assertNull(MapUtils.getList(in, "key2", (List) null)); + assertNull(MapUtils.getList(null, "key2", (List) null)); + } + @Test public void testGetMap() { final Map> in = new HashMap<>();