Convert Model Exception to Runtime Exception
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfUtils.java
index fa7d21f..6a119d6 100644 (file)
@@ -1,6 +1,6 @@
-/*
+/*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
 package org.onap.policy.models.base;
 
 import java.lang.reflect.InvocationTargetException;
+import java.util.Collection;
 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;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
 
 /**
  * Utility class for Policy Framework concept utilities.
@@ -68,15 +71,45 @@ public final class PfUtils {
     }
 
     /**
-     * Convenience method to apply a mapping function to all of the elements of a list,
-     * generating a new list.
+     * Compare two collections of the same type, nulls are allowed.
+     *
+     * @param leftCollection the first collection
+     * @param rightCollection the second collection
+     * @return a measure of the comparison
+     */
+    public static <T> int compareCollections(final Collection<T> leftCollection, final Collection<T> rightCollection) {
+        if (CollectionUtils.isEmpty(leftCollection) && CollectionUtils.isEmpty(rightCollection)) {
+            return 0;
+        }
+
+        return compareObjects(leftCollection, rightCollection);
+    }
+
+
+    /**
+     * Compare two maps of the same type, nulls are allowed.
+     *
+     * @param leftMap the first map
+     * @param rightMap the second map
+     * @return a measure of the comparison
+     */
+    public static <K, V> int compareMaps(final Map<K, V> leftMap, final Map<K, V> rightMap) {
+        if (MapUtils.isEmpty(leftMap) && MapUtils.isEmpty(rightMap)) {
+            return 0;
+        }
+
+        return compareObjects(leftMap, rightMap);
+    }
+
+    /**
+     * 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,35 +118,33 @@ 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 <K, T, R> Map<K, R> mapMap(Map<K, T> source, Function<T, R> mapFunc,
+            Map<K, R> defaultValue) {
         if (source == null) {
             return defaultValue;
         }
 
-        Map<String, T> map = new LinkedHashMap<>();
-        for (Entry<String, T> ent : source.entrySet()) {
+        Map<K, R> map = new LinkedHashMap<>();
+        for (Entry<K, T> ent : source.entrySet()) {
             map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
         }
 
@@ -121,15 +152,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 <K, T, R> Map<K, R> mapMap(Map<K, T> source, Function<T, R> mapFunc) {
         return mapMap(source, mapFunc, null);
     }
 
@@ -152,9 +182,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);
         }
     }
 }