Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / openecomp / 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.openecomp.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.\r
32    * The order of the permutation is not taken into account when computing\r
33    * the uniqueness.\r
34    * eg: A list of A,B,C,D will return\r
35    * [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]]\r
36    * \r
37    * @param list The list to create the unique permutations\r
38    * @return    A Arraylist which contains a array list of all possible combinations\r
39    */\r
40   @SuppressWarnings("serial")\r
41   public ArrayList<ArrayList<String>> getSuggestionsPermutation(List<String> list) {\r
42     List<String> statusList = new ArrayList<>(list);\r
43     List<String> dupStatusList;\r
44     ArrayList<ArrayList<String>> uniqueList = new ArrayList<ArrayList<String>>();\r
45     int mainLoopIndexCounter = 0;\r
46     for (String status : statusList) {\r
47       // Add the single entity subset\r
48       uniqueList.add(new ArrayList<String>() {\r
49         {\r
50           add(status);\r
51         }\r
52       });\r
53       // Remove all the elements to left till the current index\r
54       dupStatusList = truncateListUntill(statusList, mainLoopIndexCounter);\r
55 \r
56       while (dupStatusList.size() > 0) {\r
57         ArrayList<String> suggListInIterate= new ArrayList<>();\r
58         suggListInIterate.add(status);\r
59         for (String dupStatus : dupStatusList) {\r
60           suggListInIterate.add(dupStatus);\r
61         }\r
62         uniqueList.add(suggListInIterate);\r
63         dupStatusList.remove(0);\r
64       }\r
65       mainLoopIndexCounter++;\r
66     }\r
67     return uniqueList;\r
68 \r
69   }\r
70 \r
71   private List<String> truncateListUntill(List<String> lists, int index) {\r
72     List<String> truncatedList = new ArrayList<>(lists);\r
73     int counter = 0;\r
74     while (counter <= index) {\r
75       truncatedList.remove(0);\r
76       counter++;\r
77     }\r
78     return truncatedList;\r
79   }\r
80 }\r