190b270eaa12272858732968875bd8968cd7f9ba
[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
27 /**
28  * This is common utility class with static methods for handling collections.
29  *
30  * @author Liam Fallon (liam.fallon@ericsson.com)
31  */
32 public class CollectionUtils {
33     /**
34      * Private constructor used to prevent sub class instantiation.
35      */
36     private CollectionUtils() {
37     }
38
39     /**
40      * Compare two lists, checks for equality, then for equality on members.
41      *
42      * @param <T> The type of the lists being compared
43      * @param leftList The leftmost List
44      * @param rightList The rightmost list
45      * @return an integer indicating how different the lists are
46      */
47     public static <T> int compareLists(final List<? extends Comparable<T>> leftList,
48                     final List<? extends Comparable<T>> rightList) {
49         // Check for nulls
50         if (leftList == null && rightList == null) {
51             return 0;
52         }
53         if (leftList != null && rightList == null) {
54             return -1;
55         }
56         if (leftList == null) {
57             return 1;
58         }
59
60         // Check for equality
61         if (leftList.equals(rightList)) {
62             return 0;
63         }
64
65         return compareListEntries(leftList, rightList);
66     }
67
68     /**
69      * Compare two lists for equality on members.
70      *
71      * @param <T> The type of the lists being compared
72      * @param leftList The leftmost List
73      * @param rightList The rightmost list
74      * @return an integer indicating how different the lists are
75      */
76     private static <T> int compareListEntries(final List<? extends Comparable<T>> leftList,
77                     final List<? extends Comparable<T>> rightList) {
78
79         // Iterate down the lists till we find a difference
80         final ListIterator<?> leftIterator = leftList.listIterator();
81         final ListIterator<?> rightIterator = rightList.listIterator();
82
83         while (true) {
84             // Check the iterators
85             if (!leftIterator.hasNext() && !rightIterator.hasNext()) {
86                 return 0;
87             }
88             if (leftIterator.hasNext() && !rightIterator.hasNext()) {
89                 return -1;
90             }
91             if (!leftIterator.hasNext() && rightIterator.hasNext()) {
92                 return 1;
93             }
94
95             // Get the next objects
96             @SuppressWarnings("unchecked")
97             final T leftObject = (T) leftIterator.next();
98             @SuppressWarnings("unchecked")
99             final T rightObject = (T) rightIterator.next();
100
101             // Compare the objects
102             @SuppressWarnings("unchecked")
103             final int comparisonResult = ((Comparable<T>) leftObject).compareTo(rightObject);
104
105             // Check the comparison result
106             if (comparisonResult != 0) {
107                 return comparisonResult;
108             }
109         }
110     }
111 }