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;
26 import lombok.AccessLevel;
27 import lombok.NoArgsConstructor;
30 * This is common utility class with static methods for handling collections.
32 * @author Liam Fallon (liam.fallon@ericsson.com)
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public final class CollectionUtils {
38 * Compare two lists, checks for equality, then for equality on members.
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
45 public static <T> int compareLists(final List<? extends Comparable<T>> leftList,
46 final List<? extends Comparable<T>> rightList) {
48 if (leftList == null && rightList == null) {
51 if (leftList != null && rightList == null) {
54 if (leftList == null) {
59 if (leftList.equals(rightList)) {
63 return compareListEntries(leftList, rightList);
67 * Compare two lists for equality on members.
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
74 private static <T> int compareListEntries(final List<? extends Comparable<T>> leftList,
75 final List<? extends Comparable<T>> rightList) {
77 // Iterate down the lists till we find a difference
78 final ListIterator<?> leftIterator = leftList.listIterator();
79 final ListIterator<?> rightIterator = rightList.listIterator();
82 // Check the iterators
83 if (!leftIterator.hasNext() && !rightIterator.hasNext()) {
86 if (leftIterator.hasNext() && !rightIterator.hasNext()) {
89 if (!leftIterator.hasNext() && rightIterator.hasNext()) {
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();
99 // Compare the objects
100 @SuppressWarnings("unchecked")
101 final int comparisonResult = ((Comparable<T>) leftObject).compareTo(rightObject);
103 // Check the comparison result
104 if (comparisonResult != 0) {
105 return comparisonResult;