Add Legacy Op Policy Persistence
[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.util.ArrayList;
25 import java.util.List;
26 import java.util.function.Function;
27 import java.util.stream.Collectors;
28
29 /**
30  * Utility class for Policy Framework concept utilities.
31  *
32  * @author Liam Fallon (liam.fallon@est.tech)
33  */
34 public final class PfUtils {
35     private PfUtils() {
36         // Cannot be subclassed
37     }
38
39     /**
40      * Compare two objects using their equals methods, nulls are allowed.
41      *
42      * @param leftObject the first object
43      * @param rightObject the second object
44      * @return a measure of the comparison
45      */
46     public static int compareObjects(final Object leftObject, final Object rightObject) {
47         if (leftObject == null && rightObject == null) {
48             return 0;
49         }
50
51         if (leftObject == null) {
52             return 1;
53         }
54
55         if (rightObject == null) {
56             return -1;
57         }
58
59         if (!leftObject.equals(rightObject)) {
60             return leftObject.hashCode() - rightObject.hashCode();
61         }
62
63         return 0;
64     }
65
66     /**
67      * Convenience method to apply a mapping function to all of the elements of a list,
68      * generating a new list.
69      *
70      * @param source list whose elements are to be mapped, or {@code null}
71      * @param mapFunc mapping function
72      * @return a new list, containing mappings of all of the items in the original list
73      */
74     public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
75         if (source == null) {
76             return new ArrayList<>();
77         }
78
79         return source.stream().map(mapFunc).collect(Collectors.toList());
80     }
81 }