package com.google.firebase.components; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; /* loaded from: classes.dex */ class CycleDetector { /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static class Dep { private final Class anInterface; private final boolean set; private Dep(Class cls, boolean z) { this.anInterface = cls; this.set = z; } public boolean equals(Object obj) { if (!(obj instanceof Dep)) { return false; } Dep dep = (Dep) obj; return dep.anInterface.equals(this.anInterface) && dep.set == this.set; } public int hashCode() { return ((this.anInterface.hashCode() ^ 1000003) * 1000003) ^ Boolean.valueOf(this.set).hashCode(); } } CycleDetector() { } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static class ComponentNode { private final Component component; private final Set dependencies = new HashSet(); private final Set dependents = new HashSet(); ComponentNode(Component component) { this.component = component; } void addDependency(ComponentNode componentNode) { this.dependencies.add(componentNode); } void addDependent(ComponentNode componentNode) { this.dependents.add(componentNode); } void removeDependent(ComponentNode componentNode) { this.dependents.remove(componentNode); } boolean isRoot() { return this.dependents.isEmpty(); } boolean isLeaf() { return this.dependencies.isEmpty(); } Set getDependencies() { return this.dependencies; } Component getComponent() { return this.component; } } /* JADX INFO: Access modifiers changed from: package-private */ public static void detect(List> list) { Set graph = toGraph(list); Set roots = getRoots(graph); int i = 0; while (!roots.isEmpty()) { ComponentNode next = roots.iterator().next(); roots.remove(next); i++; for (ComponentNode componentNode : next.getDependencies()) { componentNode.removeDependent(next); if (componentNode.isRoot()) { roots.add(componentNode); } } } if (i == list.size()) { return; } ArrayList arrayList = new ArrayList(); for (ComponentNode componentNode2 : graph) { if (!componentNode2.isRoot() && !componentNode2.isLeaf()) { arrayList.add(componentNode2.getComponent()); } } throw new DependencyCycleException(arrayList); } private static Set toGraph(List> list) { Set set; HashMap hashMap = new HashMap(list.size()); Iterator> it = list.iterator(); while (true) { if (it.hasNext()) { Component next = it.next(); ComponentNode componentNode = new ComponentNode(next); for (Class cls : next.getProvidedInterfaces()) { Dep dep = new Dep(cls, !next.isValue()); if (!hashMap.containsKey(dep)) { hashMap.put(dep, new HashSet()); } Set set2 = (Set) hashMap.get(dep); if (!set2.isEmpty() && !dep.set) { throw new IllegalArgumentException(String.format("Multiple components provide %s.", cls)); } set2.add(componentNode); } } else { Iterator it2 = hashMap.values().iterator(); while (it2.hasNext()) { for (ComponentNode componentNode2 : (Set) it2.next()) { for (Dependency dependency : componentNode2.getComponent().getDependencies()) { if (dependency.isDirectInjection() && (set = (Set) hashMap.get(new Dep(dependency.getInterface(), dependency.isSet()))) != null) { for (ComponentNode componentNode3 : set) { componentNode2.addDependency(componentNode3); componentNode3.addDependent(componentNode2); } } } } } HashSet hashSet = new HashSet(); Iterator it3 = hashMap.values().iterator(); while (it3.hasNext()) { hashSet.addAll((Set) it3.next()); } return hashSet; } } } private static Set getRoots(Set set) { HashSet hashSet = new HashSet(); for (ComponentNode componentNode : set) { if (componentNode.isRoot()) { hashSet.add(componentNode); } } return hashSet; } }