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
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.apex.model.utilities;
24 import java.util.List;
25 import java.util.ListIterator;
28 * This is common utility class with static methods for handling collections.
30 * @author Liam Fallon (liam.fallon@ericsson.com)
32 public class CollectionUtils {
34 * Private constructor used to prevent sub class instantiation.
36 private CollectionUtils() {
40 * Compare two lists, checks for equality, then for equality on members.
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
47 public static <T> int compareLists(final List<? extends Comparable<T>> leftList,
48 final List<? extends Comparable<T>> rightList) {
50 if (leftList == null && rightList == null) {
53 if (leftList != null && rightList == null) {
56 if (leftList == null) {
61 if (leftList.equals(rightList)) {
65 return compareListEntries(leftList, rightList);
69 * Compare two lists for equality on members.
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
76 private static <T> int compareListEntries(final List<? extends Comparable<T>> leftList,
77 final List<? extends Comparable<T>> rightList) {
79 // Iterate down the lists till we find a difference
80 final ListIterator<?> leftIterator = leftList.listIterator();
81 final ListIterator<?> rightIterator = rightList.listIterator();
84 // Check the iterators
85 if (!leftIterator.hasNext() && !rightIterator.hasNext()) {
88 if (leftIterator.hasNext() && !rightIterator.hasNext()) {
91 if (!leftIterator.hasNext() && rightIterator.hasNext()) {
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();
101 // Compare the objects
102 @SuppressWarnings("unchecked")
103 final int comparisonResult = ((Comparable<T>) leftObject).compareTo(rightObject);
105 // Check the comparison result
106 if (comparisonResult != 0) {
107 return comparisonResult;