d5b70d78ad75a057f407dcd63a01c2e4d07f6d9d
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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, generating a new list.
72      *
73      * @param source list whose elements are to be mapped, or {@code null}
74      * @param mapFunc mapping function
75      * @param defaultValue value to be returned if source is {@code null}
76      * @return a new list, containing mappings of all of the items in the original list
77      */
78     public static <T, R> List<R> mapList(List<T> source, Function<T, R> mapFunc, List<R> defaultValue) {
79         if (source == null) {
80             return defaultValue;
81         }
82
83         return source.stream().map(mapFunc).collect(Collectors.toList());
84     }
85
86     /**
87      * Convenience method to apply a mapping function to all of the elements of a list, generating a new list.
88      *
89      * @param source list whose elements are to be mapped, or {@code null}
90      * @param mapFunc mapping function
91      * @return a new list, containing mappings of all of the items in the original list, or {@code null} if the source
92      *         is {@code null}
93      */
94     public static <T, R> List<R> mapList(List<T> source, Function<T, R> mapFunc) {
95         return mapList(source, mapFunc, null);
96     }
97
98     /**
99      * Convenience method to apply a mapping function to all of the values of a map, generating a new map.
100      *
101      * @param source map whose values are to be mapped, or {@code null}
102      * @param mapFunc mapping function
103      * @param defaultValue value to be returned if source is {@code null}
104      * @return a new map, containing mappings of all of the items in the original map
105      */
106     public static <T, R> Map<String, R> mapMap(Map<String, T> source, Function<T, R> mapFunc,
107             Map<String, R> defaultValue) {
108         if (source == null) {
109             return defaultValue;
110         }
111
112         Map<String, R> map = new LinkedHashMap<>();
113         for (Entry<String, T> ent : source.entrySet()) {
114             map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
115         }
116
117         return map;
118     }
119
120     /**
121      * Convenience method to apply a mapping function to all of the values of a map, generating a new map.
122      *
123      * @param source map whose values are to be mapped, or {@code null}
124      * @param mapFunc mapping function
125      * @return a new map, containing mappings of all of the items in the original map, or {@code null} if the source is
126      *         {@code null}
127      */
128     public static <T, R> Map<String, R> mapMap(Map<String, T> source, Function<T, R> mapFunc) {
129         return mapMap(source, mapFunc, null);
130     }
131
132     /**
133      * Makes a copy of an object using the copy constructor from the object's class.
134      *
135      * @param source object to be copied
136      * @return a copy of the source, or {@code null} if the source is {@code null}
137      * @throws PfModelRuntimeException if the object cannot be copied
138      */
139     public static <T> T makeCopy(T source) {
140         if (source == null) {
141             return null;
142         }
143
144         try {
145             @SuppressWarnings("unchecked")
146             Class<? extends T> clazz = (Class<? extends T>) source.getClass();
147
148             return clazz.getConstructor(clazz).newInstance(source);
149
150         } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
151                 | RuntimeException e) {
152             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
153                     "error copying concept key class: " + source.getClass().getName(), e);
154         }
155     }
156 }