2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorsoftwareproduct.utils;
19 import java.util.HashMap;
20 import java.util.HashSet;
24 public class ComponentDependencyTracker {
25 private final Map<String, Set<String>> store = new HashMap<>();
30 * @param dependent the dependent
31 * @param dependsOn the depends on
33 public void addDependency(String dependent, String dependsOn) {
34 if (dependent != null && dependsOn != null && dependent.trim().length() > 0 && dependsOn.trim()
36 Set<String> dependsOnList = store
37 .computeIfAbsent(dependent.toLowerCase(), k -> new HashSet<>());
38 dependsOnList.add(dependsOn.toLowerCase());
43 * Is cyclic dependency present boolean.
47 public boolean isCyclicDependencyPresent() {
48 Set<Map.Entry<String, Set<String>>> entries = store.entrySet();
49 for (Map.Entry<String, Set<String>> entry : entries) {
50 for (String dependentOn : entry.getValue()) {
51 if (!entry.getKey().equals(dependentOn) && isCyclicDependencyPresent(entry.getKey(),
60 private boolean isCyclicDependencyPresent(String root, String dependentOn) {
61 Set<String> dependentOnList = store.get(dependentOn);
62 if (dependentOnList != null && dependentOnList.contains(root)) {
64 } else if (dependentOnList != null) {
65 for (String item : dependentOnList) {
66 return isCyclicDependencyPresent(root, item);