2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.util;
27 import java.util.ArrayList;
28 import java.util.List;
30 public class SuggestionsPermutation {
33 * Will return all the unique combinations of the suggestions provided. The order of the
34 * permutation is not taken into account when computing the uniqueness. eg: A list of A,B,C,D will
35 * return [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]]
37 * @param list The list to create the unique permutations
39 * @return A Arraylist which contains a array list of all possible combinations
41 public static ArrayList<ArrayList<String>> getUniqueListForSuggestions(
42 List<String> originalList) {
43 ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
44 if (originalList.isEmpty()) {
45 lists.add(new ArrayList<String>());
48 List<String> list = new ArrayList<String>(originalList);
49 String head = list.get(0);
50 ArrayList<String> rest = new ArrayList<String>(list.subList(1, list.size()));
52 for (ArrayList<String> activeList : getUniqueListForSuggestions(rest)) {
53 ArrayList<String> newList = new ArrayList<String>();
55 newList.addAll(activeList);
57 lists.add(activeList);
62 public static ArrayList<ArrayList<String>> getNonEmptyUniqueLists(List<String> list){
63 ArrayList<ArrayList<String>> lists = getUniqueListForSuggestions(list);
64 // remove empty list from the power set
65 for (ArrayList<String> emptyList : lists ){
66 if ( emptyList.isEmpty() ) {
67 lists.remove(emptyList);
74 public static List<List<String>> getListPermutations(List<String> list) {
75 List<String> inputList = new ArrayList<String>();
76 inputList.addAll(list);
77 if (inputList.size() == 0) {
78 List<List<String>> result = new ArrayList<List<String>>();
79 result.add(new ArrayList<String>());
83 List<List<String>> listOfLists = new ArrayList<List<String>>();
85 String firstElement = inputList.remove(0);
87 List<List<String>> recursiveReturn = getListPermutations(inputList);
88 for (List<String> li : recursiveReturn) {
90 for (int index = 0; index <= li.size(); index++) {
91 List<String> temp = new ArrayList<String>(li);
92 temp.add(index, firstElement);
93 listOfLists.add(temp);