Merge "Not logged or rethrow this exception."
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / util / SearchSuggestionPermutation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.datarouter.util;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 public class SearchSuggestionPermutation {
29     /*
30      * Will return all the unique combinations of the suggestions provided.
31      * The order of the permutation is not taken into account when computing
32      * the uniqueness.
33      * e.g.: A list of A,B,C,D will return
34      * [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]]
35      *
36      * @param list The list of statuses to create permutations of
37      * @return     A list which contains a array list of all possible combinations
38      */
39     public static ArrayList<ArrayList<String>> getUniqueListForSuggestions(
40             List<String> originalList) {
41         ArrayList<ArrayList<String>> lists = new ArrayList<>();
42         if (originalList.isEmpty()) {
43             lists.add(new ArrayList<String>());
44             return lists;
45         }
46         List<String> list = new ArrayList<>(originalList);
47         String head = list.get(0);
48         ArrayList<String> rest = new ArrayList<>(list.subList(1, list.size()));
49
50         for (ArrayList<String> activeList : getUniqueListForSuggestions(rest)) {
51             ArrayList<String> newList = new ArrayList<>();
52             newList.add(head);
53             newList.addAll(activeList);
54             lists.add(newList);
55             lists.add(activeList);
56         }
57         return lists;
58     }
59
60     public static ArrayList<ArrayList<String>> getNonEmptyUniqueLists(List<String> list) {
61         ArrayList<ArrayList<String>> lists = getUniqueListForSuggestions(list);
62         // remove empty list from the power set
63         for (ArrayList<String> emptyList : lists) {
64             if (emptyList.isEmpty()) {
65                 lists.remove(emptyList);
66                 break;
67             }
68         }
69         return lists;
70     }
71
72     public static List<List<String>> getListPermutations(List<String> list) {
73         List<String> inputList = new ArrayList<>();
74         inputList.addAll(list);
75         if (inputList.isEmpty()) {
76             List<List<String>> result = new ArrayList<>();
77             result.add(new ArrayList<String>());
78             return result;
79         }
80
81         List<List<String>> listOfLists = new ArrayList<>();
82
83         String firstElement = inputList.remove(0);
84
85         List<List<String>> recursiveReturn = getListPermutations(inputList);
86         for (List<String> li : recursiveReturn) {
87
88             for (int index = 0; index <= li.size(); index++) {
89                 List<String> temp = new ArrayList<>(li);
90                 temp.add(index, firstElement);
91                 listOfLists.add(temp);
92             }
93
94         }
95         return listOfLists;
96     }
97
98 }