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
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.LinkedHashMap;
26 import java.util.List;
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;
34 * Utility class for Policy Framework concept utilities.
36 * @author Liam Fallon (liam.fallon@est.tech)
38 public final class PfUtils {
40 // Cannot be subclassed
44 * Compare two objects using their equals methods, nulls are allowed.
46 * @param leftObject the first object
47 * @param rightObject the second object
48 * @return a measure of the comparison
50 public static int compareObjects(final Object leftObject, final Object rightObject) {
51 if (leftObject == null && rightObject == null) {
55 if (leftObject == null) {
59 if (rightObject == null) {
63 if (!leftObject.equals(rightObject)) {
64 return leftObject.hashCode() - rightObject.hashCode();
71 * Convenience method to apply a mapping function to all of the elements of a list,
72 * generating a new list.
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
79 public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc, List<T> defaultValue) {
84 return source.stream().map(mapFunc).collect(Collectors.toList());
88 * Convenience method to apply a mapping function to all of the elements of a list,
89 * generating a new list.
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}
96 public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
97 return mapList(source, mapFunc, null);
101 * Convenience method to apply a mapping function to all of the values of a map,
102 * generating a new map.
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
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) {
115 Map<String, T> map = new LinkedHashMap<>();
116 for (Entry<String, T> ent : source.entrySet()) {
117 map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
124 * Convenience method to apply a mapping function to all of the values of a map,
125 * generating a new map.
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}
132 public static <T> Map<String, T> mapMap(Map<String, T> source, Function<T, T> mapFunc) {
133 return mapMap(source, mapFunc, null);
137 * Makes a copy of an object using the copy constructor from the object's class.
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
143 public static <T> T makeCopy(T source) {
144 if (source == null) {
149 @SuppressWarnings("unchecked")
150 Class<? extends T> clazz = (Class<? extends T>) source.getClass();
152 return clazz.getConstructor(clazz).newInstance(source);
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);