Merge "Added VFModule count"
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.function.Function;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
32
33 /**
34  * Utility class for Policy Framework concept utilities.
35  *
36  * @author Liam Fallon (liam.fallon@est.tech)
37  */
38 public final class PfUtils {
39     private PfUtils() {
40         // Cannot be subclassed
41     }
42
43     /**
44      * Compare two objects using their equals methods, nulls are allowed.
45      *
46      * @param leftObject the first object
47      * @param rightObject the second object
48      * @return a measure of the comparison
49      */
50     public static int compareObjects(final Object leftObject, final Object rightObject) {
51         if (leftObject == null && rightObject == null) {
52             return 0;
53         }
54
55         if (leftObject == null) {
56             return 1;
57         }
58
59         if (rightObject == null) {
60             return -1;
61         }
62
63         if (!leftObject.equals(rightObject)) {
64             return leftObject.hashCode() - rightObject.hashCode();
65         }
66
67         return 0;
68     }
69
70     /**
71      * Convenience method to apply a mapping function to all of the elements of a list,
72      * generating a new list.
73      *
74      * @param source list whose elements are to be mapped, or {@code null}
75      * @param mapFunc mapping function
76      * @param defaultValue value to be returned if source is {@code null}
77      * @return a new list, containing mappings of all of the items in the original list
78      */
79     public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc, List<T> defaultValue) {
80         if (source == null) {
81             return defaultValue;
82         }
83
84         return source.stream().map(mapFunc).collect(Collectors.toList());
85     }
86
87     /**
88      * Convenience method to apply a mapping function to all of the elements of a list,
89      * generating a new list.
90      *
91      * @param source list whose elements are to be mapped, or {@code null}
92      * @param mapFunc mapping function
93      * @return a new list, containing mappings of all of the items in the original list,
94      *         or {@code null} if the source is {@code null}
95      */
96     public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
97         return mapList(source, mapFunc, null);
98     }
99
100     /**
101      * Convenience method to apply a mapping function to all of the values of a map,
102      * generating a new map.
103      *
104      * @param source map whose values are to be mapped, or {@code null}
105      * @param mapFunc mapping function
106      * @param defaultValue value to be returned if source is {@code null}
107      * @return a new map, containing mappings of all of the items in the original map
108      */
109     public static <T> Map<String, T> mapMap(Map<String, T> source, Function<T, T> mapFunc,
110                     Map<String, T> defaultValue) {
111         if (source == null) {
112             return defaultValue;
113         }
114
115         Map<String, T> map = new LinkedHashMap<>();
116         for (Entry<String, T> ent : source.entrySet()) {
117             map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
118         }
119
120         return map;
121     }
122
123     /**
124      * Convenience method to apply a mapping function to all of the values of a map,
125      * generating a new map.
126      *
127      * @param source map whose values are to be mapped, or {@code null}
128      * @param mapFunc mapping function
129      * @return a new map, containing mappings of all of the items in the original map,
130      *         or {@code null} if the source is {@code null}
131      */
132     public static <T> Map<String, T> mapMap(Map<String, T> source, Function<T, T> mapFunc) {
133         return mapMap(source, mapFunc, null);
134     }
135
136     /**
137      * Makes a copy of an object using the copy constructor from the object's class.
138      *
139      * @param source object to be copied
140      * @return a copy of the source, or {@code null} if the source is {@code null}
141      * @throws PfModelRuntimeException if the object cannot be copied
142      */
143     public static <T> T makeCopy(T source) {
144         if (source == null) {
145             return null;
146         }
147
148         try {
149             @SuppressWarnings("unchecked")
150             Class<? extends T> clazz = (Class<? extends T>) source.getClass();
151
152             return clazz.getConstructor(clazz).newInstance(source);
153
154         } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
155                         | RuntimeException e) {
156             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
157                             "error copying concept key class: " + source.getClass().getName(), e);
158         }
159     }
160 }