Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / util / SuggestionsPermutation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.util;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 public class SuggestionsPermutation {
27
28   /*
29    * Will return all the unique combinations of the suggestions provided. The order of the
30    * permutation is not taken into account when computing the uniqueness. eg: A list of A,B,C,D will
31    * return [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]]
32    * 
33    * @param list The list to create the unique permutations
34    * 
35    * @return A Arraylist which contains a array list of all possible combinations
36    */
37   public static ArrayList<ArrayList<String>> getUniqueListForSuggestions(
38       List<String> originalList) {
39     ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
40     if (originalList.isEmpty()) {
41       lists.add(new ArrayList<String>());
42       return lists;
43     }
44     List<String> list = new ArrayList<String>(originalList);
45     String head = list.get(0);
46     ArrayList<String> rest = new ArrayList<String>(list.subList(1, list.size()));
47     
48     for (ArrayList<String> activeList : getUniqueListForSuggestions(rest)) {
49       ArrayList<String> newList = new ArrayList<String>();
50       newList.add(head);
51       newList.addAll(activeList);
52       lists.add(newList);
53       lists.add(activeList);
54     }
55     return lists;
56   }
57   
58   public static ArrayList<ArrayList<String>> getNonEmptyUniqueLists(List<String> list){
59     ArrayList<ArrayList<String>> lists = getUniqueListForSuggestions(list);
60     // remove empty list from the power set 
61     for (ArrayList<String> emptyList : lists ){
62       if ( emptyList.isEmpty() ) {
63         lists.remove(emptyList);
64         break;
65       }
66     }
67     return lists;
68   }
69
70   public static List<List<String>> getListPermutations(List<String> list) {
71     List<String> inputList = new ArrayList<String>();
72     inputList.addAll(list);
73     if (inputList.size() == 0) {
74       List<List<String>> result = new ArrayList<List<String>>();
75       result.add(new ArrayList<String>());
76       return result;
77     }
78
79     List<List<String>> listOfLists = new ArrayList<List<String>>();
80
81     String firstElement = inputList.remove(0);
82
83     List<List<String>> recursiveReturn = getListPermutations(inputList);
84     for (List<String> li : recursiveReturn) {
85
86       for (int index = 0; index <= li.size(); index++) {
87         List<String> temp = new ArrayList<String>(li);
88         temp.add(index, firstElement);
89         listOfLists.add(temp);
90       }
91
92     }
93     return listOfLists;
94   }
95
96 }