Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / util / SuggestionsPermutation.java
diff --git a/src/main/java/org/openecomp/sparky/util/SuggestionsPermutation.java b/src/main/java/org/openecomp/sparky/util/SuggestionsPermutation.java
new file mode 100644 (file)
index 0000000..876b2f4
--- /dev/null
@@ -0,0 +1,82 @@
+/**\r
+ * ============LICENSE_START===================================================\r
+ * SPARKY (AAI UI service)\r
+ * ============================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property.\r
+ * Copyright © 2017 Amdocs\r
+ * All rights reserved.\r
+ * ============================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=====================================================\r
+ *\r
+ * ECOMP and OpenECOMP are trademarks\r
+ * and service marks of AT&T Intellectual Property.\r
+ */\r
+package org.openecomp.sparky.util;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+public class SuggestionsPermutation {\r
+  \r
+  /*\r
+   * Will return all the unique combinations of the suggestions provided.\r
+   * The order of the permutation is not taken into account when computing\r
+   * the uniqueness.\r
+   * eg: A list of A,B,C,D will return\r
+   * [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]]\r
+   * \r
+   * @param list The list to create the unique permutations\r
+   * @return    A Arraylist which contains a array list of all possible combinations\r
+   */\r
+  @SuppressWarnings("serial")\r
+  public ArrayList<ArrayList<String>> getSuggestionsPermutation(List<String> list) {\r
+    List<String> statusList = new ArrayList<>(list);\r
+    List<String> dupStatusList;\r
+    ArrayList<ArrayList<String>> uniqueList = new ArrayList<ArrayList<String>>();\r
+    int mainLoopIndexCounter = 0;\r
+    for (String status : statusList) {\r
+      // Add the single entity subset\r
+      uniqueList.add(new ArrayList<String>() {\r
+        {\r
+          add(status);\r
+        }\r
+      });\r
+      // Remove all the elements to left till the current index\r
+      dupStatusList = truncateListUntill(statusList, mainLoopIndexCounter);\r
+\r
+      while (dupStatusList.size() > 0) {\r
+        ArrayList<String> suggListInIterate= new ArrayList<>();\r
+        suggListInIterate.add(status);\r
+        for (String dupStatus : dupStatusList) {\r
+          suggListInIterate.add(dupStatus);\r
+        }\r
+        uniqueList.add(suggListInIterate);\r
+        dupStatusList.remove(0);\r
+      }\r
+      mainLoopIndexCounter++;\r
+    }\r
+    return uniqueList;\r
+\r
+  }\r
+\r
+  private List<String> truncateListUntill(List<String> lists, int index) {\r
+    List<String> truncatedList = new ArrayList<>(lists);\r
+    int counter = 0;\r
+    while (counter <= index) {\r
+      truncatedList.remove(0);\r
+      counter++;\r
+    }\r
+    return truncatedList;\r
+  }\r
+}\r