Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / util / SuggestionsPermutation.java
1 /**\r
2  * ============LICENSE_START===================================================\r
3  * SPARKY (AAI UI service)\r
4  * ============================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property.\r
6  * Copyright © 2017 Amdocs\r
7  * All rights reserved.\r
8  * ============================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *      http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  * ============LICENSE_END=====================================================\r
21  *\r
22  * ECOMP and OpenECOMP are trademarks\r
23  * and service marks of AT&T Intellectual Property.\r
24  */\r
25 package org.openecomp.sparky.util;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 \r
30 public class SuggestionsPermutation {\r
31   \r
32   /*\r
33    * Will return all the unique combinations of the suggestions provided.\r
34    * The order of the permutation is not taken into account when computing\r
35    * the uniqueness.\r
36    * eg: A list of A,B,C,D will return\r
37    * [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]]\r
38    * \r
39    * @param list The list to create the unique permutations\r
40    * @return    A Arraylist which contains a array list of all possible combinations\r
41    */\r
42   @SuppressWarnings("serial")\r
43   public ArrayList<ArrayList<String>> getSuggestionsPermutation(List<String> list) {\r
44     List<String> statusList = new ArrayList<>(list);\r
45     List<String> dupStatusList;\r
46     ArrayList<ArrayList<String>> uniqueList = new ArrayList<ArrayList<String>>();\r
47     int mainLoopIndexCounter = 0;\r
48     for (String status : statusList) {\r
49       // Add the single entity subset\r
50       uniqueList.add(new ArrayList<String>() {\r
51         {\r
52           add(status);\r
53         }\r
54       });\r
55       // Remove all the elements to left till the current index\r
56       dupStatusList = truncateListUntill(statusList, mainLoopIndexCounter);\r
57 \r
58       while (dupStatusList.size() > 0) {\r
59         ArrayList<String> suggListInIterate= new ArrayList<>();\r
60         suggListInIterate.add(status);\r
61         for (String dupStatus : dupStatusList) {\r
62           suggListInIterate.add(dupStatus);\r
63         }\r
64         uniqueList.add(suggListInIterate);\r
65         dupStatusList.remove(0);\r
66       }\r
67       mainLoopIndexCounter++;\r
68     }\r
69     return uniqueList;\r
70 \r
71   }\r
72 \r
73   private List<String> truncateListUntill(List<String> lists, int index) {\r
74     List<String> truncatedList = new ArrayList<>(lists);\r
75     int counter = 0;\r
76     while (counter <= index) {\r
77       truncatedList.remove(0);\r
78       counter++;\r
79     }\r
80     return truncatedList;\r
81   }\r
82 }\r