summaryrefslogtreecommitdiffstats
path: root/models-base
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-12-09 11:30:40 +0000
committerliamfallon <liam.fallon@est.tech>2020-12-09 15:17:53 +0000
commitab145590ffdf99b9f4e132e5f20241099d6c5cbe (patch)
tree0a6d1cdd7a1112ff0771da7bec15229c12aaba02 /models-base
parent1ca0dd29ae3e348c92b2d23d85c5c135797e21af (diff)
Add functional method for mapping maps to PfUtils
Add a functional interface to PfUtils for mapping maps so that more complex conversions can be handled. List mapping functional methods added as well. Issue-ID: POLICY-2900 Change-Id: I4189e09d6d8621638960b5d9f0d19792a7faceb8 Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-base')
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfUtils.java42
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java22
2 files changed, 30 insertions, 34 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
index fa7d21f9d..d5b70d78a 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
@@ -1,6 +1,6 @@
-/*
+/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,7 +26,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.function.UnaryOperator;
+import java.util.function.Function;
import java.util.stream.Collectors;
import javax.ws.rs.core.Response;
@@ -68,15 +68,14 @@ public final class PfUtils {
}
/**
- * Convenience method to apply a mapping function to all of the elements of a list,
- * generating a new list.
+ * Convenience method to apply a mapping function to all of the elements of a list, generating a new list.
*
* @param source list whose elements are to be mapped, or {@code null}
* @param mapFunc mapping function
* @param defaultValue value to be returned if source is {@code null}
* @return a new list, containing mappings of all of the items in the original list
*/
- public static <T> List<T> mapList(List<T> source, UnaryOperator<T> mapFunc, List<T> defaultValue) {
+ public static <T, R> List<R> mapList(List<T> source, Function<T, R> mapFunc, List<R> defaultValue) {
if (source == null) {
return defaultValue;
}
@@ -85,34 +84,32 @@ public final class PfUtils {
}
/**
- * Convenience method to apply a mapping function to all of the elements of a list,
- * generating a new list.
+ * Convenience method to apply a mapping function to all of the elements of a list, generating a new list.
*
* @param source list whose elements are to be mapped, or {@code null}
* @param mapFunc mapping function
- * @return a new list, containing mappings of all of the items in the original list,
- * or {@code null} if the source is {@code null}
+ * @return a new list, containing mappings of all of the items in the original list, or {@code null} if the source
+ * is {@code null}
*/
- public static <T> List<T> mapList(List<T> source, UnaryOperator<T> mapFunc) {
+ public static <T, R> List<R> mapList(List<T> source, Function<T, R> mapFunc) {
return mapList(source, mapFunc, null);
}
/**
- * Convenience method to apply a mapping function to all of the values of a map,
- * generating a new map.
+ * Convenience method to apply a mapping function to all of the values of a map, generating a new map.
*
* @param source map whose values are to be mapped, or {@code null}
* @param mapFunc mapping function
* @param defaultValue value to be returned if source is {@code null}
* @return a new map, containing mappings of all of the items in the original map
*/
- public static <T> Map<String, T> mapMap(Map<String, T> source, UnaryOperator<T> mapFunc,
- Map<String, T> defaultValue) {
+ public static <T, R> Map<String, R> mapMap(Map<String, T> source, Function<T, R> mapFunc,
+ Map<String, R> defaultValue) {
if (source == null) {
return defaultValue;
}
- Map<String, T> map = new LinkedHashMap<>();
+ Map<String, R> map = new LinkedHashMap<>();
for (Entry<String, T> ent : source.entrySet()) {
map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
}
@@ -121,15 +118,14 @@ public final class PfUtils {
}
/**
- * Convenience method to apply a mapping function to all of the values of a map,
- * generating a new map.
+ * Convenience method to apply a mapping function to all of the values of a map, generating a new map.
*
* @param source map whose values are to be mapped, or {@code null}
* @param mapFunc mapping function
- * @return a new map, containing mappings of all of the items in the original map,
- * or {@code null} if the source is {@code null}
+ * @return a new map, containing mappings of all of the items in the original map, or {@code null} if the source is
+ * {@code null}
*/
- public static <T> Map<String, T> mapMap(Map<String, T> source, UnaryOperator<T> mapFunc) {
+ public static <T, R> Map<String, R> mapMap(Map<String, T> source, Function<T, R> mapFunc) {
return mapMap(source, mapFunc, null);
}
@@ -152,9 +148,9 @@ public final class PfUtils {
return clazz.getConstructor(clazz).newInstance(source);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
- | RuntimeException e) {
+ | RuntimeException e) {
throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
- "error copying concept key class: " + source.getClass().getName(), e);
+ "error copying concept key class: " + source.getClass().getName(), e);
}
}
}
diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java
index 19f8ee988..d96d2243f 100644
--- a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java
+++ b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -59,14 +59,14 @@ public class PfUtilsTest {
});
assertNull(resultList);
- List<String> origList = Arrays.asList("abc", "def");
- List<String> newList = PfUtils.mapList(origList, text -> text + "X");
+ List<String> origList = Arrays.asList("123", "456");
+ List<Integer> newList = PfUtils.mapList(origList, item -> Integer.valueOf(item) + 5);
- assertEquals(Arrays.asList("abcX", "defX"), newList);
+ assertEquals(Arrays.asList(128, 461), newList);
// verify that we can modify the list without throwing an exception
- newList.remove("abcX");
- newList.add("something else");
+ newList.remove(1);
+ newList.add(789);
}
@Test
@@ -77,15 +77,15 @@ public class PfUtilsTest {
assertNull(resultMap);
Map<String, String> origMap = new TreeMap<>();
- origMap.put("key2A", "xyz2");
- origMap.put("key2B", "pdq2");
- Map<String, String> newMap = PfUtils.mapMap(origMap, text -> text + "X");
+ origMap.put("key2A", "123");
+ origMap.put("key2B", "456");
+ Map<String, Integer> newMap = PfUtils.mapMap(origMap, item -> Integer.valueOf(item) + 10);
- assertEquals("{key2A=xyz2X, key2B=pdq2X}", newMap.toString());
+ assertEquals("{key2A=133, key2B=466}", newMap.toString());
// verify that we can modify the map without throwing an exception
newMap.remove("abcX");
- newMap.put("something", "else");
+ newMap.put("something", 789);
}
@Test