Adding interfaces in documentation
[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-2018 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.util;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.List;\r
25 \r
26 public class SuggestionsPermutation {\r
27 \r
28   /*\r
29    * Will return all the unique combinations of the suggestions provided. The order of the\r
30    * permutation is not taken into account when computing the uniqueness. eg: A list of A,B,C,D will\r
31    * return [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]]\r
32    * \r
33    * @param list The list to create the unique permutations\r
34    * \r
35    * @return A Arraylist which contains a array list of all possible combinations\r
36    */\r
37   public static ArrayList<ArrayList<String>> getUniqueListForSuggestions(\r
38       List<String> originalList) {\r
39     ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();\r
40     if (originalList.isEmpty()) {\r
41       lists.add(new ArrayList<String>());\r
42       return lists;\r
43     }\r
44     List<String> list = new ArrayList<String>(originalList);\r
45     String head = list.get(0);\r
46     ArrayList<String> rest = new ArrayList<String>(list.subList(1, list.size()));\r
47     \r
48     for (ArrayList<String> activeList : getUniqueListForSuggestions(rest)) {\r
49       ArrayList<String> newList = new ArrayList<String>();\r
50       newList.add(head);\r
51       newList.addAll(activeList);\r
52       lists.add(newList);\r
53       lists.add(activeList);\r
54     }\r
55     return lists;\r
56   }\r
57   \r
58   public static ArrayList<ArrayList<String>> getNonEmptyUniqueLists(List<String> list){\r
59     ArrayList<ArrayList<String>> lists = getUniqueListForSuggestions(list);\r
60     // remove empty list from the power set \r
61     for (ArrayList<String> emptyList : lists ){\r
62       if ( emptyList.isEmpty() ) {\r
63         lists.remove(emptyList);\r
64         break;\r
65       }\r
66     }\r
67     return lists;\r
68   }\r
69 \r
70   public static List<List<String>> getListPermutations(List<String> list) {\r
71     List<String> inputList = new ArrayList<String>();\r
72     inputList.addAll(list);\r
73     if (inputList.size() == 0) {\r
74       List<List<String>> result = new ArrayList<List<String>>();\r
75       result.add(new ArrayList<String>());\r
76       return result;\r
77     }\r
78 \r
79     List<List<String>> listOfLists = new ArrayList<List<String>>();\r
80 \r
81     String firstElement = inputList.remove(0);\r
82 \r
83     List<List<String>> recursiveReturn = getListPermutations(inputList);\r
84     for (List<String> li : recursiveReturn) {\r
85 \r
86       for (int index = 0; index <= li.size(); index++) {\r
87         List<String> temp = new ArrayList<String>(li);\r
88         temp.add(index, firstElement);\r
89         listOfLists.add(temp);\r
90       }\r
91 \r
92     }\r
93     return listOfLists;\r
94   }\r
95 \r
96 }\r