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