what-the-bank/sources/com/google/firebase/components/ComponentRuntime.java

346 lines
14 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.firebase.components;
import com.google.firebase.components.ComponentRuntime;
import com.google.firebase.dynamicloading.ComponentLoader;
import com.google.firebase.events.Publisher;
import com.google.firebase.events.Subscriber;
import com.google.firebase.inject.Deferred;
import com.google.firebase.inject.Provider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
/* loaded from: classes.dex */
public class ComponentRuntime extends AbstractComponentContainer implements ComponentLoader {
private static final Provider<Set<Object>> EMPTY_PROVIDER = new Provider() { // from class: com.google.firebase.components.ComponentRuntime$$ExternalSyntheticLambda4
@Override // com.google.firebase.inject.Provider
public final Object get() {
Set emptySet;
emptySet = Collections.emptySet();
return emptySet;
}
};
private final ComponentRegistrarProcessor componentRegistrarProcessor;
private final Map<Component<?>, Provider<?>> components;
private final AtomicReference<Boolean> eagerComponentsInitializedWith;
private final EventBus eventBus;
private final Map<Class<?>, Provider<?>> lazyInstanceMap;
private final Map<Class<?>, LazySet<?>> lazySetMap;
private final List<Provider<ComponentRegistrar>> unprocessedRegistrarProviders;
@Override // com.google.firebase.components.AbstractComponentContainer, com.google.firebase.components.ComponentContainer
public /* bridge */ /* synthetic */ Object get(Class cls) {
return super.get(cls);
}
@Override // com.google.firebase.components.AbstractComponentContainer, com.google.firebase.components.ComponentContainer
public /* bridge */ /* synthetic */ Set setOf(Class cls) {
return super.setOf(cls);
}
public static Builder builder(Executor executor) {
return new Builder(executor);
}
private ComponentRuntime(Executor executor, Iterable<Provider<ComponentRegistrar>> iterable, Collection<Component<?>> collection, ComponentRegistrarProcessor componentRegistrarProcessor) {
this.components = new HashMap();
this.lazyInstanceMap = new HashMap();
this.lazySetMap = new HashMap();
this.eagerComponentsInitializedWith = new AtomicReference<>();
EventBus eventBus = new EventBus(executor);
this.eventBus = eventBus;
this.componentRegistrarProcessor = componentRegistrarProcessor;
ArrayList arrayList = new ArrayList();
arrayList.add(Component.of(eventBus, EventBus.class, Subscriber.class, Publisher.class));
arrayList.add(Component.of(this, ComponentLoader.class, new Class[0]));
for (Component<?> component : collection) {
if (component != null) {
arrayList.add(component);
}
}
this.unprocessedRegistrarProviders = iterableToList(iterable);
discoverComponents(arrayList);
}
private void discoverComponents(List<Component<?>> list) {
ArrayList arrayList = new ArrayList();
synchronized (this) {
Iterator<Provider<ComponentRegistrar>> it = this.unprocessedRegistrarProviders.iterator();
while (it.hasNext()) {
try {
ComponentRegistrar componentRegistrar = it.next().get();
if (componentRegistrar != null) {
list.addAll(this.componentRegistrarProcessor.processRegistrar(componentRegistrar));
it.remove();
}
} catch (InvalidRegistrarException unused) {
it.remove();
}
}
if (this.components.isEmpty()) {
CycleDetector.detect(list);
} else {
ArrayList arrayList2 = new ArrayList(this.components.keySet());
arrayList2.addAll(list);
CycleDetector.detect(arrayList2);
}
for (final Component<?> component : list) {
this.components.put(component, new Lazy(new Provider(this, component) { // from class: com.google.firebase.components.ComponentRuntime$$ExternalSyntheticLambda1
public final ComponentRuntime f$0;
public final Component f$1;
@Override // com.google.firebase.inject.Provider
public final Object get() {
return this.f$0.m168xc080f8d8(this.f$1);
}
{
this.f$0 = this;
this.f$1 = component;
}
}));
}
arrayList.addAll(processInstanceComponents(list));
arrayList.addAll(processSetComponents());
processDependencies();
}
Iterator it2 = arrayList.iterator();
while (it2.hasNext()) {
((Runnable) it2.next()).run();
}
maybeInitializeEagerComponents();
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: lambda$discoverComponents$0$com-google-firebase-components-ComponentRuntime, reason: not valid java name */
public /* synthetic */ Object m168xc080f8d8(Component component) {
return component.getFactory().create(new RestrictedComponentContainer(component, this));
}
private void maybeInitializeEagerComponents() {
Boolean bool = this.eagerComponentsInitializedWith.get();
if (bool != null) {
doInitializeEagerComponents(this.components, bool.booleanValue());
}
}
private static <T> List<T> iterableToList(Iterable<T> iterable) {
ArrayList arrayList = new ArrayList();
Iterator<T> it = iterable.iterator();
while (it.hasNext()) {
arrayList.add(it.next());
}
return arrayList;
}
private List<Runnable> processInstanceComponents(List<Component<?>> list) {
ArrayList arrayList = new ArrayList();
for (Component<?> component : list) {
if (component.isValue()) {
final Provider<?> provider = this.components.get(component);
for (Class<? super Object> cls : component.getProvidedInterfaces()) {
if (!this.lazyInstanceMap.containsKey(cls)) {
this.lazyInstanceMap.put(cls, provider);
} else {
final OptionalProvider optionalProvider = (OptionalProvider) this.lazyInstanceMap.get(cls);
arrayList.add(new Runnable(optionalProvider, provider) { // from class: com.google.firebase.components.ComponentRuntime$$ExternalSyntheticLambda0
public final OptionalProvider f$0;
public final Provider f$1;
@Override // java.lang.Runnable
public final void run() {
this.f$0.set(this.f$1);
}
{
this.f$0 = optionalProvider;
this.f$1 = provider;
}
});
}
}
}
}
return arrayList;
}
private List<Runnable> processSetComponents() {
ArrayList arrayList = new ArrayList();
HashMap hashMap = new HashMap();
for (Map.Entry<Component<?>, Provider<?>> entry : this.components.entrySet()) {
Component<?> key = entry.getKey();
if (!key.isValue()) {
Provider<?> value = entry.getValue();
for (Class<? super Object> cls : key.getProvidedInterfaces()) {
if (!hashMap.containsKey(cls)) {
hashMap.put(cls, new HashSet());
}
((Set) hashMap.get(cls)).add(value);
}
}
}
for (Map.Entry entry2 : hashMap.entrySet()) {
if (!this.lazySetMap.containsKey(entry2.getKey())) {
this.lazySetMap.put((Class) entry2.getKey(), LazySet.fromCollection((Collection) entry2.getValue()));
} else {
final LazySet<?> lazySet = this.lazySetMap.get(entry2.getKey());
for (final Provider provider : (Set) entry2.getValue()) {
arrayList.add(new Runnable(lazySet, provider) { // from class: com.google.firebase.components.ComponentRuntime$$ExternalSyntheticLambda2
public final LazySet f$0;
public final Provider f$1;
@Override // java.lang.Runnable
public final void run() {
this.f$0.add(this.f$1);
}
{
this.f$0 = lazySet;
this.f$1 = provider;
}
});
}
}
}
return arrayList;
}
@Override // com.google.firebase.components.ComponentContainer
public <T> Provider<T> getProvider(Class<T> cls) {
Provider<T> provider;
synchronized (this) {
Preconditions.checkNotNull(cls, "Null interface requested.");
provider = (Provider) this.lazyInstanceMap.get(cls);
}
return provider;
}
@Override // com.google.firebase.components.ComponentContainer
public <T> Deferred<T> getDeferred(Class<T> cls) {
Provider<T> provider = getProvider(cls);
if (provider == null) {
return OptionalProvider.empty();
}
if (provider instanceof OptionalProvider) {
return (OptionalProvider) provider;
}
return OptionalProvider.of(provider);
}
@Override // com.google.firebase.components.ComponentContainer
public <T> Provider<Set<T>> setOfProvider(Class<T> cls) {
synchronized (this) {
LazySet<?> lazySet = this.lazySetMap.get(cls);
if (lazySet != null) {
return lazySet;
}
return (Provider<Set<T>>) EMPTY_PROVIDER;
}
}
public void initializeEagerComponents(boolean z) {
HashMap hashMap;
AtomicReference<Boolean> atomicReference = this.eagerComponentsInitializedWith;
Boolean valueOf = Boolean.valueOf(z);
while (!atomicReference.compareAndSet(null, valueOf)) {
if (atomicReference.get() != null) {
return;
}
}
synchronized (this) {
hashMap = new HashMap(this.components);
}
doInitializeEagerComponents(hashMap, z);
}
private void doInitializeEagerComponents(Map<Component<?>, Provider<?>> map, boolean z) {
for (Map.Entry<Component<?>, Provider<?>> entry : map.entrySet()) {
Component<?> key = entry.getKey();
Provider<?> value = entry.getValue();
if (key.isAlwaysEager() || (key.isEagerInDefaultApp() && z)) {
value.get();
}
}
this.eventBus.enablePublishingAndFlushPending();
}
private void processDependencies() {
for (Component<?> component : this.components.keySet()) {
for (Dependency dependency : component.getDependencies()) {
if (dependency.isSet() && !this.lazySetMap.containsKey(dependency.getInterface())) {
this.lazySetMap.put(dependency.getInterface(), LazySet.fromCollection(Collections.emptySet()));
} else if (this.lazyInstanceMap.containsKey(dependency.getInterface())) {
continue;
} else {
if (dependency.isRequired()) {
throw new MissingDependencyException(String.format("Unsatisfied dependency for component %s: %s", component, dependency.getInterface()));
}
if (!dependency.isSet()) {
this.lazyInstanceMap.put(dependency.getInterface(), OptionalProvider.empty());
}
}
}
}
}
/* loaded from: classes.dex */
public static final class Builder {
private final Executor defaultExecutor;
private final List<Provider<ComponentRegistrar>> lazyRegistrars = new ArrayList();
private final List<Component<?>> additionalComponents = new ArrayList();
private ComponentRegistrarProcessor componentRegistrarProcessor = ComponentRegistrarProcessor.NOOP;
/* JADX INFO: Access modifiers changed from: package-private */
public static /* synthetic */ ComponentRegistrar lambda$addComponentRegistrar$0(ComponentRegistrar componentRegistrar) {
return componentRegistrar;
}
Builder(Executor executor) {
this.defaultExecutor = executor;
}
public final Builder addLazyComponentRegistrars(Collection<Provider<ComponentRegistrar>> collection) {
this.lazyRegistrars.addAll(collection);
return this;
}
public final Builder addComponentRegistrar(final ComponentRegistrar componentRegistrar) {
this.lazyRegistrars.add(new Provider(componentRegistrar) { // from class: com.google.firebase.components.ComponentRuntime$Builder$$ExternalSyntheticLambda0
public final ComponentRegistrar f$0;
@Override // com.google.firebase.inject.Provider
public final Object get() {
return ComponentRuntime.Builder.lambda$addComponentRegistrar$0(this.f$0);
}
{
this.f$0 = componentRegistrar;
}
});
return this;
}
public final Builder addComponent(Component<?> component) {
this.additionalComponents.add(component);
return this;
}
public final ComponentRuntime build() {
return new ComponentRuntime(this.defaultExecutor, this.lazyRegistrars, this.additionalComponents, this.componentRegistrarProcessor);
}
public final Builder setProcessor(ComponentRegistrarProcessor componentRegistrarProcessor) {
this.componentRegistrarProcessor = componentRegistrarProcessor;
return this;
}
}
}