Address more sonars in apex-pdp
[policy/apex-pdp.git] / model / utilities / src / main / java / org / onap / policy / apex / model / utilities / CollectionUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 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.apex.model.utilities;
23
24 import java.util.List;
25 import java.util.ListIterator;
26 import lombok.AccessLevel;
27 import lombok.NoArgsConstructor;
28
29 /**
30  * This is common utility class with static methods for handling collections.
31  *
32  * @author Liam Fallon (liam.fallon@ericsson.com)
33  */
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public final class CollectionUtils {
36
37     /**
38      * Compare two lists, checks for equality, then for equality on members.
39      *
40      * @param <T> The type of the lists being compared
41      * @param leftList The leftmost List
42      * @param rightList The rightmost list
43      * @return an integer indicating how different the lists are
44      */
45     public static <T> int compareLists(final List<? extends Comparable<T>> leftList,
46                     final List<? extends Comparable<T>> rightList) {
47         // Check for nulls
48         if (leftList == null && rightList == null) {
49             return 0;
50         }
51         if (leftList != null && rightList == null) {
52             return -1;
53         }
54         if (leftList == null) {
55             return 1;
56         }
57
58         // Check for equality
59         if (leftList.equals(rightList)) {
60             return 0;
61         }
62
63         return compareListEntries(leftList, rightList);
64     }
65
66     /**
67      * Compare two lists for equality on members.
68      *
69      * @param <T> The type of the lists being compared
70      * @param leftList The leftmost List
71      * @param rightList The rightmost list
72      * @return an integer indicating how different the lists are
73      */
74     private static <T> int compareListEntries(final List<? extends Comparable<T>> leftList,
75                     final List<? extends Comparable<T>> rightList) {
76
77         // Iterate down the lists till we find a difference
78         final ListIterator<?> leftIterator = leftList.listIterator();
79         final ListIterator<?> rightIterator = rightList.listIterator();
80
81         while (true) {
82             // Check the iterators
83             if (!leftIterator.hasNext() && !rightIterator.hasNext()) {
84                 return 0;
85             }
86             if (leftIterator.hasNext() && !rightIterator.hasNext()) {
87                 return -1;
88             }
89             if (!leftIterator.hasNext() && rightIterator.hasNext()) {
90                 return 1;
91             }
92
93             // Get the next objects
94             @SuppressWarnings("unchecked")
95             final var leftObject = (T) leftIterator.next();
96             @SuppressWarnings("unchecked")
97             final var rightObject = (T) rightIterator.next();
98
99             // Compare the objects
100             @SuppressWarnings("unchecked")
101             final int comparisonResult = ((Comparable<T>) leftObject).compareTo(rightObject);
102
103             // Check the comparison result
104             if (comparisonResult != 0) {
105                 return comparisonResult;
106             }
107         }
108     }
109 }