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