2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.Collection;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.function.Function;
31 import java.util.stream.Collectors;
32 import javax.ws.rs.core.Response;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.collections4.MapUtils;
37 * Utility class for Policy Framework concept utilities.
39 * @author Liam Fallon (liam.fallon@est.tech)
41 public final class PfUtils {
43 // Cannot be subclassed
47 * Compare two objects using their equals methods, nulls are allowed.
49 * @param leftObject the first object
50 * @param rightObject the second object
51 * @return a measure of the comparison
53 public static int compareObjects(final Object leftObject, final Object rightObject) {
54 if (leftObject == null && rightObject == null) {
58 if (leftObject == null) {
62 if (rightObject == null) {
66 if (!leftObject.equals(rightObject)) {
67 return leftObject.hashCode() - rightObject.hashCode();
74 * Compare two collections of the same type, nulls are allowed.
76 * @param leftCollection the first collection
77 * @param rightCollection the second collection
78 * @return a measure of the comparison
80 public static <T> int compareCollections(final Collection<T> leftCollection, final Collection<T> rightCollection) {
81 if (CollectionUtils.isEmpty(leftCollection) && CollectionUtils.isEmpty(rightCollection)) {
85 return compareObjects(leftCollection, rightCollection);
90 * Compare two maps of the same type, nulls are allowed.
92 * @param leftMap the first map
93 * @param rightMap the second map
94 * @return a measure of the comparison
96 public static <K, V> int compareMaps(final Map<K, V> leftMap, final Map<K, V> rightMap) {
97 if (MapUtils.isEmpty(leftMap) && MapUtils.isEmpty(rightMap)) {
101 return compareObjects(leftMap, rightMap);
105 * Convenience method to apply a mapping function to all of the elements of a list, generating a new list.
107 * @param source list whose elements are to be mapped, or {@code null}
108 * @param mapFunc mapping function
109 * @param defaultValue value to be returned if source is {@code null}
110 * @return a new list, containing mappings of all of the items in the original list
112 public static <T, R> List<R> mapList(List<T> source, Function<T, R> mapFunc, List<R> defaultValue) {
113 if (source == null) {
117 return source.stream().map(mapFunc).collect(Collectors.toList());
121 * Convenience method to apply a mapping function to all of the elements of a list, generating a new list.
123 * @param source list whose elements are to be mapped, or {@code null}
124 * @param mapFunc mapping function
125 * @return a new list, containing mappings of all of the items in the original list, or {@code null} if the source
128 public static <T, R> List<R> mapList(List<T> source, Function<T, R> mapFunc) {
129 return mapList(source, mapFunc, null);
133 * Convenience method to apply a mapping function to all of the values of a map, generating a new map.
135 * @param source map whose values are to be mapped, or {@code null}
136 * @param mapFunc mapping function
137 * @param defaultValue value to be returned if source is {@code null}
138 * @return a new map, containing mappings of all of the items in the original map
140 public static <K, T, R> Map<K, R> mapMap(Map<K, T> source, Function<T, R> mapFunc,
141 Map<K, R> defaultValue) {
142 if (source == null) {
146 Map<K, R> map = new LinkedHashMap<>();
147 for (Entry<K, T> ent : source.entrySet()) {
148 map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
155 * Convenience method to apply a mapping function to all of the values of a map, generating a new map.
157 * @param source map whose values are to be mapped, or {@code null}
158 * @param mapFunc mapping function
159 * @return a new map, containing mappings of all of the items in the original map, or {@code null} if the source is
162 public static <K, T, R> Map<K, R> mapMap(Map<K, T> source, Function<T, R> mapFunc) {
163 return mapMap(source, mapFunc, null);
167 * Makes a copy of an object using the copy constructor from the object's class.
169 * @param source object to be copied
170 * @return a copy of the source, or {@code null} if the source is {@code null}
171 * @throws PfModelRuntimeException if the object cannot be copied
173 public static <T> T makeCopy(T source) {
174 if (source == null) {
179 @SuppressWarnings("unchecked")
180 Class<? extends T> clazz = (Class<? extends T>) source.getClass();
182 return clazz.getConstructor(clazz).newInstance(source);
184 } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
185 | RuntimeException e) {
186 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
187 "error copying concept key class: " + source.getClass().getName(), e);