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