Adding support for external microservice
[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   public static ArrayList<ArrayList<String>> getUniqueListForSuggestions(
42       List<String> originalList) {
43     ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
44     if (originalList.isEmpty()) {
45       lists.add(new ArrayList<String>());
46       return lists;
47     }
48     List<String> list = new ArrayList<String>(originalList);
49     String head = list.get(0);
50     ArrayList<String> rest = new ArrayList<String>(list.subList(1, list.size()));
51     
52     for (ArrayList<String> activeList : getUniqueListForSuggestions(rest)) {
53       ArrayList<String> newList = new ArrayList<String>();
54       newList.add(head);
55       newList.addAll(activeList);
56       lists.add(newList);
57       lists.add(activeList);
58     }
59     return lists;
60   }
61   
62   public static ArrayList<ArrayList<String>> getNonEmptyUniqueLists(List<String> list){
63     ArrayList<ArrayList<String>> lists = getUniqueListForSuggestions(list);
64     // remove empty list from the power set 
65     for (ArrayList<String> emptyList : lists ){
66       if ( emptyList.isEmpty() ) {
67         lists.remove(emptyList);
68         break;
69       }
70     }
71     return lists;
72   }
73
74   public static List<List<String>> getListPermutations(List<String> list) {
75     List<String> inputList = new ArrayList<String>();
76     inputList.addAll(list);
77     if (inputList.size() == 0) {
78       List<List<String>> result = new ArrayList<List<String>>();
79       result.add(new ArrayList<String>());
80       return result;
81     }
82
83     List<List<String>> listOfLists = new ArrayList<List<String>>();
84
85     String firstElement = inputList.remove(0);
86
87     List<List<String>> recursiveReturn = getListPermutations(inputList);
88     for (List<String> li : recursiveReturn) {
89
90       for (int index = 0; index <= li.size(); index++) {
91         List<String> temp = new ArrayList<String>(li);
92         temp.add(index, firstElement);
93         listOfLists.add(temp);
94       }
95
96     }
97     return listOfLists;
98   }
99
100 }