Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods to get List values from Map #241

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions src/main/java/org/apache/commons/collections4/MapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -861,6 +862,57 @@ public static <K> long getLongValue(final Map<? super K, ?> map, final K key, fi
return applyDefaultValue(map, key, MapUtils::getLong, defaultValue).longValue();
}

/**
* Gets a List from a Map in a null-safe manner.
* <p>
* If the value returned from the specified list is not a List then {@code null} is returned.
* </p>
*
* @param <K> 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 <K> List<?> getList(final Map<? super K, ?> 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 <K> 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 <K> List<?> getList(final Map<? super K, ?> map, final K key, final Function<K, List<?>> 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 <K> 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 <K> List<?> getList(final Map<? super K, ?> 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.
* <p>
Expand Down Expand Up @@ -890,7 +942,7 @@ public static <K> long getLongValue(final Map<? super K, ?> 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
*/
Expand All @@ -907,7 +959,7 @@ public static <K> long getLongValue(final Map<? super K, ?> 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 <K> Map<?, ?> getMap(final Map<? super K, ?> map, final K key, final Map<?, ?> defaultValue) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/apache/commons/collections4/MapUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,19 @@ public void testGetBooleanValue() {
assertTrue(MapUtils.getBoolean(inStr, "str1", true));
}

@Test
public void testGetList() {
final Map<String, List<String>> in = new HashMap<>();
final List<String> 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<String, Map<String, String>> in = new HashMap<>();
Expand Down